AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: | simple-websockets-chat-app SAM Template for simple-websockets-chat-app that has the DynamoDB table and Lambda functions needed to demonstrate the Websocket protocol on API Gateway. Parameters: TableName: Type: String Default: simplechat_connections Description: (Required) The name of the new DynamoDB to store connection identifiers for each connected clients. Minimum 3 characters MinLength: 3 MaxLength: 50 AllowedPattern: ^[A-Za-z_]+$ ConstraintDescription: Required. Can be characters and underscore only. No numbers or special characters allowed. Resources: SimpleChatWebSocket: Type: AWS::ApiGatewayV2::Api Properties: Name: SimpleChatWebSocket ProtocolType: WEBSOCKET RouteSelectionExpression: $request.body.message ConnectRoute: Type: AWS::ApiGatewayV2::Route Properties: ApiId: Ref: SimpleChatWebSocket RouteKey: $connect AuthorizationType: NONE OperationName: ConnectRoute Target: Fn::Join: - / - - integrations - Ref: ConnectInteg ConnectInteg: Type: AWS::ApiGatewayV2::Integration Properties: ApiId: Ref: SimpleChatWebSocket Description: Connect Integration IntegrationType: AWS_PROXY IntegrationUri: Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${OnConnectFunction.Arn}/invocations DisconnectRoute: Type: AWS::ApiGatewayV2::Route Properties: ApiId: Ref: SimpleChatWebSocket RouteKey: $disconnect AuthorizationType: NONE OperationName: DisconnectRoute Target: Fn::Join: - / - - integrations - Ref: DisconnectInteg DisconnectInteg: Type: AWS::ApiGatewayV2::Integration Properties: ApiId: Ref: SimpleChatWebSocket Description: Disconnect Integration IntegrationType: AWS_PROXY IntegrationUri: Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${OnDisconnectFunction.Arn}/invocations SendRoute: Type: AWS::ApiGatewayV2::Route Properties: ApiId: Ref: SimpleChatWebSocket RouteKey: sendMessage AuthorizationType: NONE OperationName: SendRoute Target: Fn::Join: - / - - integrations - Ref: SendInteg SendInteg: Type: AWS::ApiGatewayV2::Integration Properties: ApiId: Ref: SimpleChatWebSocket Description: Send Integration IntegrationType: AWS_PROXY IntegrationUri: Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${SendMessageFunction.Arn}/invocations Deployment: Type: AWS::ApiGatewayV2::Deployment DependsOn: - ConnectRoute - SendRoute - DisconnectRoute Properties: ApiId: Ref: SimpleChatWebSocket Stage: Type: AWS::ApiGatewayV2::Stage Properties: StageName: Prod Description: Prod Stage DeploymentId: Ref: Deployment ApiId: Ref: SimpleChatWebSocket ConnectionsTable: Type: AWS::DynamoDB::Table Properties: AttributeDefinitions: - AttributeName: connectionId AttributeType: S KeySchema: - AttributeName: connectionId KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 SSESpecification: SSEEnabled: true TableName: Ref: TableName OnConnectFunction: Type: AWS::Serverless::Function Properties: CodeUri: Bucket: <%REPO_BUCKET%> Key: 07a400a4-d13f-4d23-ac71-8b244284d184 Handler: app.handler MemorySize: 256 Runtime: nodejs10.x Environment: Variables: TABLE_NAME: Ref: TableName Policies: - DynamoDBCrudPolicy: TableName: Ref: TableName OnConnectPermission: Type: AWS::Lambda::Permission DependsOn: - SimpleChatWebSocket - OnConnectFunction Properties: Action: lambda:InvokeFunction FunctionName: Ref: OnConnectFunction Principal: apigateway.amazonaws.com OnDisconnectFunction: Type: AWS::Serverless::Function Properties: CodeUri: Bucket: <%REPO_BUCKET%> Key: 0832c5f6-a81c-40dd-8f84-de6abe2e5401 Handler: app.handler MemorySize: 256 Runtime: nodejs10.x Environment: Variables: TABLE_NAME: Ref: TableName Policies: - DynamoDBCrudPolicy: TableName: Ref: TableName OnDisconnectPermission: Type: AWS::Lambda::Permission DependsOn: - SimpleChatWebSocket - OnDisconnectFunction Properties: Action: lambda:InvokeFunction FunctionName: Ref: OnDisconnectFunction Principal: apigateway.amazonaws.com SendMessageFunction: Type: AWS::Serverless::Function Properties: CodeUri: Bucket: <%REPO_BUCKET%> Key: d1638bef-3e9c-4b79-ade6-37cb03f25b2a Handler: app.handler MemorySize: 256 Runtime: nodejs10.x Environment: Variables: TABLE_NAME: Ref: TableName Policies: - DynamoDBCrudPolicy: TableName: Ref: TableName - Statement: - Effect: Allow Action: - execute-api:ManageConnections Resource: - Fn::Sub: arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${SimpleChatWebSocket}/* SendMessagePermission: Type: AWS::Lambda::Permission DependsOn: - SimpleChatWebSocket - SendMessageFunction Properties: Action: lambda:InvokeFunction FunctionName: Ref: SendMessageFunction Principal: apigateway.amazonaws.com Outputs: ConnectionsTableArn: Description: Connections table ARN Value: Fn::GetAtt: - ConnectionsTable - Arn OnConnectFunctionArn: Description: OnConnect function ARN Value: Fn::GetAtt: - OnConnectFunction - Arn OnDisconnectFunctionArn: Description: OnDisconnect function ARN Value: Fn::GetAtt: - OnDisconnectFunction - Arn SendMessageFunctionArn: Description: SendMessage function ARN Value: Fn::GetAtt: - SendMessageFunction - Arn WebSocketURI: Description: The WSS Protocol URI to connect to Value: Fn::Join: - '' - - wss:// - Ref: SimpleChatWebSocket - .execute-api. - Ref: AWS::Region - .amazonaws.com/ - Ref: Stage