DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Changing the AWS API Gateway Response Status Code in SAM

Changing the AWS API Gateway Response Status Code in SAM

See what happens when you change the AWS API Gateway response status code in SAM.

Rob Allen user avatar by
Rob Allen
·
May. 28, 19 · Tutorial
Like (1)
Save
Tweet
Share
10.24K Views

Join the DZone community and get the full member experience.

Join For Free

When creating an API with AWS Lambda and API Gateway, I discovered that a client request to a given resource with a verb that wasn't supported resulted in an unexpected response.

You can see this from this curl command to the /test resource which is only defined for GET:

$ curl -i -X PUT https://mh5rwr9q25.execute-api.eu-west-2.amazonaws.com/prod/test
HTTP/2 403
content-type: application/json
content-length: 42

{"message":"Missing Authentication Token"}


Given that I can GET the /Prod/hello resource, I would not expect to see 405 Method Not Allowed for PUT, and 403 Forbidden is a bit of a head-scratcher.

One way to handle this is to customize the Gateway Response. Gateway Responses are the set responses that API Gateway will return when it can't processing an incoming request. There are quite a few responses, and the one we want is MISSING_AUTHENTICATION_TOKEN.

What we are going to do is create an AWS::Serverless::Api resource in our template.yaml, which sets a different status code and response for the MISSING_AUTHENTICATION_TOKEN response. This is a new ability of SAM version 1.11.0, so make sure you have at least that version.

Our template looks like this:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'Test API'

Resources:
    MyApiName:
        Type: 'AWS::Serverless::Api'
        Properties:
            StageName: prod
            GatewayResponses:
                MISSING_AUTHENTICATION_TOKEN:
                    StatusCode: 405
                    ResponseTemplates:
                        "application/json": '{ "message": "Method Not Allowed" }'

    MyFunction:
        Type: AWS::Serverless::Function
        Properties:
            FunctionName: 'myapiname-test'
            Description: ''
            CodeUri: .
            Handler: index.php
            Timeout: 10
            MemorySize: 256
            Runtime: provided
            Layers:
                - 'arn:aws:lambda:eu-west-2:209497400698:layer:php-73:4'
            Events:
                HttpRoot:
                    Type: Api
                    Properties:
                        RestApiId: !Ref "MyApiName"
                        Path: /test
                        Method: GET

# This lets us retrieve the app's URL in the "Outputs" tab in CloudFormation
Outputs:
    MyApiName:
        Description: 'API URL in the Prod environment'
        Value: !Sub 'https://${MyApiName}.execute-api.${AWS::Region}.amazonaws.com/prod/'


Once we've deployed, a PUT request to our endpoint now returns the expected response:

$ curl -i -X PUT https://mh5rwr9q25.execute-api.eu-west-2.amazonaws.com/prod/test
HTTP/2 405
content-type: application/json
content-length: 35

{ "message": "Method Not Allowed" }


Warning

I should note that this solution isn't a panacea and introduces another problem. If you try to access an endpoint that doesn't exist, you also get a 405 back rather than the expected 404. Of course, before this change, you got a 403, so it's wrong regardless. More work here is definitely needed.

API AWS Sam (text editor)

Published at DZone with permission of Rob Allen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Chaos Engineering Tutorial: Comprehensive Guide With Best Practices
  • How To Choose the Right Streaming Database
  • Solving the Kubernetes Security Puzzle
  • A Gentle Introduction to Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: