Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

環境変数によるシステム環境の分離とActionsの有効化 #16

Merged
merged 7 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/cdk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: cdk

on:
push:
branches:
- main
pull_request:
jobs:
aws_cdk:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Main Branch Check
if: ${{ github.ref == 'refs/heads/main' }}
run: echo "SYSTEM_ENV=stg" >> $GITHUB_ENV

- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: "14.x"

- name: Setup dependencies
run: yarn --frozen-lockfile

- name: Build
run: yarn run build

- name: Unit tests
if: contains(github.event_name, 'pull_request')
run: yarn run test

- name: CDK Diff Check
if: contains(github.event_name, 'pull_request')
run: yarn run cdk diff
env:
AWS_DEFAULT_REGION: "ap-northeast-1"
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

- name: CDK Deploy
if: contains(github.event_name, 'push')
run: yarn run deploy
env:
AWS_DEFAULT_REGION: "ap-northeast-1"
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
20 changes: 5 additions & 15 deletions bin/ohakuma-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@
import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import { OhakumaApiStack } from '../lib/ohakuma-api-stack';
import { ResourceName } from '../lib/resourceName';

const app = new cdk.App();
new OhakumaApiStack(app, 'OhakumaApiStack', {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */

/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
const systemEnv = process.env.SYSTEM_ENV ? process.env.SYSTEM_ENV : 'dev';
const resourceName = new ResourceName('Ohakuma', systemEnv);

/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },

/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
const app = new cdk.App();
new OhakumaApiStack(app, resourceName);
File renamed without changes.
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ module.exports = {
'^.+\\.tsx?$': 'ts-jest'
}
};

process.env = Object.assign(process.env, { TABLE_NAME: 'local-table' });

38 changes: 25 additions & 13 deletions lib/ohakuma-api-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,42 @@ import * as cdk from '@aws-cdk/core';
import * as apigw from '@aws-cdk/aws-apigateway';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import { ResourceName } from './resourceName';

export class OhakumaApiStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
constructor(
scope: cdk.Construct,
resourceName: ResourceName,
props?: cdk.StackProps
) {
const id = resourceName.stackName('Api');
super(scope, id, props);

const tableName = 'ohakumaTable';
// The code that defines your stack goes here
const appLambda = new NodejsFunction(this, 'appLambda', {
// DynamoDB
const bearTableName = resourceName.dynamodbName('bear');
const bearTable = new dynamodb.Table(this, bearTableName, {
tableName: bearTableName,
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
});

// Lambda
const manageBearLambdaName = resourceName.lambdaName('manage-bear');
const manageBearLambda = new NodejsFunction(this, manageBearLambdaName, {
functionName: manageBearLambdaName,
entry: 'src/lambda/handlers/api-gw/index.ts',
handler: 'handler',
environment: {
TABLE_NAME: tableName,
TABLE_NAME: bearTableName,
},
});

new apigw.LambdaRestApi(this, 'ohakumaApi', {
handler: appLambda,
});
bearTable.grantReadWriteData(manageBearLambda);

const table = new dynamodb.Table(this, 'ohakumaTable', {
tableName: tableName,
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
// API Gateway
const apiName = resourceName.apiName('manage-bear');
new apigw.LambdaRestApi(this, apiName, {
restApiName: apiName,
handler: manageBearLambda,
});

table.grantReadWriteData(appLambda);
}
}
29 changes: 29 additions & 0 deletions lib/resourceName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export class ResourceName {
public readonly systemName: string;
public readonly systemEnv: string;

constructor(systemName: string, systemEnv: string) {
this.systemName = systemName;
this.systemEnv = systemEnv;
}

private basicName(name: string): string {
return `${this.systemName}-${this.systemEnv}-${name}`;
}

public apiName(name: string): string {
return this.basicName(`${name}-api`);
}

public lambdaName(name: string): string {
return this.basicName(`${name}-function`);
}

public dynamodbName(name: string): string {
return this.basicName(`${name}-table`);
}

public stackName(name: string): string {
return this.basicName(`${name}-stack`);
}
}
Loading