Forked from maatthc/aws-websockets-api-gw-mock.yaml
Created
February 12, 2021 10:27
-
-
Save veve90/54629c0fc735b79cbbd3a09acdcb1fc1 to your computer and use it in GitHub Desktop.
Basic WebSocket mock in AWS ApiGateway using CloudFormation in AWS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: '2010-09-09' | |
Description: | | |
AWS CloudFormation template for Mock WebSocket API Gateway. When deploying this stack please remember to check the option: | |
- I acknowledge that AWS CloudFormation might create IAM resources. | |
This template can help you to solve issues like: | |
- CloudWatch Logs role ARN must be set in account settings to enable logging | |
- Execution failed due to configuration error: statusCode should be an integer which defined in request template | |
- This custom domain name cannot map to WEBSOCKET protocol APIs | |
- Error during WebSocket handshake: Unexpected response code: 500 | |
Resources: | |
ApiGwAccountConfig: | |
Type: "AWS::ApiGateway::Account" | |
Properties: | |
CloudWatchRoleArn: !GetAtt "ApiGatewayLoggingRole.Arn" | |
ApiGatewayLoggingRole: | |
Type: "AWS::IAM::Role" | |
Properties: | |
AssumeRolePolicyDocument: | |
Version: "2012-10-17" | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: | |
- "apigateway.amazonaws.com" | |
Action: "sts:AssumeRole" | |
Path: "/" | |
ManagedPolicyArns: | |
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs" | |
WebSocketMock: | |
Type: AWS::ApiGatewayV2::Api | |
Properties: | |
Name: WebSocketMock | |
ProtocolType: WEBSOCKET | |
RouteSelectionExpression: "$request.body.message" | |
Description: "Mock WebSocket API Gateway." | |
ConnectRoute: | |
Type: AWS::ApiGatewayV2::Route | |
Properties: | |
ApiId: !Ref WebSocketMock | |
RouteKey: $connect | |
RouteResponseSelectionExpression: '$default' | |
AuthorizationType: NONE | |
ApiKeyRequired: false | |
OperationName: ConnectRoute | |
Target: !Join | |
- '/' | |
- - 'integrations' | |
- !Ref ConnectInteg | |
ConnectInteg: | |
Type: AWS::ApiGatewayV2::Integration | |
Properties: | |
ApiId: !Ref WebSocketMock | |
Description: Connect Integration | |
IntegrationType: MOCK | |
RequestTemplates: | |
"200" : '{"statusCode" : 200}' | |
TemplateSelectionExpression: '200' | |
PassthroughBehavior: 'WHEN_NO_MATCH' | |
ConnectIntegResp: | |
Type: AWS::ApiGatewayV2::IntegrationResponse | |
Properties: | |
ApiId: !Ref WebSocketMock | |
IntegrationId: !Ref ConnectInteg | |
IntegrationResponseKey: '$default' | |
ResponseTemplates: | |
"200" : '{"statusCode" : 200}' | |
ConnectRouteResponse: | |
Type: AWS::ApiGatewayV2::RouteResponse | |
Properties: | |
RouteId: !Ref ConnectRoute | |
ApiId: !Ref WebSocketMock | |
RouteResponseKey: $default | |
DisconnectRoute: | |
Type: AWS::ApiGatewayV2::Route | |
Properties: | |
ApiId: !Ref WebSocketMock | |
RouteKey: $disconnect | |
RouteResponseSelectionExpression: '$default' | |
AuthorizationType: NONE | |
OperationName: DisconnectRoute | |
Target: !Join | |
- '/' | |
- - 'integrations' | |
- !Ref DisconnectInteg | |
DisconnectInteg: | |
Type: AWS::ApiGatewayV2::Integration | |
Properties: | |
ApiId: !Ref WebSocketMock | |
Description: Disconnect Integration | |
IntegrationType: MOCK | |
RequestTemplates: | |
"200" : '{"statusCode" : 200}' | |
TemplateSelectionExpression: '200' | |
PassthroughBehavior: 'WHEN_NO_MATCH' | |
DisconnectIntegResp: | |
Type: AWS::ApiGatewayV2::IntegrationResponse | |
Properties: | |
ApiId: !Ref WebSocketMock | |
IntegrationId: !Ref DisconnectInteg | |
IntegrationResponseKey: '$default' | |
ResponseTemplates: | |
"200" : '{"statusCode" : 200}' | |
DisconnectRouteResponse: | |
Type: AWS::ApiGatewayV2::RouteResponse | |
Properties: | |
RouteId: !Ref DisconnectRoute | |
ApiId: !Ref WebSocketMock | |
RouteResponseKey: $default | |
DefaultRoute: | |
Type: AWS::ApiGatewayV2::Route | |
Properties: | |
ApiId: !Ref WebSocketMock | |
RouteKey: $default | |
RouteResponseSelectionExpression: '$default' | |
AuthorizationType: NONE | |
OperationName: DefaultRoute | |
Target: !Join | |
- '/' | |
- - 'integrations' | |
- !Ref DefaultInteg | |
DefaultInteg: | |
Type: AWS::ApiGatewayV2::Integration | |
Properties: | |
ApiId: !Ref WebSocketMock | |
Description: Default Integration | |
IntegrationType: MOCK | |
RequestTemplates: | |
"200" : '{"statusCode" : 200}' | |
TemplateSelectionExpression: '200' | |
DefaultIntegResp: | |
Type: AWS::ApiGatewayV2::IntegrationResponse | |
Properties: | |
ApiId: !Ref WebSocketMock | |
IntegrationId: !Ref DefaultInteg | |
IntegrationResponseKey: $default | |
ResponseTemplates: | |
"200" : '{"statusCode" : 200, "connectionId" : "$context.connectionId"}' | |
TemplateSelectionExpression: '200' | |
DefaultRouteResponse: | |
Type: AWS::ApiGatewayV2::RouteResponse | |
Properties: | |
RouteId: !Ref DefaultRoute | |
ApiId: !Ref WebSocketMock | |
RouteResponseKey: $default | |
Deployment: | |
Type: AWS::ApiGatewayV2::Deployment | |
DependsOn: | |
- ConnectRoute | |
- DisconnectRoute | |
- DefaultRoute | |
Properties: | |
ApiId: !Ref WebSocketMock | |
Stage: | |
Type: AWS::ApiGatewayV2::Stage | |
Properties: | |
StageName: LATEST | |
Description: One and only Stage | |
DeploymentId: !Ref Deployment | |
ApiId: !Ref WebSocketMock | |
DefaultRouteSettings: | |
DetailedMetricsEnabled: true | |
LoggingLevel: INFO | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment