Install custom metrics CWAgent to improve AWS cloud monitoring.

  1. Go to the AWS Systems Manager Parameter Store **page to create a parameter and click the Create parameter button.

    linux 11.png

  2. Set AmazonCloudWatch-linux as name, select level Default and type as String.

    linux 12.png

  3. Enter the JSON content described below under Value. Finally, click on Create parameter.

    {
      "metrics":{
        "append_dimensions":{
          "AutoScalingGroupName":"${aws:AutoScalingGroupName}",
          "ImageId":"${aws:ImageId}",
          "InstanceId":"${aws:InstanceId}",
          "InstanceType":"${aws:InstanceType}"
        },
        "metrics_collected":{
          "disk":{
            "measurement":[
              "disk_total",
              "disk_used"
            ],
            "metrics_collection_interval":300,
            "ignore_file_system_types":[
              "tmpfs",
              "devtmpfs",
              "rootfs",
              "sysfs"
            ],
            "resources":[
              "*"
            ]
          },
          "mem":{
            "measurement":[
              "mem_total",
              "mem_used"
            ],
            "metrics_collection_interval":300
          },
          "swap":{
            "measurement":[
              "swap_used",
              "swap_used_percent"
            ],
            "metrics_collection_interval":300
          }
        }
      }
    }
    

    linux 13.png

  4. Once it is created, the parameter will appear on the My parameters screen.

    linux 14.png

  5. In order for CWAgent to send data to CloudWatch, you must create a role and include two permissions: AmazonEC2RoleforSSM and CloudWatchAgentServerPolicy, through AWS Managed policies in the IAM Role for EC2 instances. This IAM role must be attached to EC2 instances. If you already have an IAM role in use for EC2, you only need to add the AmazonEC2RoleforSSM and CloudWatchAgentServerPolicy permissions. Go to the Roles page to proceed with the tutorial:

    linux 15.png

  6. On the Create role screen, select AWS Service and EC2 as Use case. Then click Next.

    linux 16.png

  7. Select the AmazonEC2RoleforSSM policy and click Next.

    linux 17.png

  8. On the Review screen, verify that the correct policies are being applied and click Create role.

    linux 18.png

  9. You must install CWAgent on all the machines that you want to collect the metrics from. For this, it is possible to carry out the process using the individual access of each of the machines or automate this step by installing the bash script in the DevOps routine.

  10. Follow the script below to perform the installation:

    #!/bin/bash
    
    cd /home/ubuntu
    wgethttps://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_amd64/amazon-ssm-agent.deb
    sudo dpkg -i amazon-ssm-agent.deb
    sudo rm -rf amazon-ssm-agent.deb
    
    mkdir cwAgentPackage
    cd cwAgentPackage
    wgethttps://s3.amazonaws.com/amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip
    sudo apt-get install -y --force-yes unzip
    unzip AmazonCloudWatchAgent.zip
    sudo chmod +x install.sh
    sudo ./install.sh
    cd ..
    sudo rm -rf cwAgentPackage
    
    ## JSON file stored via ParameterStore called AmazonCloudWatch-linux
    sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c ssm:AmazonCloudWatch-linux -s
    sudo service amazon-ssm-agent restart
    

🎉 Now, the agent sends the metrics to CloudWatch, which will be collected by CleanCloud and made available within 12 hours to be used in dashboards and insights.

<aside> ❓ Are you in doubt? Access the links below:

****What is CloudWatch? What is SSM? What is CWAgent?

</aside>