Installing CloudWatch Agent

Install CloudWatch Agent

  1. Start the EC2 instance connection
  • Select Instances
  • Select the newly created instance
  • Copy Public IPv4 address
  • Use PuTTY or MobaXterm to connect via port 22
  • For detailed information about EC2 connection, please refer to Connecting Linux Virtual Machine

Attach IAM Role

  1. MobaXterm interface.

CloudWatchAgent

  • Select SSH

CloudWatchAgent

  • Enter ec2-user

CloudWatchAgent

  1. Since this is our first time accessing EC2 Instance we need to install the latest updates.

CloudWatchAgent

sudo yum update -y

CloudWatchAgent

  1. Connect to the EC2 Instance you just created and load the CloudWatch Agent package with the following command:
sudo wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm

CloudWatchAgent

  1. Install the downloaded agent package with the command:
sudo rpm -U ./amazon-cloudwatch-agent.rpm

CloudWatchAgent

  1. Create a script to send memory metrics to CloudWatch:
sudo vi /opt/aws/amazon-cloudwatch-agent/bin/config.json
  • Press the i key and copy the following JSON file into the script:
{
"logs": {
    "logs_collected": {
        "files": {
            "collect_list": [
                {
                    "file_path": "/var/log/httpd/access_log",
                    "log_group_name": "access_log"
                }
            ]
        }
    }
},
"metrics": {
    "metrics_collected": {
        "mem": {
            "measurement": [
                "mem_used_percent"
            ],
            "metrics_collection_interval": 30
        }
    }
}
}

CloudWatchAgent

  • Press the ESC key and then type :wq! to save the script.

CloudWatchAgent

  1. Run that script to send information to CloudWatch with the command:
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

CloudWatchAgent

  1. Check Connection to CloudWatch
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

CloudWatchAgent

  • If the status is running, then proceed to the next step
  • If the status is stopped, you need to start the Cloudwatch agent with the following command:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config. json

CloudWatchAgent

  1. Enter the following command to move to the directory containing the CloudWatch Agent log file:
cd /opt/aws/amazon-cloudwatch-agent/logs/

CloudWatchAgent

  1. Enter the following command to view the contents of the log file
cat amazon-cloudwatch-agent.log

CloudWatchAgent