API Logging

Understand the important concepts related to API logging with API Gateway and CloudWatch.

Importance of logging

Comments and logs are a critical part of any application development. Unfortunately, most coders feel it’s an unnecessary overhead. Still, to help whoever maintains the code, you should invest every effort to ensure that the comments are accurate and intuitive.

That’s sufficient when we’re working on monoliths. When we come to the cloud and the serverless world, though, we need a lot more. For example, application logs get scattered when we have multiple independent API requests. It takes a rock-solid framework to collate the logs for each of these into one single repository.

No marks for guessing. We’re talking about the AWS CloudWatch service, the single service that manages all logs for all services in AWS. The API Gateway can connect with CloudWatch to enable detailed records of every API invocation.

Much caution is necessary here. Logging is essential for any application, but we must remember that too many logs can be an unwanted load on the system. When we work on the cloud, such a load translates into high costs because we have to pay for each byte of data we store.

We must ensure that we use the optimal log settings. As we see, API Gateway allows granular control on what should log and what shouldn’t. We should use this feature with due consideration and evaluate the information we require in the logs.

Now, let’s look at how we can configure the API Gateway for logging.

Connecting to CloudWatch

Before we start using API Gateway logs, we need some initial configuration. First, open the IAM console in another tab to create a new role for the API Gateway.

Note: You must give ApiGatewayLoggingRole name to the CloudWatch role. This role is required in some of the next lessons, if you find an error during stack creation that refers to the CloudWatch log role, make sure to create this role and provide its ARN in the API's settings as shown below.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy