Setting up your AWS API-Gateway logging

Edoardo Barp
2 min readMay 20, 2021

No more incomprehensible “Internal Server Error” and the likes of it

The API gateway of AWS is a wonderful piece of software to write serverless.. everything. In my case, it’s mostly REST-like APIs using lambda function in the backend, but this guide is agnostic of the output type.

When setting up and working with an API gateway, you will without a doubt end up with “Internal server errors” left and right. These errors can be extremely annoying and time consuming to debug. By default, the gateway doesn’t have logging enabled. Please do yourself a favour and enabled it (at least do so for the development version, you will save yourself some headaches).

To enable it, we first need to create a logging destination in CloudWatch. Go to CloudWatch > Log groups, and click “Create log group”.

Give it a name, and then get the ARN, we will need it in a second.

Got back to the API gateway page > logging. Enable logging, and when prompted for the ARN, paste the one we just created.

When creating (or editing) the monitoring, please do yourself a favour and do not leave the terrible template provided by AWS, which will leave you none the wiser. Instead, go have a look at the context variables, and add the ones that you’re interested in. If you’re working with integrations, I think the ones you must include at the least are `$context.error.message`, and `$context.integration.error`.

Check the context variables, and then pick and choose in the format.

Be careful, the logging is stage dependent, so you must enable it and modify for every stage! and don’t forget to deploy it afterwards.

Enable logging for all the required stages, and deploy!

Hope this guide will have been useful, it would’ve saved me countless hours if I had known it a few months back!

--

--

Edoardo Barp

Physicist with an engineering mind — love to write about my experiences — Creator of https://localassistant.ai: openrouter.ai chat interface