aws-cdk-examples icon indicating copy to clipboard operation
aws-cdk-examples copied to clipboard

cdkv2 python sample ec2-alarms-opsitem

Open alekasw opened this issue 2 years ago • 0 comments

Describe the feature

This example shows how you can use the cdk version 2 to automate attaching an alarm to an ec2 instance at launch time, then when the alarm is triggered how it can create an systems manager opsitem with an associated run book.

Use Case

Show customers how they can easily automate attaching Alarms to instances when they are deployed. Also shows how CDK can be used to: -Create Event bridge rule -Deploy lambda functions -Bootstrap software installation on EC2 instances -Deploy SSM Run Command Document

Proposed Solution

Koi-Demo-Architecture

The workflow of this solution is as follows: When an EC2 instance is launched it will trigger an Eventbridge rule that kicks off a lambda function. The lambda function determines if the EC2 instance already has a matching alarm. If it does not then it will create and attach a "StatusCheckFailed" metric alarm and tag the instance so next time it is launched it will skip the Alarm creation logic.

The solution also deploys an SSM automation run command document that can be used to easily trigger the alarm via a bash shell script that executes the set-alarm-state aws cli command.

Once the alarm is triggered another Eventbridge rule will kick-off the second lambda function that creates an SSM OpsItem with an associated runbook.

Other Information

IAM Policies and Roles IAM Instance Profile EC2 Instance SNS Topic and Subscription Lambda Functions (Two) Eventbridge Rules (Two) SSM Document

Environment CDK CLI Version: 2.4.x Example: Automate EC2 alarm creation that triggers an OpsItem when in "ALARM" state Example Version: 2.0 OS: Amazon Linux 2 Language: Python

Acknowledgements

  • [X] I may be able to implement this feature request
  • [ ] This feature might incur a breaking change

Language

Python

alekasw avatar Sep 23 '22 17:09 alekasw