AWS Compute Blog
Monitoring memory usage in Amazon Lightsail instance
This post is written by Sebastian Lee, Solution Architect, Startup Singapore.
Amazon Lightsail is a great starting point for those looking to get started on AWS. Lightsail is ideal for startups, SMBs, and hobbyist developers because it simplifies the deployment of instances, databases, load-balancers, CDNs, and even containers. However, you cannot track metrics beyond CPU utilization, network utilization, and error messages. Many startups and small businesses need to review more metrics like memory usage and disk usage.
In this blog, I walk through the steps to configure a Lightsail instance to send memory usage to Amazon CloudWatch for monitoring, alarming and notifications.
Product and Solution Overview
Amazon CloudWatch is a monitoring and observability service built for DevOps engineers, developers, site-reliability engineers and IT managers. CloudWatch collects monitoring and operational data in the form of logs, metrics, and events. It provides a unified view of your AWS resources, applications and services that run on AWS and on-premise servers. You can configure your Lightsail resources to work with Amazon CloudWatch to receive more metrics.
The following sections include steps to install a Cloudwatch agent on your Amazon Lightsail instance and configure it to have the necessary permission to send memory usage metrics to Amazon Cloudwatch.
Prerequisites
Before you begin the walkthrough, you must have an instance running in your Lightsail account. You can follow the steps here if you need help creating an instance.
Walkthrough
1. Create IAM user
First, you must create an IAM user to provide permission to send data to CloudWatch.
- Sign in to the AWS Management Console and open the IAM console.
- In the navigation pane, choose Users, and then choose Add user.
- Enter “lightsail-cloudwatch-agent” in the User name text box.
- For Access type, select Programmatic access, and then choose Next: Permissions.
- For Set permissions, choose Attach existing policies directly.
- In the list of policies, select the check box next to CloudWatchAgentServerPolicy. You can use the search text box to find the policy.
- Choose Next: Tags.
- Optionally, you can add one or more tag-key value pairs to organize, track, or control access for this role, and then choose Next: Review.
- Confirm that the correct policies are listed, and then choose Create user.
- In the row for the new user, choose Show. Copy the access key and secret key to a file so that you can use them when installing the agent.
- Important: You will not be able to copy the secret key after leaving this page. If you lose it, you will have to create a new one
- Choose Close.
Now that you created an IAM user, you can SSH into your Lightsail instance.
2. SSH into Amazon Lightsail instance
You can connect to your instance using the browser-based SSH client available in the Lightsail console, or by using your own SSH client with the SSH key of your instance.
Complete the following steps to connect to your instance using the browser-based SSH client in the Lightsail console:
- Open the Lightsail console.
- Click the terminal icon, next to the instance, as shown in the following screenshot.
3. Installing the CloudWatch agent
Now that you have SSH’d into your instance, you are ready to install the CloudWatch agent. The CloudWatch agent is available as a package on Amazon Linux 2 instances. For other operating systems, see Download and configure the CloudWatch agent using the command line.
Enter the following command to install the CloudWatch agent on a linux instance.
4. Setup credentials
Now that you installed the CloudWatch Agent, you must allow it to access your AWS resources. First, setup the necessary credentials.
Enter the following command to create a credentials profile in the AWS Command Line Interface (AWS CLI).
Follow the prompts to enter the access key ID and secret access key you copied in the preceding steps.
> sudo aws configure --profile AmazonCloudWatchAgent
Follow the prompts to enter the access key ID and secret access key you copied earlier in this tutorial
5. Create CloudWatch configuration file to collect memory usage metrics
To tell CloudWatch agent to collect memory usage metrics, you will need to create a CloudWatch config file.
Enter the following command to create a config file for the CloudWatch agent.
> sudo vim /opt/aws/amazon-cloudwatch-agent/bin/config.json
Press “I” to enter insert mode in Vim, and paste the following text into the file.
Press “ESC”, and then type “:wq!” to save the file and exit Vim.
6. Configure CloudWatch agent
In this section, you configure the CloudWatch agent to use the shared credential profile created earlier.
Enter the following command to create a common configuration file for the CloudWatch agent.
> sudo vim /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml
Press “I” to enter insert mode in Vim, and paste the following text into the file.
[credentials]
shared_credential_profile = "AmazonCloudWatchAgent"
Press “ESC”, and then type “:wq!” to save the file and exit Vim.
7. Start CloudWatch agent
Now the necessary configuration for CloudWatch agent is setup. Let’s start the agent.
Enter the following command to start the CloudWatch agent.
Enter the following command to verify that the CloudWatch agent is running.
8. Verify metrics in CloudWatch
At this point, you should be able to view your metrics in CloudWatch.
- Navigate to the CloudWatch console.
- On the left navigation panel, choose Metrics.
- Under “Custom Namespaces”, You should see a link for “CWAgent”.
- Choose CWAgent.
- Choose ImageId, InstanceId, InstanceType.
- Select checkbox to display metrics on graph.
In addition, you can create a CloudWatch alarm to monitor the memory usage metrics to automatically send you a notification when the metric reaches a threshold you specify. To create an alarm in CloudWatch, you can follow this guide.
Conclusion
In this blog, I covered how you can install the CloudWatch agent on your Amazon Lightsail instance to send memory metrics to Amazon CloudWatch. For more information on additional metrics and logs supported by CloudWatch Agent, see the CloudWatch User Guide
To get started with Amazon Lightsail, check out our getting started page for more tutorial and resources.