Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save veve90/54629c0fc735b79cbbd3a09acdcb1fc1 to your computer and use it in GitHub Desktop.
Save veve90/54629c0fc735b79cbbd3a09acdcb1fc1 to your computer and use it in GitHub Desktop.

Revisions

  1. @maatthc maatthc revised this gist Nov 12, 2019. 1 changed file with 34 additions and 4 deletions.
    38 changes: 34 additions & 4 deletions aws-websockets-api-gw-mock.yaml
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,40 @@
    AWSTemplateFormatVersion: '2010-09-09'
    Description: AWS CloudFormation template for WebSocket API Gateway Mock
    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: "WebSocket API Gateway"
    Description: "Mock WebSocket API Gateway."

    ConnectRoute:
    Type: AWS::ApiGatewayV2::Route
    @@ -82,7 +108,7 @@ Resources:
    IntegrationId: !Ref DisconnectInteg
    IntegrationResponseKey: '$default'
    ResponseTemplates:
    "application/json" : '{"statusCode" : 200}'
    "200" : '{"statusCode" : 200}'

    DisconnectRouteResponse:
    Type: AWS::ApiGatewayV2::RouteResponse
    @@ -146,4 +172,8 @@ Resources:
    StageName: LATEST
    Description: One and only Stage
    DeploymentId: !Ref Deployment
    ApiId: !Ref WebSocketMock
    ApiId: !Ref WebSocketMock
    DefaultRouteSettings:
    DetailedMetricsEnabled: true
    LoggingLevel: INFO

  2. @maatthc maatthc revised this gist Nov 11, 2019. 1 changed file with 0 additions and 7 deletions.
    7 changes: 0 additions & 7 deletions ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +0,0 @@
    # Basic WebSocket mock in AWS ApiGateway using CloudFormation in AWS

    ## Click here to deploy

    ![Launch Stack](https://s3.amazonaws.com/cloudformation-examples/cloudformation-launch-stack.png "Launch Stack")

    https://console.aws.amazon.com/cloudformation/home?#/stacks/new?stackName=WsApiGw&templateURL=https://gist.github.com/maatthc/9d2dfe0448733f0ee1624d658fbac80f/raw/d8360e59a9322ec53e0270bec08cbd978e6aaeb9/aws-websockets-api-gw-mock.yaml
  3. @maatthc maatthc revised this gist Nov 11, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,4 @@

    ![Launch Stack](https://s3.amazonaws.com/cloudformation-examples/cloudformation-launch-stack.png "Launch Stack")

    https://console.aws.amazon.com/cloudformation/home?#/stacks/new?stackName=WsApiGw&templateURL=
    https://console.aws.amazon.com/cloudformation/home?#/stacks/new?stackName=WsApiGw&templateURL=https://gist.github.com/maatthc/9d2dfe0448733f0ee1624d658fbac80f/raw/d8360e59a9322ec53e0270bec08cbd978e6aaeb9/aws-websockets-api-gw-mock.yaml
  4. @maatthc maatthc revised this gist Nov 11, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,4 @@

    ![Launch Stack](https://s3.amazonaws.com/cloudformation-examples/cloudformation-launch-stack.png "Launch Stack")

    https://console.aws.amazon.com/cloudformation/home?region=region#/stacks/new?stackName=stack_name&templateURL=template_location
    https://console.aws.amazon.com/cloudformation/home?#/stacks/new?stackName=WsApiGw&templateURL=
  5. @maatthc maatthc revised this gist Nov 11, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,6 @@

    ## Click here to deploy

    ![Launch Stack](https://s3.amazonaws.com/cloudformation-examples/cloudformation-launch-stack.png)
    ![Launch Stack](https://s3.amazonaws.com/cloudformation-examples/cloudformation-launch-stack.png "Launch Stack")

    https://console.aws.amazon.com/cloudformation/home?region=region#/stacks/new?stackName=stack_name&templateURL=template_location
  6. @maatthc maatthc revised this gist Nov 11, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Basic WebSocket mock in AWS ApiGateway using CloudFormation in AWS

    # Click here to deploy
    ## Click here to deploy

    (https://s3.amazonaws.com/cloudformation-examples/cloudformation-launch-stack.png)
    ![Launch Stack](https://s3.amazonaws.com/cloudformation-examples/cloudformation-launch-stack.png)

    https://console.aws.amazon.com/cloudformation/home?region=region#/stacks/new?stackName=stack_name&templateURL=template_location
  7. @maatthc maatthc revised this gist Nov 11, 2019. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,7 @@
    # Basic WebSocket mock in AWS ApiGateway using CloudFormation in AWS
    # Basic WebSocket mock in AWS ApiGateway using CloudFormation in AWS

    # Click here to deploy

    (https://s3.amazonaws.com/cloudformation-examples/cloudformation-launch-stack.png)

    https://console.aws.amazon.com/cloudformation/home?region=region#/stacks/new?stackName=stack_name&templateURL=template_location
  8. @maatthc maatthc revised this gist Nov 11, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    # Basic WebSocket mock in AWS ApiGateway using CloudFormation in AWS
  9. @maatthc maatthc created this gist Nov 11, 2019.
    149 changes: 149 additions & 0 deletions aws-websockets-api-gw-mock.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,149 @@
    AWSTemplateFormatVersion: '2010-09-09'
    Description: AWS CloudFormation template for WebSocket API Gateway Mock

    Resources:
    WebSocketMock:
    Type: AWS::ApiGatewayV2::Api
    Properties:
    Name: WebSocketMock
    ProtocolType: WEBSOCKET
    RouteSelectionExpression: "$request.body.message"
    Description: "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:
    "application/json" : '{"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