At TrackIt, we are acutely aware of AWS costs; we know things can get expensive if you don’t monitor your resources. That’s why we began tagging our resources with (at the minimum) a simple tag: `TrackitPersistent`. This tag value can be true or false depending on whether we need the resource to be persistent — or not.
We use our tool Tagbot, that is available on the Amazon Marketplace, to track those “not persistent” resources, providing easy visibility to non-essential processing. If you want to know more about tagging & Tagbot you can follow this link: https://aws.amazon.com/blogs/apn/how-better-tagging-can-help-organizations-optimize-expenses-and-improve-roi/.
While this was a good way to start, we wanted to be more proactive and that is where AWSTerminator came into play.
AWSTerminator is an open-source tool, also developed by TrackIt, that will help you and your team manage your resources through tags:
- It will notify you on Slack about untagged resources
- It will stop untagged resources
- It will terminate/delete untagged resources
Here is the description of how it works!
AWSTerminator comes with a simple CloudFormation template file that will deploy a stack with one behavior (notify, stop or terminate), then a lambda will be triggered depending on a given schedule expression. You’ll be able to give the template some parameters to configure the lambda:
- AwsRegions: A list of regions where you want to execute the AWSTerminator behavior (e.g.: “us-west-1,us-west-2”)
- SlackWebHook: A webhook url that you can get from a Slack channel
- PersistentTagKey: The desired tag key (e.g.: “TrackitPersistent”)
- PersistentTagValue: The desired tag value (e.g.: “True”)
- LifetimeTagKey: We’ll talk later about this feature
- DesiredMessage: A simple message that you want to see on the slack notification
- Behavior: Whether notify, stop or terminate
- ScheduleExpression: A cron expression to trigger the lambda when you want (e.g.: “cron(0 16 ? * THU *)”)
Contents
Persistent Tag
PersistentTagKey and PersistentTagValue parameters will both be taken into consideration to determine if resources are persistent or not.
For example, if I have an EC2 instance tagged with `TrackitPersistent`=`False`, and that `PersistentTagKey`=`TrackitPersistent` and `PersistentTagValue`=`True`, AWSTerminator will detect that this resource shouldn’t be persistent — because the tag key & value doesn’t match. It will then send a notification to Slack or stop/terminate the EC2 instance depending on the tool’s behavior.
LifetimeTag
We discovered that the persistent tag wasn’t quite enough, however; sometimes you want to create a resource just to be able to run a test, for example. In this case, you don’t want your resource to be persistent, so then you’d tag it with `TrackitPersistent`=`False`, but when the lambda is triggered with the terminate behavior your resource will be removed from your AWS account! You’d have to recreate it again in order to continue your tests. This is obviously inconvenient since we don’t want our resources to be destroyed while we are using them…
The LifetimeTagKey is a new feature that will allow you to define when your resource should be expired. Let’s say our DesiredLifetimeTagKey is `TrackitLifetime`. Resources that must have an expiration date could be tagged in 2 different ways:
- `TrackitLifetime`=`yyyy-mm-dd`
- `TrackitLifetime`=`x` with `x` a number of days
Note: if the resource targeted doesn’t have a creation date (like an ECS Cluster), the second usage won’t work.
AWSTerminator will then take a look at this tag value and determine whether the resource is expired or not. Resources with the LifetimeTag will always be notified on Slack (notify behavior), but they won’t be stopped/terminated until they are expired.
Resources
Currently, AWSTerminator covers EC2, ECS, Fargate & RDS resources:
AWSTerminator is an open-source project and we would be delighted to have you help us develop the tool.
How to Use AWS Terminator
Requirements
To deploy AWSTerminator, you’ll need an S3 Bucket in order to deploy the Lambda with CloudFormation and the AWS CLI. You’ll also have to clone our Github repository.
Package
Before deploying the stack you’ll have to make a zip of the code:
$ cd aws-clean-untagged-resources
Make sure you’ve installed dependencies, if not you can use the following command:
$ pip3 install — target ./package -r requirements.txt
You’re now able to zip packages (you can skip this step if packages were already zipped):
$ cd package<br>$ zip -r ../../aws-clean-untagged-resources.zip .<br>$ cd ..
Every time you make changes on the code you’ll have to zip your code like the following (including the first time you want to deploy):
$ zip -g ../aws-clean-untagged-resources.zip *.py
If you want more details about these steps you can take a look at the AWS documentation.
Slack Webhook
You’ll also need to create a Slack webhook on your slack workspace (this is not mandatory but the notify behavior won’t work without it). You can follow this Slack guide in order to generate a webhook url for a given Slack channel.
Deploy
To deploy the function, replace the variables in the following commands:
$ aws cloudformation package --template-file template.yml --s3-bucket YOUR_DEPLOYMENT_BUCKET --s3-prefix YOUR_S3_PREFIX --output-template-file output.yaml$ aws cloudformation deploy --template-file output.yaml --stack-name STACK_NAME --capabilities CAPABILITY_NAMED_IAM --parameter-overrides SlackWebHook=YOUR_SLACK_WEBHOOK_URL ScheduleExpression="cron(0 16 ? * THU *)" PersistentTagKey=MY_RESOURCE_TAG_NAME PersistentTagValue=MY_RESOURCE_TAG_VALUE LifetimeTagKey=LIFETIME_TAG_KEY Behavior=notify|stop|terminate AwsRegions=AWS_REGIONS
Destroy
In order to destroy the stack you’ll have to perform the following command:
$ aws cloudformation delete-stack STACK_NAME
Ideal Solution for Organizations Looking to Minimize Expenses by Optimizing their Resources
When combined with a tagging tool like Tagbot, AWS Terminator is an ideal solution for organizations looking to minimize expenses by optimizing the management of their resources on the cloud.
About TrackIt
TrackIt is an international AWS cloud consulting, systems integration, and software development firm headquartered in Marina del Rey, CA.
We have built our reputation on helping media companies architect and implement cost-effective, reliable, and scalable Media & Entertainment workflows in the cloud. These include streaming and on-demand video solutions, media asset management, and archiving, incorporating the latest AI technology to build bespoke media solutions tailored to customer requirements.
Cloud-native software development is at the foundation of what we do. We specialize in Application Modernization, Containerization, Infrastructure as Code and event-driven serverless architectures by leveraging the latest AWS services. Along with our Managed Services offerings which provide 24/7 cloud infrastructure maintenance and support, we are able to provide complete solutions for the media industry.