This is Part 3 of the series of setting up a Chatbot for deploying artifacts to AWS EC2 Windows instances. In this post, I’ll discuss using AWS Lambda to make sure instances are not left running for too long to save money.
The Chatbot makes deployment much easier and encourages developers to run more tests. However, those launched instances are more likely to be left running for a long time. The situation is worse for Windows instances, which are way more expensive that Linux instances ($0.15 comparing to $0.03). So I need a way to terminate those instances when not used.
Here is my solution. I asked developers to specify the time-to-live (TTL) for each deployment with a default
ttl of 60 minutes. I added an extra argument
ttl and passed it to Ansible playbook. The
ttl value is added as a tag to the EC2 instance. I used an AWS Lambda scheduled event to trigger the update of this tag value. When the tag value reaches
0, the instance is terminated. If the developer wants to prolong the deployment, the tag can be updated manually using AWS console, or a new Chatbot command can be developed to do that.
managed_by with value
describeInstances lists all running instances managed by Ansible. For each found instance, it checks the value of tag
ttl and determines whether the instance should be terminated. Instances are terminated using
terminateInstances, while tags are updated using
Note: This Lambda function can only terminate instances or update tags in one run, it cannot do both. This is because async programming with AWS Lambda is not that easy. Of course, you can use the library async and upload the packaged code to AWS. But that’s too much for a simple task like this. You can improve the script yourself for more advanced usage.