1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| import * as cdk from 'aws-cdk-lib'; import { WebSocketApi, WebSocketStage } from 'aws-cdk-lib/aws-apigatewayv2'; import { WebSocketLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations'; import { BillingMode, Table } from 'aws-cdk-lib/aws-dynamodb'; import { Architecture, Runtime } from 'aws-cdk-lib/aws-lambda'; import { Construct } from 'constructs';
export class CdkNodeExampleStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props);
const table = new cdk.aws_dynamodb.Table(this, 'ConnectionTable', { billingMode: BillingMode.PAY_PER_REQUEST, removalPolicy: cdk.RemovalPolicy.DESTROY, partitionKey: { name: 'connectionId', type: cdk.aws_dynamodb.AttributeType.STRING, }, });
const connectHandler = this.connectHandlerBuilder(table); const disconnectHandler = this.disconnectHandlerBuilder(table); const sendMessageHandler = this.sendMessageHandlerBuilder(table); const defaultHandler = this.defaultHandlerBuilder();
const webSocketApi = new WebSocketApi(this, 'CdkNodeExampleWebSocketApi', { routeSelectionExpression: '$request.body.action', connectRouteOptions: { integration: new WebSocketLambdaIntegration('ConnectIntegration', connectHandler), }, disconnectRouteOptions: { integration: new WebSocketLambdaIntegration('DisconnectIntegration', disconnectHandler), }, defaultRouteOptions: { integration: new WebSocketLambdaIntegration('DefaultIntegration', defaultHandler), }, });
webSocketApi.addRoute('send-message', { integration: new WebSocketLambdaIntegration('SendMessageIntegration', sendMessageHandler), }); webSocketApi.grantManageConnections(sendMessageHandler); webSocketApi.grantManageConnections(defaultHandler);
new WebSocketStage(this, 'CdkNodeExampleProductionStage', { webSocketApi, stageName: 'production', autoDeploy: true, }); }
connectHandlerBuilder(table: Table) { const handler = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'ConnectHandler', { environment: { TABLE_NAME: table.tableName, }, architecture: Architecture.ARM_64, runtime: Runtime.NODEJS_20_X, entry: 'lambda/connect-handler.ts', });
table.grantWriteData(handler);
return handler; }
disconnectHandlerBuilder(table: Table) { const handler = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'DisconnectHandler', { environment: { TABLE_NAME: table.tableName, }, architecture: Architecture.ARM_64, runtime: Runtime.NODEJS_20_X, entry: 'lambda/disconnect-handler.ts', });
table.grantWriteData(handler);
return handler; }
sendMessageHandlerBuilder(table: Table) { const handler = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'SendMessageHandler', { environment: { TABLE_NAME: table.tableName, }, architecture: Architecture.ARM_64, runtime: Runtime.NODEJS_20_X, entry: 'lambda/send-handler.ts', });
table.grantReadWriteData(handler);
return handler; }
defaultHandlerBuilder() { return new cdk.aws_lambda_nodejs.NodejsFunction(this, 'DefaultHandler', { architecture: Architecture.ARM_64, runtime: Runtime.NODEJS_20_X, entry: 'lambda/default-handler.ts', }); } }
|