Created
September 6, 2019 06:46
-
-
Save cm-fujii/5854ff037d532a5d712fa6e0bc9d99cf to your computer and use it in GitHub Desktop.
AWS-CDK-ResourceNameSample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as apigateway from '@aws-cdk/aws-apigateway'; | |
import * as lambda from '@aws-cdk/aws-lambda'; | |
import * as dynamodb from '@aws-cdk/aws-dynamodb'; | |
import { Duration } from '@aws-cdk/core'; | |
import cdk = require('@aws-cdk/core'); | |
import { ResourceName } from './resourceName'; | |
export class AwscdkResourceNameSampleStack extends cdk.Stack { | |
constructor(scope: cdk.Construct, resourceName: ResourceName, props?: cdk.StackProps) { | |
const id = resourceName.stackName('App'); | |
super(scope, id, props); | |
// DynamoDB | |
const userTableName = resourceName.dynamodbName('user'); | |
const userTable = new dynamodb.Table(this, userTableName, { | |
tableName: userTableName, | |
partitionKey: { | |
name: 'userId', | |
type: dynamodb.AttributeType.STRING, | |
}, | |
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, | |
}); | |
// Lmabda (ユーザ作成) | |
const createUserLambdaName = resourceName.lambdaName('create-user'); | |
const createUserLambda = new lambda.Function(this, createUserLambdaName, { | |
code: lambda.Code.asset('src/lambda'), | |
handler: 'createUser.handler', | |
runtime: lambda.Runtime.NODEJS_10_X, | |
functionName: createUserLambdaName, | |
timeout: Duration.seconds(3), | |
environment: { | |
SYSTEM_ENV: resourceName.systemEnv, | |
USER_TABLE_NAME: userTable.tableName, | |
} | |
}); | |
// Lmabda (ユーザ取得) | |
const getUserLambdaName = resourceName.lambdaName('get-user'); | |
const getUserLambda = new lambda.Function(this, getUserLambdaName, { | |
code: lambda.Code.asset('src/lambda'), | |
handler: 'getUser.handler', | |
runtime: lambda.Runtime.NODEJS_10_X, | |
functionName: getUserLambdaName, | |
timeout: Duration.seconds(3), | |
environment: { | |
SYSTEM_ENV: resourceName.systemEnv, | |
USER_TABLE_NAME: userTable.tableName, | |
} | |
}); | |
// API Gateway | |
const apiName = resourceName.apiName('user'); | |
const api = new apigateway.RestApi(this, apiName, { | |
restApiName: apiName, | |
}); | |
const integrationCreateUserLambda = new apigateway.LambdaIntegration(createUserLambda, { | |
proxy: true, | |
}); | |
const integrationGetUserLambda = new apigateway.LambdaIntegration(getUserLambda, { | |
proxy: true, | |
}); | |
const userResource = api.root.addResource('user'); | |
const idResource = userResource.addResource('{id}'); | |
idResource.addMethod('POST', integrationCreateUserLambda); | |
idResource.addMethod('GET', integrationGetUserLambda); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
import 'source-map-support/register'; | |
import cdk = require('@aws-cdk/core'); | |
import { AwscdkResourceNameSampleStack } from '../lib/awscdk-resource_name_sample-stack'; | |
import { ResourceName } from '../lib/resourceName'; | |
const systemEnv = process.env.SYSTEM_ENV ? process.env.SYSTEM_ENV : 'dev'; | |
const resourceName = new ResourceName('AWS-CDK-Resource-Sample', systemEnv); | |
const app = new cdk.App(); | |
new AwscdkResourceNameSampleStack(app, resourceName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function handler(event: any) { | |
const id = event.pathParameters.id; | |
// 作ったことにする | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ | |
env: process.env.SYSTEM_ENV, | |
message: `create user id: ${id}` | |
}), | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function handler(event: any) { | |
const id = event.pathParameters.id; | |
// 取得したことにする | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ | |
env: process.env.SYSTEM_ENV, | |
message: `get user id: ${id}` | |
}), | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment