Welcome to CloudAffaire and this is Debjeet
In the last blog post, we have discussed CloudWatch Alarm. We have also created a CloudWatch Alarm on an EC2 instance memory utilization.
In this blog post, we are going to discuss CloudWatch Events. We are also going to configure CloudWatch Events on EC2 instance state change.
Amazon CloudWatch Events delivers a near real-time stream of system events that describe changes in Amazon Web Services (AWS) resources. Using simple rules that you can quickly set up, you can match events and route them to one or more target functions or streams. CloudWatch Events becomes aware of operational changes as they occur. CloudWatch Events responds to these operational changes and takes corrective action as necessary, by sending messages to respond to the environment, activating functions, making changes, and capturing state information. You can also use CloudWatch Events to schedule automated actions that self-trigger at certain times using cron or rate expressions.
Components of CloudWatch Events:
An event indicates a change in your AWS environment. AWS resources can generate events when their state changes. For example, Amazon EC2 generates an event when the state of an EC2 instance changes from pending to running, and Amazon EC2 Auto Scaling generates events when it launches or terminates instances. AWS CloudTrail publishes events when you make API calls. You can generate custom application-level events and publish them to CloudWatch Events. You can also set up scheduled events that are generated on a periodic basis. For a list of services that generate events, and sample events from each service, see CloudWatch Events Event Examples From Supported Services.
A target processes events. Targets can include Amazon EC2 instances, AWS Lambda functions, Kinesis streams, Amazon ECS tasks, Step Functions state machines, Amazon SNS topics, Amazon SQS queues, and built-in targets. A target receives events in JSON format.
A rule matches incoming events and routes them to targets for processing. A single rule can route to multiple targets, all of which are processed in parallel. Rules are not processed in a particular order. This enables different parts of an organization to look for and process the events that are of interest to them. A rule can customize the JSON sent to the target, bypassing only certain parts or by overwriting it with a constant.
Next, we are going to create a CloudWatch event on EC2 instance state change with SNS as a target.
Prerequisite for this demo:
- A running EC2 instance
- SNS topic with email subscription.
We have already an EC2 instance and SNS topic.
Step 1: Login to AWS console and navigate to CloudWatch.
Step 2: Click ‘Get started’ located under ‘Events’.
Step 3: Select ‘EC2’ as service and ‘EC2 instance State-change notification’ as the Event type.
Select ‘Any instance’.
Click ‘Add target’
Provide SNS topic details and click ‘Configure details’. You will be redirected to the rule page.
Step 4: Provide the rule name and description. Click ‘Create rule’.
Our rule successfully created.
Next, we are going to stop the instance so that EC2 instance state changes.
You will get 2 emails for the EC2 state ‘stopping’ and ‘stopped’.
Hope you have enjoyed this article. In the next blog post we are going to discuss CloudWatch Logs.
To get more details on CloudWatch, please refer below AWS documentation