AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Amazon IVS Simple Chat demo 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: # Amazon IVS Channel: Type: AWS::IVS::Channel Properties: Name: simple-chat-demo StreamKey: Type: AWS::IVS::StreamKey Properties: ChannelArn: !Ref Channel # Chat serverless backend SimpleChatWebSocket: Type: AWS::ApiGatewayV2::Api Properties: Name: SimpleChatWebSocket ProtocolType: WEBSOCKET RouteSelectionExpression: "$request.body.action" ConnectIntegration: 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 ConnectRoute: Type: AWS::ApiGatewayV2::Route Properties: ApiId: !Ref SimpleChatWebSocket RouteKey: $connect AuthorizationType: NONE OperationName: ConnectRoute Target: !Join - '/' - - 'integrations' - !Ref ConnectIntegration SendMessageIntegration: 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 SendRoute: Type: AWS::ApiGatewayV2::Route Properties: ApiId: !Ref SimpleChatWebSocket RouteKey: sendmessage AuthorizationType: NONE OperationName: SendRoute Target: !Join - '/' - - 'integrations' - !Ref SendMessageIntegration DisconnectIntegration: 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 DisconnectRoute: Type: AWS::ApiGatewayV2::Route Properties: ApiId: !Ref SimpleChatWebSocket RouteKey: $disconnect AuthorizationType: NONE OperationName: DisconnectRoute Target: !Join - '/' - - 'integrations' - !Ref DisconnectIntegration 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: lambda/ Handler: app.onConnect MemorySize: 256 Runtime: nodejs12.x Environment: Variables: TABLE_NAME: !Ref TableName Policies: - DynamoDBCrudPolicy: TableName: !Ref TableName SendMessageFunction: Type: AWS::Serverless::Function Properties: CodeUri: lambda/ Handler: app.sendMessage MemorySize: 256 Runtime: nodejs12.x Environment: Variables: TABLE_NAME: !Ref TableName Policies: - DynamoDBCrudPolicy: TableName: !Ref TableName - Statement: - Effect: Allow Action: - 'execute-api:ManageConnections' Resource: - !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${SimpleChatWebSocket}/*' SendMessagePermission: Type: AWS::Lambda::Permission DependsOn: - SimpleChatWebSocket Properties: Action: lambda:InvokeFunction FunctionName: !Ref SendMessageFunction Principal: apigateway.amazonaws.com OnConnectPermission: Type: AWS::Lambda::Permission DependsOn: - SimpleChatWebSocket Properties: Action: lambda:InvokeFunction FunctionName: !Ref OnConnectFunction Principal: apigateway.amazonaws.com OnDisconnectFunction: Type: AWS::Serverless::Function Properties: CodeUri: lambda/ Handler: app.onDisconnect MemorySize: 256 Runtime: nodejs12.x Environment: Variables: TABLE_NAME: !Ref TableName Policies: - DynamoDBCrudPolicy: TableName: !Ref TableName OnDisconnectPermission: Type: AWS::Lambda::Permission DependsOn: - SimpleChatWebSocket Properties: Action: lambda:InvokeFunction FunctionName: !Ref OnDisconnectFunction Principal: apigateway.amazonaws.com Outputs: WebSocketURI: Description: "The WSS Protocol URI to connect to" Value: !Join [ '', [ 'wss://', !Ref SimpleChatWebSocket, '.execute-api.',!Ref 'AWS::Region','.amazonaws.com/',!Ref 'Stage'] ] ChannelArn: Description: "Amazon IVS channel ARN:" Value: !Ref Channel ChannelIngestEndpoint: Description: "Amazon IVS ingest server:" Value: !Join [ '', [ 'rtmps://', !GetAtt Channel.IngestEndpoint, ':443/app/'] ] StreamKey: Description: "Amazon IVS stream key:" Value: !GetAtt StreamKey.Value ChannelPlaybackUrl: Description: "Amazon IVS playback URL:" Value: !GetAtt Channel.PlaybackUrl