Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

AWS Cloud Watch

Create a custom Cloud Watch metric

To create your custom metric:


1. Log in to the instance through the AWS CLI.
2. Copy the following Bash script, and then save it to your instance (for
example, mem.sh).
This example script shows the values that you can publish in Cloud Watch. In this
example, we are using the put-metric-data API call to push the following values to Cloud
Watch: the percentage of used memory, the number of total and port 80 TCP connections,
the number of users currently logged in, and the percentage of I/O wait time.

3. After creating the Bash script, give execute permissions to the file.

4. Run the Bash script to check that it works.


Push your metric to Cloud Watch

1. Create a cron job:

$ crontab -e

2. Add this line to execute your script every minute:

*/1 * * * * /home/ec2-user/mem.sh

3. Save and exit.


When the crontab is saved, crontab: installing new crontab is displayed.
Monitor your EC2 instance
Find your custom metric in the CloudWatch console:
1. Sign in to the CloudWatch console.
2. Choose Metrics.
3. Choose the All Metrics view.
4. Choose Custom.
5. Choose the dimension Instance.
6. Select your custom metric by its InstanceId and Metric Name.
7. View the graph of your metric.
Other uses
You can use this example to build your own logic to process multiple dimensions
(memory, CPU, swap, and so on), and then push that metric data to CloudWatch.
For example, suppose that you benchmark your application. Then, you discover that
when the I/O wait time and percentage memory usage reach a certain threshold, the
system stops functioning properly. To address this problem, you monitor both values
simultaneously in a script, storing the logical AND of the values in a third variable that you
push to CloudWatch.

To compose an SNS message filtering graph with CloudWatch:

1. Open the CloudWatch console.


2. Choose Metrics, SNS, All Metrics, and Topic Metrics.
3. Select all metrics to add to the graph, such as:
o NumberOfMessagesPublished
o NumberOfNotificationsDelivered
o NumberOfNotificationsFilteredOut
4. Choose Graphed metrics.
5. In the Statistic column, switch from Average to Sum.
6. Title your graph with a descriptive name, such as “SNS Message Filtering”

The graph shows the number of messages published, delivered, and filtered out
within the timeframe you specify (1h, 3h, 12h, 1d, 3d, 1w, or custom).
To set alarms using the CloudWatch console:

1. Sign in to the AWS Management Console and open the CloudWatch console at
https://console.aws.amazon.com/cloudwatch/.
2. Choose Alarms, and then choose the Create Alarm button. This launches
the Create Alarm wizard.
3. Scroll through the Amazon SNS metrics to locate the metric you want to place an
alarm on. Select the metric to create an alarm on and choose Continue.
4. Fill in the Name, Description, Threshold, and Time values for the metric, and
then choose Continue.
5. Choose Alarm as the alarm state. If you want CloudWatch to send you an email
when the alarm state is reached, either select a preexisting Amazon SNS topic or
choose Create New Email Topic. If you choose Create New Email Topic, you
can set the name and email addresses for a new topic. This list will be saved and
appear in the drop-down box for future alarms. Choose Continue.
Note:

If you use Create New Email Topic to create a new Amazon SNS topic, the
email addresses must be verified before they will receive notifications. Emails are
sent only when the alarm enters an alarm state. If this alarm state change
happens before the email addresses are verified, they will not receive a
notification.

6. At this point, the Create Alarm wizard gives you a chance to review the alarm
you’re about to create. If you need to make any changes, you can use
the Edit links on the right. Once you are satisfied, choose Create Alarm.

Example:

Creating a CPU Usage Alarm That Sends Email

To create an alarm that sends email based on CPU usage

1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.


2. In the navigation pane, choose Alarms, Create Alarm.
3. Under EC2 Metrics, choose a metric category (for example, Per-Instance
Metrics).
4. Select a metric as follows:
a. Select a row with the instance and the CPUUtilization metric.
b. For the statistic, choose Average, choose one of the predefined
percentiles, or specify a custom percentile (for example,p95.45).
c. Choose a period (for example, 5 minutes).
d. Choose Next.
5. Define the alarm as follows:

a. Under Alarm Threshold, enter a unique name for the alarm (for
example, myHighCpuAlarm) and a description of the alarm (for
example, CPU usage exceeds 70 percent). Alarm names must contain
only ASCII characters.
b. Under Whenever, for is, choose > and enter 70. For for, enter 2. This
specifies that the alarm is triggered if the CPU usage is above 70 percent
for two consecutive sampling periods.
c. Under Actions, for Whenever this alarm, choose State is ALARM.
For Send notification to, select an existing SNS topic or create a new
one.

d. To create a new SNS topic, choose New list. For Send notification to, enter a
name for the SNS topic (for example, myHighCpuAlarm), and for Email list,
enter a comma-separated list of email addresses to be notified when the alarm
changes to the ALARM state. Each email address is sent a topic subscription
confirmation email. You must confirm the subscription before notifications can be
sent.
e. Choose Create Alarm.

You might also like