Lab complete!
Now that you have completed this lab, make sure to update your Well-Architected review if you have implemented these changes in your workload.
Click here to access the Well-Architected Tool
We are now going to manually install the CloudWatch agent to start collecting memory data, to start let’s go back to the Amazon EC2 Dashboard.
On the left bar, click on Instances and select the EC2 Instance with the CloudWatchAgentServerRole IAM role.
Connect into the EC2 Instance using the browser-based SSH connection tool.
Download the Amazon Cloudwatch agent package, the instructions below are for Amazon Linux, for other OS please check here
wget https://s3.amazonaws.com/amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip
unzip AmazonCloudWatchAgent.zip
sudo ./install.sh
Before running the CloudWatch agent on any servers, you must create a CloudWatch agent configuration file, which is a JSON file that specifies the metrics and logs that the agent is to collect, including custom metrics. You can create it by using the wizard or by writting it yourself from scratch. Any time you change the agent configuration file, you must then restart the agent to have the changes take effect.
The wizard can autodetect the credentials and AWS Region to use if you have the AWS credentials and configuration files in place. For more information about these files, see Configuration and Credential Files in the AWS Systems Manager User Guide and the AWS documentation page .
For now, let’s start the CloudWatch agent configuration file wizard executing the command below at the selected EC2 instance.
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
For this lab we want to keep the following structure:
CloudWatch Agent Configutation File Wizard | Parameter |
---|---|
On which OS are you planning to use the agent? | 1. Linux |
Are you using EC2 or On-Premises hosts? | 1. EC2 |
Which user are you planning to run the agent? | 2. cwagent |
Do you want to turn on StatsD daemon? | 2. No |
Do you want to monitor metrics from CollectD? | 2. No |
Do you want to monitor any host metrics? | 1. Yes |
Do you want to monitor cpu metrics per core? | 2. No |
Do you want to add ec2 dimensions? | 1. Yes |
Would you like to collect your metrics at high resolution? | 4. 60s |
Which default metrics config do you want? | 1. Basic |
Are you satisfied with the above config? | 1. Yes |
Do you have any existing CloudWatch Log Agent? | 2. No |
Do you want to monitor any log files? | 2. No |
Do you want to store the config in the SSM parameter store? | 2. No |
The CloudWatch Agent config file should look like the following:
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "cwagent"
},
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}"
},
"metrics_collected": {
"disk": {
"measurement": [
"used_percent"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"mem": {
"measurement": [
"mem_used_percent"
],
"metrics_collection_interval": 60
}
}
}
}
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s
It may take up to 5 minutes for the metrics to become available, go back to the Amazon CloudWatch console page, under the Metrics session to validate that you are getting Memory information.
Click CWAgent:
Click ImageID,InstanceID,InstanceType:
Select the Instance from the list below:
You have now completed the CloudWatch agent installation and will be able to monitor on Amazon CloudWatch the memory utilization of that instance.
[BONUS] The next step is not mandatory to complete this lab.
If you have a lot of instances manually installing the CloudWatch agent in each of them is not a scalable option, instead consider using a pre-configured AWS CloudFormation template to automatically install the CloudWatch agent by default on all your stack. As an example on how to do that check the following steps:
Right-click and save link as: here to download the AWS Cloudformation template
Go to the AWS CloudFormation console
Click to Create Stack and select Upload a template file and point to the downloaded file
Enter a Stack Name and select a KeyName
Enter the tag Key: Event | Value: myStackforWACostLab
Click Next and Create stack
Now that you have completed this lab, make sure to update your Well-Architected review if you have implemented these changes in your workload.
Click here to access the Well-Architected Tool