Skip to main content

AWS EC2 instance Reaper

Within EC2 there are quite some use cases for ephemeral ec2 instances: for batch jobs like Hashicorp Packer and Gitlab autoscaling runners. However, sometimes these ephemeral instances are not stopped because errors in the controlling process.

This utility stops or terminated all virtual machines which have the tags 'ExpiresAfter' and 'ExpirationAction' set.

tag Description
ExpiresAfter duration after which the instance is considered expired
ExpirationAction action to take after the expiration time: terminate or stop

You can use it as a command line utility or install it as an AWS Lambda function and stop the spend , NoOps style!

To make the utility work for hashicorp Packer, add a run_tags block to the Packer builder specification:

source "amazon-ebs" "ubuntu" {
   name = "my_ubuntu"
   ...
   run_tags = {
     ExpiresAfter = "2h"
     ExpirationAction = "terminate"
   }

install the ec2 instance reaper

to install the ec2 instance reaper, type:

pip install aws-ec2-instance-reaper

show running ephemeral instances

To show running ephemeral instances:

$ aws-ec2-instance-reaper list

i-06ac951992dbc11a1 (Packer Builder) launched 13 minutes ago - terminated
i-035ebe427a538c829 (Packer Builder) launched 4 minutes ago - running
INFO: 2 emphemeral ec2 instances found

usage with multiple profiles

If you have multiple AWS Profiles (e.g. when working with AWS Organisations and SSO) you can specify the profile to use on the command line before executing the aws-ec2-instance-reaper.

$ AWS_PROFILE=not-my-default-profile aws-ec2-instance-reaper list

i-0d72937e04c1123a9 (Packer Builder) launched 31 minutes ago - running
i-0712d9bd593adb2fb (Packer Builder) launched 11 minutes ago - running
INFO: 2 emphemeral ec2 instances found

stop ephemeral instances

To stop running ephemeral instances which expired:

$ aws-ec2-instance-reaper stop

INFO: stopping i-035ebe427a538c829 (Packer Builder) created 2 hours ago
INFO: total of 1 running ephemeral instances stopped

terminate ephemeral instances

To terminate stopped and running expired ephemeral instances

aws-ec2-instance-reaper --verbose terminate

INFO: terminating i-035ebe427a538c829 (Packer Builder) created 25 hours ago
INFO: total of 1 instances terminated

deploy the ec2 instance reaper

To deploy the ec2 instance reaper as an AWS Lambda, type:

git clone https://github.com/binxio/aws-ec2-instance-reaper.git
cd aws-ec2-instance-reaper
aws cloudformation deploy \
	--capabilities CAPABILITY_IAM \
	--stack-name aws-ec2-instance-reaper \
	--template-file ./cloudformation/aws-ec2-instance-reaper.yaml

This will install the ec2 instance reaper in your AWS account and run every hour.

Metadata

Release files for aws-ec2-instance-reaper 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aws-ec2-instance-reaper 0.1.4
File Size Uploaded
aws_ec2_instance_reaper-0.1.4.tar.gz 19.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aws-ec2-instance-reaper 0.1.4
File Interpreter ABI Platform
aws_ec2_instance_reaper-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 32.9 kB

Release files / aws_ec2_instance_reaper-0.1.4.tar.gz

Download URL aws_ec2_instance_reaper-0.1.4.tar.gz
Size 19.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5cdcbe898c7cf66995e4d281a5c7bd917d13cec5be4386565ac3582de855c506
BLAKE2b-256 checksum
How to use checksums
f199837910f6c002fbd6b5fb8b946ad57e706f0dec50e62153d2f5184086283a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.17

Release files / aws_ec2_instance_reaper-0.1.4-py3-none-any.whl

Download URL aws_ec2_instance_reaper-0.1.4-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be96fda1336465e3bd0ecbd5c9ccf08d8c5346ce6aeed2b41f1f9d1eef3026e5
BLAKE2b-256 checksum
How to use checksums
a7a641f3c27a8197194f30386d052a0c7b827404fa0aa517ea7fbdaec1780c4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.17

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page