|
| 1 | +AWSTemplateFormatVersion: 2010-09-09 |
| 2 | +Transform: 'AWS::Serverless-2016-10-31' |
| 3 | +Description: An Amazon API Gateway WebSocket API and an AWS Lambda function. |
| 4 | + |
| 5 | +# Global values that are applied to all applicable resources in this template |
| 6 | +Globals: |
| 7 | + Function: |
| 8 | + CodeUri: ./src |
| 9 | + Runtime: python3.12 |
| 10 | + MemorySize: 128 |
| 11 | + Timeout: 15 |
| 12 | + |
| 13 | +Resources: |
| 14 | + # API Gateway WebSocket API |
| 15 | + WebSocketApi: |
| 16 | + Type: 'AWS::ApiGatewayV2::Api' |
| 17 | + Properties: |
| 18 | + Name: !Ref AWS::StackName |
| 19 | + Description: An Amazon API Gateway WebSocket API and an AWS Lambda function. |
| 20 | + ProtocolType: WEBSOCKET |
| 21 | + RouteSelectionExpression: "$request.body.action" |
| 22 | + # Lambda Function - uses Globals to define additional configuration values |
| 23 | + OnConnectLambdaFunction: |
| 24 | + Type: 'AWS::Serverless::Function' |
| 25 | + Properties: |
| 26 | + FunctionName: !Sub '${AWS::StackName}-onconnect-function' |
| 27 | + Handler: lambda_function.lambda_handler |
| 28 | + MemorySize: 256 |
| 29 | + Policies: |
| 30 | + - Statement: |
| 31 | + - Effect: Allow |
| 32 | + Action: |
| 33 | + - 'lambda:InvokeFunction' |
| 34 | + Resource: |
| 35 | + - !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:WebsocketPostToConnectionId' |
| 36 | + # Function permissions grant an AWS service or another account permission to use a function |
| 37 | + OnConnectFunctionResourcePermission: |
| 38 | + Type: 'AWS::Lambda::Permission' |
| 39 | + Properties: |
| 40 | + Action: 'lambda:InvokeFunction' |
| 41 | + Principal: apigateway.amazonaws.com |
| 42 | + FunctionName: !Ref OnConnectLambdaFunction |
| 43 | + SourceArn: !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${WebSocketApi}/*' |
| 44 | + OnConnectIntegration: |
| 45 | + Type: AWS::ApiGatewayV2::Integration |
| 46 | + Properties: |
| 47 | + ApiId: !Ref WebSocketApi |
| 48 | + Description: OnConnect Integration |
| 49 | + IntegrationType: AWS_PROXY |
| 50 | + IntegrationUri: |
| 51 | + Fn::Sub: |
| 52 | + arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${OnConnectLambdaFunction.Arn}/invocations |
| 53 | + OnConnectRoute: |
| 54 | + Type: AWS::ApiGatewayV2::Route |
| 55 | + Properties: |
| 56 | + ApiId: !Ref WebSocketApi |
| 57 | + RouteKey: $connect |
| 58 | + AuthorizationType: NONE |
| 59 | + OperationName: OnConnectRoute |
| 60 | + Target: !Join |
| 61 | + - '/' |
| 62 | + - - 'integrations' |
| 63 | + - !Ref OnConnectIntegration |
| 64 | + # Lambda Function - uses Globals to define additional configuration values |
| 65 | + PostLambdaFunction: |
| 66 | + Type: 'AWS::Serverless::Function' |
| 67 | + Properties: |
| 68 | + FunctionName: WebsocketPostToConnectionId |
| 69 | + Handler: lambda_function_postToConnection.lambda_handler |
| 70 | + MemorySize: 256 |
| 71 | + Policies: |
| 72 | + - Statement: |
| 73 | + - Effect: Allow |
| 74 | + Action: |
| 75 | + - 'execute-api:ManageConnections' |
| 76 | + Resource: |
| 77 | + - !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${WebSocketApi}/*' |
| 78 | + # Function permissions grant an AWS service or another account permission to use a function |
| 79 | + |
| 80 | + OnDisconnectLambdaFunction: |
| 81 | + Type: 'AWS::Serverless::Function' |
| 82 | + Properties: |
| 83 | + FunctionName: !Sub '${AWS::StackName}-ondisconnect-function' |
| 84 | + Handler: ondisconnect.lambda_handler |
| 85 | + MemorySize: 256 |
| 86 | + # Function permissions grant an AWS service or another account permission to use a function |
| 87 | + OnDisconnectFunctionResourcePermission: |
| 88 | + Type: 'AWS::Lambda::Permission' |
| 89 | + Properties: |
| 90 | + Action: 'lambda:InvokeFunction' |
| 91 | + Principal: apigateway.amazonaws.com |
| 92 | + FunctionName: !Ref OnDisconnectLambdaFunction |
| 93 | + SourceArn: !Sub 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${WebSocketApi}/*' |
| 94 | + OnDisconnectIntegration: |
| 95 | + Type: AWS::ApiGatewayV2::Integration |
| 96 | + Properties: |
| 97 | + ApiId: !Ref WebSocketApi |
| 98 | + Description: OnDisconnect Integration |
| 99 | + IntegrationType: AWS_PROXY |
| 100 | + IntegrationUri: |
| 101 | + Fn::Sub: |
| 102 | + arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${OnDisconnectLambdaFunction.Arn}/invocations |
| 103 | + OnDisconnectRoute: |
| 104 | + Type: AWS::ApiGatewayV2::Route |
| 105 | + Properties: |
| 106 | + ApiId: !Ref WebSocketApi |
| 107 | + RouteKey: $disconnect |
| 108 | + AuthorizationType: NONE |
| 109 | + OperationName: OnDisconnectRoute |
| 110 | + Target: !Join |
| 111 | + - '/' |
| 112 | + - - 'integrations' |
| 113 | + - !Ref OnDisconnectIntegration |
| 114 | + Deployment: |
| 115 | + Type: AWS::ApiGatewayV2::Deployment |
| 116 | + DependsOn: |
| 117 | + - OnConnectRoute |
| 118 | + - OnDisconnectRoute |
| 119 | + Properties: |
| 120 | + ApiId: !Ref WebSocketApi |
| 121 | + Stage: |
| 122 | + Type: AWS::ApiGatewayV2::Stage |
| 123 | + Properties: |
| 124 | + StageName: prod |
| 125 | + Description: Prod Stage |
| 126 | + DeploymentId: !Ref Deployment |
| 127 | + ApiId: !Ref WebSocketApi |
| 128 | + |
| 129 | +Outputs: |
| 130 | + OnConnectLambdaFunctionArn: |
| 131 | + Description: "OnConnect function ARN" |
| 132 | + Value: !GetAtt OnConnectLambdaFunction.Arn |
| 133 | + OnDisconnectLambdaFunctionArn: |
| 134 | + Description: "OnDisconnect function ARN" |
| 135 | + Value: !GetAtt OnDisconnectLambdaFunction.Arn |
| 136 | + PostLambdaFunctionArn: |
| 137 | + Description: "Post function ARN" |
| 138 | + Value: !GetAtt PostLambdaFunction.Arn |
| 139 | + WebSocketURL: |
| 140 | + Description: "The WSS Protocol URL to connect to" |
| 141 | + Value: !Join [ '', [ 'wss://', !Ref WebSocketApi, '.execute-api.',!Ref 'AWS::Region','.amazonaws.com/',!Ref 'Stage'] ] |
| 142 | + |
0 commit comments