100 AWS Projects

AWS Projects to get you started

May 21, 2018

API Gateway using Swagger and SAM


This example combines AWS SAM and Swagger Notation.Review the below 2 files given below and understand different aspects.Here are some things about this example.

  • When deployed , the swagger file here serves to create the API in the API Gateway.
  • The template file maps the api gateway API’s to the node js methods.


Swagger file to create the API’s in the API Gateway.

The x-amazon-apigateway-integration helps point to the IAM credetials that will be used for integrating the API with the AWS Lambda function code. We are using the aws_proxy type integration in our example.

Replace 374525342871 with your ARN in the petstore_v1.yaml

        credentials: arn:aws:iam::374525342871:role/apigAwsProxyRole
            statusCode: 200
          Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPetFunction.Arn}/invocations"
        passthroughBehavior: when_no_match
        httpMethod: POST
        type: aws_proxy


The below yaml snippet helps to setup the CORS and include the the swagger file in the template.yaml

 Cors: "'http://localhost:4200'"
            Name: 'AWS::Include'
            # Replace <bucket> with your bucket name
              Location: "s3://xdr56yhn-aws-sam/petstore_v2.yaml"


Use the below informaton to package and run the application.

  • create a new bucket like xdr56yhn-aws-sam in your account.
  • Copy the petstore_v2.yaml into the xdr56yhn-aws-sam s3 bucket.
  • sam package --template-file template_v2.yaml --s3-bucket xdr56yhn-aws-sam --output-template-file packaged_v2.yaml
  • sam deploy --template-file ./packaged_v2.yaml --stack-name PetStack --capabilities CAPABILITY_IAM