title: CloudWatch custom EC2 memory metric and alarm with AutoScaling policy
body: <p>My goal is to test AutoScaling based on memory used on my EC2 instances.</p>
  
  <p>To monitor my EC2 instance memory, I installed CloudWatch agent on my EC2 Instance and created and configured my CloudWatch agent configuration file, as explained here:
  <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent.html</a>
  <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file.html</a></p>
  
  <pre><code>{
          "metrics": {
                  "append_dimensions": {
                          "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
                          "ImageId": "${aws:ImageId}",
                          "InstanceId": "${aws:InstanceId}",
                          "InstanceType": "${aws:InstanceType}"
                  },
                  "metrics_collected": {
                          "cpu": {
                                  "measurement": [
                                          "cpu_usage_idle",
                                          "cpu_usage_iowait",
                                          "cpu_usage_user",
                                          "cpu_usage_system"
                                  ],
                                  "metrics_collection_interval": 60,
                                  "totalcpu": false
                          },
                          "disk": {
                                  "measurement": [
                                          "used_percent",
                                          "inodes_free"
                                  ],
                                  "metrics_collection_interval": 60,
                                  "resources": [
                                          "*"
                                  ]
                          },
                          "diskio": {
                                  "measurement": [
                                          "io_time"
                                  ],
                                  "metrics_collection_interval": 60,
                                  "resources": [
                                          "*"
                                  ]
                          },
                          "mem": {
                                  "measurement": [
                                          "mem_used_percent"
                                  ],
                                  "metrics_collection_interval": 60
                          },
                          "swap": {
                                  "measurement": [
                                          "swap_used_percent"
                                  ],
                                  "metrics_collection_interval": 60
                          }
                  }
          }
  }
  </code></pre>
  
  <p>Once I start the CloudWatch agent on the EC2 Instance, the custom metrics work ok and I can see them on CloudWatch.</p>
  
  <pre><code>sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c ssm:configuration-parameter-store-name -s
  </code></pre>
  
  <p>I created an AMI with this EC2 Instance and created an AutoScaling group with this AMI.
  I then set up an alarm based on the custom metric "mem_used_percent" (when >=50) and used it as an AutoScaling Group Policy (add 1 Instance). Image attached.</p>
  
  <p><a href="https://i.stack.imgur.com/CJHmH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CJHmH.png" alt="enter image description here"></a></p>
  
  <p>I connect to the EC2 instance and increase the memory used using "Stress". The alarm is raised, the AutoScaling policy is triggered and a new EC2 instance is created.</p>
  
  <p>The problem I see is that I am creating an alarm on an specific EC2 Instance, instead of the whole AutoScaling Group, and once the initial EC2 Instance is terminated, the alarm does not make sense any more.
  How could I configured the same idea but for the AutoScaling Group?</p>
  
  <p>Thanks.</p>