Use a mapping template to override an API's request and response parameters and status codes
Mapping template overrides cannot be used with proxy integration endpoints, which lack data mappings
- Models are only for representing input and output formats
- Mapping templates for transforming data
You can enable the logging in API gateway by following the steps:
- Create an IAM with
arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs
permissions attached and following trust policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
- Set this IAM role in the
- In the API Gateway console, on the APIs pane, choose the name of an API that you created.
- In the left navigation pane, at the bottom, choose Settings.
- Under Settings, for CloudWatch log role ARN, paste the IAM role ARN that you copied.
- Choose Save.
Now there are two methods for logging the IP address
- Go to your AWS API Gateway instance within the AWS Console. Select Stages on the left menu and then select the Logs/Tracing tab Toggle on
Enable CloudWatch Logs
and select Log Level
as INFO
and then
You can enable the mapping template by going 
which results in the access logs like:

On top of this you can see the ip address in the Method request headers
as well
(b1fe0021-8064-4be8-a548-203c6fd795a6) Method request headers:
{accept-language=en-us, User-Agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15,
X-Forwarded-Proto=https,
X-Forwarded-For=8.xx.xx.xx,
Host=xxxxx.execute-api.eu-central-1.amazonaws.com,
X-Forwarded-Port=443, accept-encoding=gzip, deflate, br,
X-Amzn-Trace-Id=Root=1-602e74b0-439e58b379ac537c27664a34, accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8}
If you don't enable the CloudWatch
Loggin this information for the IP
address via the mapping template will be passed to the endpoint as well.
Like descirbed in this answer, for example lambda.
'use strict';
console.log('Loading function');
exports.handler = (event, context, callback) => {
console.log('SourceIP =', event.identity.sourceIP);
callback(null, event.identity.sourceIP);
};
OR
{
"ip": "$context.identity.sourceIp",
"apiId": "$context.apiId",
"requestId": "$context.requestId",
"requestTime": "$context.requestTime",
"protocol": "$context.protocol",
...
...
}
- Click Save Changes and that’s it! API logs should start showing up after a few minutes.

How do I enable CloudWatch Logs for troubleshooting my API Gateway REST API or WebSocket API?