Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

Amazon API Gateway REST API with Mapping templates for status code and parameter mapping.

Created with SnapAPI GatewayAWS Lambda

Using mapping template to over-ride status, status code and body returned from the Lambda function.

This sample project demonstrates how to map status codes and over-ride parameters in a REST API - Lambda integration.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An example API using Mapping templates in Integration response along with Lambda function (uksb-1tthgi812) (tag:apigw-mappingtemplate-lambda)

Resources:
  RestApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
      DefinitionBody: # an OpenApi definition
        'Fn::Transform':
          Name: 'AWS::Include'
          Parameters:
            Location: './api.yaml'
      OpenApiVersion: 3.0.3
      EndpointConfiguration:
          Type: REGIONAL

  # Lambda function
  APIFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/
      Handler: lambda_function4.lambda_handler
      Runtime: python3.9
      Events:
        ApiEvent:
          Type: Api
          Properties:
            RestApiId: !Ref RestApi
            Path: /
            Method: any


Outputs:
  API:
    Description: Invoke URL to test mapping template
    Value: !Sub "https://${RestApi}.execute-api.${AWS::Region}.amazonaws.com/dev"

< Back to all patterns


GitHub icon Visit the GitHub repo for this pattern.

Download

git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/apigw-mappingtemplate-lambda

Deploy

sam deploy --guided --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

Delete the stack: sam delete.

Additional resources

D Surya Sai

Presented by D Surya Sai

A Serverless Cloud Engineer and technical writer, who works on building efficient and scalable solutions using AWS services.

Follow on LinkedIn