Using mapping template to over-ride status, status code and body returned from the Lambda function.
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"
Visit the GitHub repo for this pattern.
git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/apigw-mappingtemplate-lambda
sam deploy --guided --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM
sam delete
.