📖 Scenario: You are building a simple API using AWS API Gateway. Your API will receive requests and send responses. To control how data is passed between the client and backend, you need to create request and response mapping templates.This helps transform the data format so your backend gets exactly what it expects, and your client receives the right response.
🎯 Goal: Create an AWS API Gateway method with request and response mapping templates that transform JSON data between client and backend.
📋 What You'll Learn
Create a REST API resource called /items
Add a POST method to /items
Configure a request mapping template to extract itemName and itemQuantity from the incoming JSON body and pass them as JSON to the backend
Configure a response mapping template to transform the backend JSON response to include a message and statusCode
Use valid AWS API Gateway mapping template syntax
💡 Why This Matters
🌍 Real World
API Gateway request and response mapping is used to control data flow between clients and backend services, ensuring data formats match expectations.
💼 Career
Understanding request and response mapping is essential for cloud architects and developers building serverless APIs on AWS.
Progress0 / 4 steps
1
Create the API resource and POST method
Create a REST API resource called /items and add a POST method to it in AWS API Gateway.
AWS
Hint
Use AWS CloudFormation syntax to define the resource and method.
2
Add request mapping template
Add a request mapping template to the POST method integration that extracts itemName and itemQuantity from the incoming JSON body and passes them as JSON to the backend.
AWS
Hint
Use $input.path to extract JSON fields in the mapping template.
3
Add response mapping template
Add a response mapping template to the POST method integration that transforms the backend JSON response to include a message and statusCode fields.
AWS
Hint
Use IntegrationResponses and ResponseTemplates to define the response mapping.
4
Complete the method response configuration
Add the method response configuration for status code 200 to complete the POST method setup.
AWS
Hint
MethodResponses define what the client receives. Use status code 200 and response model "Empty".
Practice
(1/5)
1. What is the main purpose of request and response mapping in AWS API Gateway?
easy
A. To encrypt data during transmission
B. To change data formats between client and backend
C. To monitor user activity on the website
D. To store data permanently in the cloud
Solution
Step 1: Understand the role of mapping templates
Mapping templates in API Gateway transform data formats between client requests and backend responses.
Step 2: Identify the main purpose
This transformation allows different systems to communicate smoothly by renaming, adding, or removing fields.
Final Answer:
To change data formats between client and backend -> Option B
Quick Check:
Mapping templates = data format change [OK]
Hint: Mapping templates transform data formats between client and backend [OK]
Common Mistakes:
Confusing mapping with data storage
Thinking mapping encrypts data
Assuming mapping monitors activity
2. Which of the following is the correct syntax to access a JSON field named userId in a request mapping template?
easy
A. $input.path('$.userId')
B. $input.json('userId')
C. $input.get('userId')
D. $input.body('userId')
Solution
Step 1: Recall JSON path syntax in mapping templates
The correct way to access a JSON field is using $input.path('$.fieldName').
Step 2: Match the syntax to the field userId
Using $input.path('$.userId') correctly accesses the userId field.
Final Answer:
$input.path('$.userId') -> Option A
Quick Check:
JSON field access = $input.path('$.field') [OK]
Hint: Use $input.path('$.fieldName') to access JSON fields [OK]
5. You want to add a new field requestTime with the current timestamp to every request sent to your backend using request mapping. Which mapping template snippet correctly adds this field?
hard
A. {
"user": "$input.path('$.user')",
"requestTime": "$context.stage"
}
B. {
"user": "$input.path('$.user')",
"requestTime": "$input.requestTime"
}
C. {
"user": "$input.path('$.user')",
"requestTime": "$stageVariables.requestTime"
}
D. {
"user": "$input.path('$.user')",
"requestTime": "$context.requestTime"
}
Solution
Step 1: Identify how to get current request time
In API Gateway mapping templates, $context.requestTime provides the current request timestamp.
Step 2: Check each option for correct usage
{
"user": "$input.path('$.user')",
"requestTime": "$context.requestTime"
} uses $context.requestTime correctly; others use invalid or unrelated variables.
Final Answer:
{
"user": "$input.path('$.user')",
"requestTime": "$context.requestTime"
} -> Option D
Quick Check:
Use $context.requestTime for current timestamp [OK]
Hint: Use $context.requestTime to add current timestamp in mapping [OK]
Common Mistakes:
Using $input.requestTime which doesn't exist
Confusing stage variables with request time
Using unrelated context variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.