This content is part of the Essential Guide: AWS Lambda architecture brings serverless to enterprise cloud

How can we automate with CloudWatch Events and Lambda?

We use Amazon CloudWatch to track cloud performance and create notifications for service metrics. How can we automate events to respond to the metrics?

The depth of the AWS platform makes it possible for developers, architects and DevOps engineers to automate many...

tasks. Combining Amazon CloudWatch Events with AWS Lambda can further extend the automation capabilities.

Amazon CloudWatch is a monitoring service that creates alarms and notifications for metrics such as high CPU use. CloudWatch Events goes one step further by providing a near real-time stream of events that occur within an AWS environment. CloudWatch Events enables IT teams to create rules that listen to and respond to specific events or operational changes.

When an Elastic Compute Cloud (EC2) instance launches, for example, it generates an event that says it's going into a pending state. If no errors occur, Amazon CloudWatch Events generates another event when it gets into the running state. Events can describe state changes for many services, including EC2 instances, API calls and Elastic Block Store volumes and snapshots. IT teams can subscribe to any number of events to perform tasks such as invoking custom code. Developers can create a custom event that fires on a schedule, that runs at a fixed interval or that is based on a CRON expression.

After creating an event, a developer can tie the event to a target, which is a resource that acts on event data. There are targets for Simple Notification Service, Simple Queue Service, Kinesis Streams and Lambda functions. For example, a developer can create a CloudWatch Events rule to schedule an event for 9 p.m. That event can trigger an existing Lambda function that contains the custom logic an application requires. Developers can assign AWS Identity and Access Management roles to Lambda functions; those functions have access to AWS software development kits, which enable nearly limitless possibilities. When the schedule runs the event at 9 p.m., the Lambda function triggers a deployment, runs tests and kicks off a backup -- in addition to other actions.

How Auto Scaling factors in

IT teams can also use event rules to tap into the Auto Scaling lifecycle; in other words, a rule can listen for Auto Scaling actions. So, when the demand for a web application increases suddenly, and it's time to scale out, the Auto Scaling event routes to a Lambda function target that automates any required task. For instance, the task could add a domain name system record for a new scaled instance to Amazon Route 53 or it could update the configuration for a NGINX server that should start routing requests to that new instance.

Auto Scaling lifecycle hooks are common events that occur from rules. Many IT teams create terminating lifecycle policies for EC2 instances so that, when demand for an app drops and resources should terminate, a CloudWatch Events rule triggers a Lambda function to upload log files from the instance into Simple Storage Service or to perform a decommission task before deleting the instance.

Next Steps

Monitor and track EC2 resources with CloudWatch

Weigh the pros and cons of AWS Lambda

Book excerpt: Master Amazon CloudWatch

Dig Deeper on AWS CloudWatch and application performance monitoring