DEV Community

Cover image for AWS Global EC2 notifications w/CDK + CF
Mick
Mick

Posted on

AWS Global EC2 notifications w/CDK + CF

Here is what we'll be building today:

I present, tnc-ec2runner, a helpful? solution, maybe:

Alt Text

But why?

On my usual journey around the internet looking at all things AWS, I found a user asking the question:

How can I find all ec2 instances across all regions and receive a notification if the instance is running for longer than 24, 48 or 72 hours?

And I thought, that is a good question. I've seen the AWS solution for instance scheduling, it's been around for a while but that didn't really fit this use case. I was curious to know if we could tackle this with Lambda and SNS. I started by creating a standard python boto3 program and digging through the various bits of documentation to see if it was at least possible.

Indeed it was possible, listing all ec2 regions is actually pretty simple, now all we need to do is loop through them and then query some EC2 instance information and compare that against the current time and the threshold.

What this solution does

At a high level this solution basically does the following:

  • Runs once a day on a schedule
  • Runs a Lambda program
  • Loops through all EC2 regions
  • Finds ec2 instances stopped, removes tnc-ec2runner tag if present
  • Finds ec2 instances running
  • Checks ec2 instances against three time based thresholds
  • Publishes to SNS topic if found
  • Emails user

Lambda

The lambda function was the easy bit, throw some code up in AWS, put some environment variables in to make it a bit more user friendly for the reader and it should be fine? Well, it was, how good is that?

SNS

Like lambda, SNS should be easy and it also was, the integration between Lambda and SNS is easy. Boto3 makes it simple to program. No issues.

CDK

This stack was developed in typescript using the AWS CDK, spoiler, all of my projects are! I did learn a new trick in CDK which makes it a bit cleaner. I used a helper to associate the SNS and Lambda together for policy so that everything was on the up and up.

Want to have a go?

All of the code is open source on github, you can simply pull and deploy as CDK or you can use the Launch Stack button which deploys this solution as a cloudformation stack in your account. Easy as:

https://github.com/talkncloud/aws/tree/main/global-ec2-notifications

Full write up

I have done a full write up on my site if you want to go into detail, there cost estimates (hint $0) and a program flow chart as well as tips on how to customize:

https://www.talkncloud.com/aws-ec2-global-notification-runner/

Closing

Most of my posts seem to be related to saving money in AWS, not sure how it ended up this way but it's all good fun. I like that this solution is cost effective, simple to use and you can really see how you can customize it for your needs.

I'm keen to hear from others on what they think, how they would have tackled it. What would you have done?

Discussion (0)