AWS - Mapping Template in API Gateway with VTL
- Anh Nguyen tuan
- Mar 8, 2023
- 1 min read
Use Case:
When API Gateway integrates with AWS Service (like SQS) and need to transform data by using Request & Response Template.
AWS Console:
Go to the menu 'Resources' of an API in the service 'API Gateway'
Select a method. Click to the link 'Integration Request'
Go to Mapping Templates section, click a content type in the list of Content-Type. You will see the section of Request Template.
Notes:
AWS API Gateway only supports some functions (VTL tools), not fully support like AppSync.
Function $utils in API Gateway: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
Function $utils in AppSync: https://docs.aws.amazon.com/appsync/latest/devguide/utility-helpers-in-util.html
Using $input.path( ) to get Object instead of $input.json( ).
For example, API gateway integrates with SQS:
requestTemplates:
application/json: |-
Action=SendMessage&MessageBody={"field1":$util.urlEncode($input.json('$.field1'))}Tricks:
Convert String to Integer or Long by this snippet
## Convert String to Integer
#set($Val=0)
$Val.parseInt('123')
## Convert String to Long
#set($Val=100000000000000)
$Val.parseLong('123')LInk Reference: https://aws.amazon.com/blogs/compute/using-api-gateway-mapping-templates-to-handle-changes-in-your-back-end-apis/





Comments