Amazon API Gateway Mock Integration built with CloudFormation: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * API Gateway Operations")
 
 
(21 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[Amazon_API_Gateway_Operations#Examples|API Gateway Operations]]
* [[Amazon_API_Gateway_Operations#Examples|API Gateway Operations]]
* [[Amazon_API_Gateway_Deployment_with_CloudFormation#Examples|API Gateway Deployment with CloudFormation]]
=CloudFormation Stack=
elysium-stack.yaml:
<syntaxhighlight lang='yaml'>
AWSTemplateFormatVersion: '2010-09-09'
Description: |
  An API Gateway Stack.
Parameters:
  ProjectID:
    Type: String
    Default: elysium
  Bucket:
    Type: String
    Default: ovidiu-experiments
    Description: |
      The name of the S3 bucket that will contain the OpenAPI AWS metadata file during stack creation.
      The content of the file will provide the API metadata.
  OpenApiAwsS3Key:
    Type: String
    Default: elysium-openapi-aws.yaml
    Description: |
      The S3 key of the file that contains the OpenAPI AWS metadata. The S3 object is expected to be
      available in the ${Bucket} bucket.
Resources:
  AccessLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub ${ProjectID}-api-access-logs
      RetentionInDays: 1
  Api:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: !Ref AWS::StackName
      Description: |
        A manually deployed API, used for experiments. If you are reading this, it means that it can be  safely deleted.
      FailOnWarnings: true
      BodyS3Location:
        Bucket: !Ref Bucket
        Key: !Ref OpenApiAwsS3Key
  ApiDeployment:
    Type: AWS::ApiGateway::Deployment
    DependsOn: Api
    Properties:
      RestApiId: !Ref Api
      Description: |
        Deployment created as part of a CloudFormation stack.
  Stage:
    Type: AWS::ApiGateway::Stage
    DependsOn:
      - Api
      - ApiDeployment
    Properties:
      StageName: test
      RestApiId: !Ref Api
      DeploymentId: !Ref ApiDeployment
      MethodSettings:
        - ResourcePath: '/*'
          HttpMethod: '*'
          LoggingLevel: 'INFO'
      AccessLogSetting:
        DestinationArn: !GetAtt AccessLogGroup.Arn
        Format: >-
          {"requestId":"$context.requestId", "ip": "$context.identity.sourceIp",
          "caller":"$context.identity.caller",
          "user":"$context.identity.user","requestTime":"$context.requestTime",
          "eventType":"$context.eventType","routeKey":"$context.routeKey",
          "status":"$context.status","connectionId":"$context.connectionId"}
</syntaxhighlight>
=OpenAPI AWS Specification=
elysium-openapi-aws.yaml:
<syntaxhighlight lang='yaml'>
---
swagger: "2.0"
info:
  title: "elysium"
schemes:
- "https"
paths:
  /a:
    get:
      consumes:
      - "application/json"
      produces:
      - "application/json"
      responses:
        200:
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        requestTemplates:
          application/json: "{\"statusCode\": 200}"
        type: "mock"
definitions:
  Empty:
    type: "object"
    title: "Empty Schema"
</syntaxhighlight>
=Procedure=
* Save the content of [[#CloudFormation_Stack|CloudFormation stack]] in a file named elysium-stack.yaml.
* Save the [[#OpenAPI_AWS_Specification|OpenAPI specification]] into a file named elysium-openapi-aws.yaml
* Create an S3 bucket or use one that already exists:
aws s3 mb s3://ovidiu-experiments
* Copy the OpenAPI AWS metadata file in the S3 bucket:
aws s3 cp ./elysium-openapi-aws.yaml s3://ovidiu-experiments
* Deploy the stack:
aws cloudformation create-stack --stack-name elysium --template-body file://./elysium-stack.yaml
If everything goes as planned, a new API, API Deployment and "test" Stage will become available. The API can be tested with:
curl https://''api-id''.execute-api.us-west-2.amazonaws.com/test/a

Latest revision as of 05:49, 28 March 2019

Internal

CloudFormation Stack

elysium-stack.yaml:

AWSTemplateFormatVersion: '2010-09-09'

Description: |
  An API Gateway Stack.

Parameters:

  ProjectID:
    Type: String
    Default: elysium

  Bucket:
    Type: String
    Default: ovidiu-experiments
    Description: |
      The name of the S3 bucket that will contain the OpenAPI AWS metadata file during stack creation.
      The content of the file will provide the API metadata.

  OpenApiAwsS3Key:
    Type: String
    Default: elysium-openapi-aws.yaml
    Description: |
      The S3 key of the file that contains the OpenAPI AWS metadata. The S3 object is expected to be
      available in the ${Bucket} bucket.

Resources:

  AccessLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub ${ProjectID}-api-access-logs
      RetentionInDays: 1

  Api:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: !Ref AWS::StackName
      Description: |
        A manually deployed API, used for experiments. If you are reading this, it means that it can be  safely deleted.
      FailOnWarnings: true
      BodyS3Location:
        Bucket: !Ref Bucket
        Key: !Ref OpenApiAwsS3Key

  ApiDeployment:
    Type: AWS::ApiGateway::Deployment
    DependsOn: Api
    Properties:
      RestApiId: !Ref Api
      Description: |
        Deployment created as part of a CloudFormation stack.

  Stage:
    Type: AWS::ApiGateway::Stage
    DependsOn:
      - Api
      - ApiDeployment
    Properties:
      StageName: test
      RestApiId: !Ref Api
      DeploymentId: !Ref ApiDeployment
      MethodSettings:
        - ResourcePath: '/*'
          HttpMethod: '*'
          LoggingLevel: 'INFO'
      AccessLogSetting:
        DestinationArn: !GetAtt AccessLogGroup.Arn
        Format: >-
          {"requestId":"$context.requestId", "ip": "$context.identity.sourceIp",
          "caller":"$context.identity.caller",
          "user":"$context.identity.user","requestTime":"$context.requestTime",
          "eventType":"$context.eventType","routeKey":"$context.routeKey",
          "status":"$context.status","connectionId":"$context.connectionId"}

OpenAPI AWS Specification

elysium-openapi-aws.yaml:

---
swagger: "2.0"
info:
  title: "elysium"
schemes:
- "https"
paths:
  /a:
    get:
      consumes:
      - "application/json"
      produces:
      - "application/json"
      responses:
        200:
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        requestTemplates:
          application/json: "{\"statusCode\": 200}"
        type: "mock"
definitions:
  Empty:
    type: "object"
    title: "Empty Schema"

Procedure

aws s3 mb s3://ovidiu-experiments
  • Copy the OpenAPI AWS metadata file in the S3 bucket:
aws s3 cp ./elysium-openapi-aws.yaml s3://ovidiu-experiments
  • Deploy the stack:
aws cloudformation create-stack --stack-name elysium --template-body file://./elysium-stack.yaml

If everything goes as planned, a new API, API Deployment and "test" Stage will become available. The API can be tested with:

curl https://api-id.execute-api.us-west-2.amazonaws.com/test/a