
This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.
Cloudwatch alarms can be used to do something, such as:
- Publish message to a Simple Notifcation Service (SNS) topic
- Reboot an EC2 instance
- Auto Scale to create/start additional EC2 instances or to stop/terminate EC2 instances
When some conditiion is met, such as:
- An EC2 instance has:
- high CPU usage
- high memory usage
- high network usage
- EC2 instance status check has failed
- An EC2 instance in a Load Balancer Target Group health check returns "unhealthy"
- An ElastiCache Redis Cache Cluster has a high number of connections
- a Lambda Function has one or more errors (this article)
- Your monthy AWS Bill is estimated to exceed x
Before creating an alarm, you will need:
- the name of your Lambda Function
Typically, you will want to be contacted somehow when an alert fires. For example, perhaps you want to get an email when an alert fires. An AWS Simple Notification Service (SNS) Topics can be setup so that you get an email when the alert fires. Check out my article on Creating a Simple Notification Service (SNS) Topics using the AWS CLI. And then users will subscribe to the Topic which can be done using the aws sns subscribe command.
~]$ aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic --protocol email --notification-endpoint john.doe@example.com
{
"SubscriptionArn": "pending confirmation"
}
The aws lambda list-functions command can be used to list your Lambda Functions. Something like this should be returned.
~]# aws lambda list-functions
{
"Functions": [
{
"FunctionName": "myfunction"
Now that you have the Amazon Resource Number (ARN) of one of your Simple Notification Service (SNS) Topics and the name of one of your Lambda Functions, you can use the aws cloudwatch put-metric-alarm command to create a new cloudwatch alarm. This will not do anything with the Lambda Function. This will just create an alarm.
- Period and Evaluation Periods and Datapoints to Alarm are used to set the Alarm State, such as In Alarm. In this example:
- --period 60 means that the Lambda Function be checked for Errors once every 60 seconds
- --datapoints-to-alarm 1 means there only needs to be a single occurrence where the Lambda Function is in an Error state for the alarm state to be set to In Alarm. This works in conjunction with Comparison Operator and Threshold. In this example, the combination of --comparison-operator GreaterThanThreshold and --threshold 1.0 means that the evaulation is True when the Lambda Function is in an Error state
- --evaulation-periods 1 means only the current occurence is evaluated to determine if the alarm state should be updated
- A message will be published to your Simple Notification Service (SNS) Topic if the Lambda Function is in an Error state or if the Alarm has INSUFFICIENT_DATA.
It is also noteworthy that if Datapoints to Alarm were 2 and Evaluation Periods were 10, then the alarm state would be set to In Alarm if the Lambda Function is in an Error state in 2 of the last 10 periods.
aws cloudwatch put-metric-alarm \
--alarm-name "Lambda Function myfunction Errors" \
--alarm-description "Lambda Function myfunction Errors" \
--namespace AWS/Lambda \
--metric-name Errors \
--comparison-operator GreaterThanThreshold \
--threshold 1.0 \
--statistic Sum \
--period 60 \
--evaluation-periods 1 \
--treat-missing-data missing \
--datapoints-to-alarm 1 \
--dimensions "Name=FunctionName,Value=myfunction" \
--alarm-actions arn:aws:sns:<region goes here, e.g. us-east-1>:<your AWS account ID goes here>:<the name of your topic>
The aws cloudwatch list-metrics --namespace=AWS/Lambda command can be used to return metrics.
aws cloudwatch list-metrics --namespace=AWS/Lambda
Something like this should be returned.
{
"Metrics": [
{
"Namespace": "AWS/Lambda",
"Dimensions": [
{
"Name": "FunctionName",
"Value": "myfunction"
},
{
"Name": "Resource",
"Value": "myfunction"
}
],
"MetricName": "ConcurrentExecutions"
}
]
}
Did you find this article helpful?
If so, consider buying me a coffee over at