Is it possible to get the headers in HttpApi Gateway logs?
Howdy!
Is it possible to access and log specific header values directly from the HttpApi Gateway to Cloudwatch?
Im using Cloudflare for my DNS and am routing from Cloudflare to my HttpApi Gateway where I have Lambdas firing. My Gateway is logging the ip to Cloudwatch; however, this is the Cloudflare IP, not the original user IP which is a value sent along in the headers.
I am able to get the original users' ip in my Lambda by accessing event.headers['cf-connecting-ip']
, but I would like to have my HttpApi Gateway log this to Cloudwatch as well. I've reviewed the documentation and didn't see mention of accessing headers from the HttpApi Gateway.
Here's what my current log template is set up as for my Gateway
{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", " "requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","routeKey":"$context.routeKey", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength"}
Thanks!
It doesn't look like it is possible to log header in the API Gateway access log. From the documentation, only the following variables can be logged from API Gateway access logs to Cloudwatch - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-logging-variables.html
Having said that, it is possible to log the headers in the backend integration lambda.
I used this tutorial https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-dynamo-db.html and just modified the lambda function code to log the event object
const AWS = require("aws-sdk");
const dynamo = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event, context) => { let body; let statusCode = 200; const headers = { "Content-Type": "application/json" }; console.log("#####################") console.log(event) console.log("#####################") try { .....rest of the code
When I called the API from the API Gateway, I do see the headers getting logged in the Cloudwatch logs of the lambda function.
Relevant questions
Cross-account cross-region in cloudwatch for specific log group
asked 7 months agoIs it possible to pass headers in Nodejs Lambda API
asked 6 days agoAPI Gateway access log still get requests blocked by WAF
asked 3 months agoIs it possible to throw custom error message from API Gateway Lambda Authorizer
asked 2 months agoIs it possible to send json data in body of GET method defined in API Gateway REST API ?
Accepted Answerasked 4 months agoIs it possible to pass variables in a websocket $connect call?
Accepted Answerasked 2 years agoIs it possible to get the headers in HttpApi Gateway logs?
asked 11 days agoHttpApi timeout setting missing from SAM cloudformation template
Accepted Answerasked 2 years agoAPI Gateway not forwarding all access logs to Cloudwatch
asked 2 years agoIs it possible to get a single & sorted exported log data from CloudWatch Logs export task?
Accepted Answerasked 5 months ago