Skip to main content

reap dangling EC2 machines on AWS

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws_ec2_instance_reaper-0.1.4.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

aws_ec2_instance_reaper-0.1.4-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file aws_ec2_instance_reaper-0.1.4.tar.gz.

File metadata

File hashes

Hashes for aws_ec2_instance_reaper-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5cdcbe898c7cf66995e4d281a5c7bd917d13cec5be4386565ac3582de855c506
MD5 78912947f18e8e39425a9f87af62e960
BLAKE2b-256 f199837910f6c002fbd6b5fb8b946ad57e706f0dec50e62153d2f5184086283a

See more details on using hashes here.

File details

Details for the file aws_ec2_instance_reaper-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for aws_ec2_instance_reaper-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 be96fda1336465e3bd0ecbd5c9ccf08d8c5346ce6aeed2b41f1f9d1eef3026e5
MD5 dc621053c8a9a58cda0958258d020e67
BLAKE2b-256 a7a641f3c27a8197194f30386d052a0c7b827404fa0aa517ea7fbdaec1780c4f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page