Skip to content

Instantly share code, notes, and snippets.

@hassaku63
Created December 28, 2020 10:25
Show Gist options
  • Save hassaku63/87bcc1952c05af6ca8adeb0c096b6253 to your computer and use it in GitHub Desktop.
Save hassaku63/87bcc1952c05af6ca8adeb0c096b6253 to your computer and use it in GitHub Desktop.
aws-cdk example - API Gateway V2 HTTP API & lambda NodejsFunction
import * as path from 'path';
import * as cdk from '@aws-cdk/core';
import * as ddb from '@aws-cdk/aws-dynamodb';
import * as apiGatewayV2 from '@aws-cdk/aws-apigatewayv2';
import { LambdaProxyIntegration } from '@aws-cdk/aws-apigatewayv2-integrations';
import { NodejsFunction, NodejsFunctionProps } from '@aws-cdk/aws-lambda-nodejs';
import { HttpIntegration, HttpIntegrationType } from '@aws-cdk/aws-apigatewayv2';
import { dirname } from 'path';
const project_root_dir = path.join(dirname(__filename), '../')
export interface DetermineApiStackProps {
application: string;
stage: string;
}
export class DetermineApiStack extends cdk.Stack {
dataStore: DataStore;
api: DeterminationApi;
constructor(scope: cdk.Construct, id: string, props: DetermineApiStackProps) {
super(scope, id);
this.dataStore = new DataStore(this, `DataStore${props.stage}`, {
application: 'DeterminApi',
stage: props.stage,
});
this.api = new DeterminationApi(this, `Api${props.stage}`, {
application: 'DetermineApi',
stage: props.stage,
})
/**
* Stack Output
*/
new cdk.CfnOutput(this, `TableName`, {
value: this.dataStore.getTableName(),
});
new cdk.CfnOutput(this, `TableArn`, {
value: this.dataStore.getTableArn(),
});
new cdk.CfnOutput(this, `StreamArn`, {
value: this.getDdbStreamArn(),
});
}
getDdbStreamArn (): string {
return this.dataStore.getStreamArn();
}
}
interface ApiProps {
application: string;
stage: string;
}
class DeterminationApi extends cdk.Construct {
private api: apiGatewayV2.HttpApi;
private routes: apiGatewayV2.HttpRoute[];
constructor(scope: cdk.Construct, id: string, props: ApiProps) {
super(scope, id);
this.api = new apiGatewayV2.HttpApi(this, 'HttpApi', {
apiName: `${props.application}-${props.stage}`,
createDefaultStage: true,
});
// lambda integration
const lambdaFunc = new NodejsFunction(this, 'hello-function', {
entry: path.resolve(path.dirname(__filename), '../functions/determination-api/handler.ts'),
handler: 'handler',
});
const lambdaIntegration = new LambdaProxyIntegration({
handler: lambdaFunc,
});
// route
this.routes = this.api.addRoutes({
path: '/hello',
methods: [ apiGatewayV2.HttpMethod.GET ],
integration: lambdaIntegration,
});
}
getRoutes (): apiGatewayV2.HttpRoute[] {
return this.routes;
}
}
interface DataStoreProps {
application: string;
stage: string;
}
class DataStore extends cdk.Construct {
private table: ddb.Table;
private partitionKey: ddb.Attribute;
private sortKey: ddb.Attribute;
constructor(scope: cdk.Construct, id: string, props: DataStoreProps) {
super(scope, id);
this.partitionKey = { name: 'Id', type: ddb.AttributeType.STRING };
this.sortKey = { name: 'Created', type: ddb.AttributeType.NUMBER };
this.table = new ddb.Table(this, `Ddb${props.application}${props.stage}`, {
partitionKey: this.partitionKey,
sortKey: this.sortKey,
billingMode: ddb.BillingMode.PAY_PER_REQUEST,
stream: ddb.StreamViewType.NEW_AND_OLD_IMAGES,
});
}
getTableName(): string {
return this.table.tableName;
}
getTableArn(): string {
return this.table.tableArn;
}
getStreamArn(): string {
if (this.table.tableStreamArn) {
return this.table.tableArn;
}
throw ReferenceError();
}
getPartitionKey(): ddb.Attribute {
return this.partitionKey;
}
getSortKey(): ddb.Attribute {
return this.sortKey;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment