Getting Started with AWS CloudWatch Logging for a Node.js Application

When developing an application, logging is a crucial part as it allows developers to gain insights into what is happening in the app and detect any potential issues early on. AWS CloudWatch is a powerful tool that provides these logging capabilities. This guide will walk you through the process of setting up CloudWatch and seeing information being logged from a Node.js application.

Prerequisites

Before you begin, ensure you have the following prerequisites in place:

  1. 1.An AWS account: You must have an active AWS account to access and use CloudWatch.

  2. 2. Node.js and npm: Install Node.js and npm on your local development machine.

  3. 3. AWS SDK for JavaScript: Install the AWS SDK for JavaScript to interact with AWS services programmatically.

  4. IAM User: Create an IAM user with appropriate permissions to access CloudWatch and other relevant AWS services.

Step 1: Integrate AWS SDK into the Node.js application

1. In your Node.js project directory, install the AWS SDK for JavaScript using npm:

Install AWS SDK for JavaScript

2. Require the AWS SDK in your application:

Require AWS SDK

3. Set up AWS credentials. You can provide them directly in your code, but it's recommended to use environment variables or an external configuration file. For example:

Set up AWS credentials

Step 2: Set up IAM Permissions

Create an IAM policy granting the necessary permissions to create and view CloudWatch Logs. Make sure the following policy is attached to the IAM user associated with your application. This can be done in the AWS IAM service:

IAM policy permissions in CloudWatch logs

Step 3: Integrate CloudWatch Logging into the application

1. Create an instance of the CloudWatch Logs client:

CloudWatch log client

2. Define a function to create the log group and stream. You can also create these resources in the AWS CloudWatch console, but for this example we will create them within the application. Note that this route will require you to check for the existence of the resources first before creating them:

AWS CloudWatch console

3. Define a function to send application logs to CloudWatch:

Define a function to send application logs to CloudWatch

4. With the CloudWatch functions in place, its time to create an application which will utilize these functions and send logs to CloudWatch. The application we are building will call the PokeAPI to query a random pokemon every 2 minutes and log the results to CloudWatch. We will also include a 30% chance for the query to fail to simulate error logging. Below you’ll find the entire code of the application and all the CloudWatch parts put together:

Code of application and CloudWatch parts
Code of application and CloudWatch parts

Step 4: View the logs in the CloudWatch console

  1. Log in to your AWS Management Console.

  2. Navigate to the CloudWatch service from the list of available services.

  3. In the CloudWatch dashboard, click on "Logs" in the left navigation pane.

  4. Select your log group (e.g., /pokemon) from the list of log groups.

  5. You can now see the logs in the log stream you defined (e.g., results). Click on it to view log events.

CloudWatch Log Stream

Conclusion

Integrating CloudWatch Logging into your Node.js application empowers you to manage and analyze your logs effectively within the AWS ecosystem. By following the steps outlined in this guide, you can easily set up logging to CloudWatch, allowing you to monitor your application's behavior, troubleshoot issues, and gain valuable insights into its performance. The example provided in this guide only scratches the surface of what you can achieve with the service. You can easily build upon this example to match any specific needs.  Now, you're equipped with a powerful logging solution to enhance the observability of your Node.js applications.

Next Steps

We hope you found this article to be both informative and helpful in the integration of CloudWatch Logging. If you have any questions regarding your cloud solution, or would like PMsquare’s guidance in your cloud integration, contact us today.

Steven Nguyen1 Comment