How to set up AWS OpsWorks Stacks auto healing notifications in Amazon CloudWatch Events
In this post, we describe how to set up Amazon CloudWatch Events so that you are notified when AWS OpsWorks Stacks auto healing triggers stop and start events on an Amazon EC2 instance.
Recently, AWS OpsWorks Stacks announced support for CloudWatch Events. You can now send state changes in OpsWorks Stacks, such as instance stopped or deployment failed, to CloudWatch Events.
AWS OpsWorks Stacks provides you with an integrated management experience that spans the entire application lifecycle including resource provisioning, Amazon EBS volume setup, configuration management, application deployment, monitoring, and access control.
Amazon CloudWatch Events enables you to react selectively to events in the cloud as well as in your applications. Specifically, you can create CloudWatch Events rules that match event patterns, and take actions in response to those patterns. CloudWatch Events lets you process both AWS-provided events and custom events (those that you create and inject yourself).
Scenario – Instance auto healed
In our scenario, an organization has multiple instances in multiple OpsWorks Stacks, where auto healing can be enabled on all layers. Auto healing functions by issuing a stop and start command to instances it considers have failed.
A problem arises when an instance in OpsWorks Stacks is stopped and started by auto healing, but the organization doesn’t know who or what stopped and started the instance.
AWS OpsWorks Stacks event sources are now available in Amazon CloudWatch Events. You can use these event sources to trigger events or notifications, including events triggered by auto healing.
Previously, you had to orchestrate calls to multiple APIs to trigger events in response to OpsWorks Stacks events. Now, you can use CloudWatch Events to trigger an event when an OpsWorks Stacks instance, deployment, or command state changes. In addition you can use CloudWatch Events to indicate that an OpsWorks Stacks service error was raised.
Events patterns in CloudWatch Events are represented as JSON objects. The following AWS OpsWorks Stacks event types are supported in CloudWatch Events.
- Instance state changes: Includes states such as “requested”, “online”, “stopped”.
- Command execution results: Includes states such as “successful”, “failed”, “expired”.
- Deployment results: Includes states such as “running”, “successful”, “failed”.
- Alerts: Indicates an OpsWorks Stacks service error was raised.
Set up the solution
To create CloudWatch rules, you first need to create a target. In this solution we will use an Amazon SNS topic, but Amazon SQS and AWS Lambda functions are other possibilities.
Create an SNS topic named OpsWorksAutoHealingNotifier. To receive messages published to the topic, you have to subscribe an endpoint to that topic. Complete the subscription by opening the confirmation message from AWS Notifications, and then click the link to confirm your subscription.
Now we are ready to create our CloudWatch Rule.
To create the rule using the AWS Management Console:
- Open the AWS Management Console, and go to the Amazon CloudWatch console.
- In the left navigation pane, chose Events.
- Choose Create Rule, and then choose Show advanced options.
- Choose Edit the JSON version of the pattern.
- Paste the contents of the OpsWorksAutoHealingPattern.json file in the text box.
- Choose Add target under the Targets section. Select the SNS Topic OpsWorksAutoHealingNotifier from the list.
- Choose Configure details.
- Name the rule as OpsWorks-AutoHealing.
To create the rule with the AWS CLI:
1. Save the following event pattern as a file named OpsWorksAutoHealingPattern.json.
2. Create a rule named OpsWorks-AutoHealing by running the following command.
3. Make the SNS Topic, OpsWorksAutoHealingNotifier, that you created earlier the target of the rule by running the following command.
4. Save the following policy as a file named OpsWorksAutoHealingPolicy.json.
5. Use the following command to add permission to the SNS topic, so that CloudWatch Events can invoke and publish a notification to the topic.
Make sure to use a different ID for the second target if you are using the AWS CLI, or choose Add Target one more time in the AWS Management Console. If you use the AWS CLI, add resource-based policies for CloudWatch Events to invoke each of the targets.
Create notifications when instances are in start_failed or stop_failed status by setting up notifications.
One example of a situation in which you could build a custom event pattern is when an instance times out when stopping.
In this blog post, I show you how to create a CloudWatch Events rule that monitors for stop and start events invoked by OpsWorks Stacks auto healing.
If you have comments about this blog post, submit them in the Comments section. If you have troubleshooting questions related to the solution in this post, please start a new thread on the OpsWorks forum.
About the Author
David Bezuidenhout is a Cloud Support Engineer based in Cape Town, South Africa, where he focuses on AWS OpsWorks Stacks and CloudFormation. David is passionate about DevOps and Infrastructure as code. In his spare time, he enjoys spending time touring through South Africa.