Automatically terminate EC2 instances with configurable lifetimes
This workflow looks at all of the EC2 instances in a given account and region and selects a subset of those to terminate. The termination criteria are:
- Not tagged with a termination date or lifetime after 4 minutes
- The
termination_dateorlifetimetags are present but cannot be parsed - The
termination_dateorlifetimetags indicate that the instance hasexpired
An instance may be configured to never terminate if its lifetime tag has thespecial value indefinite.
Prerequisites
Before you run this workflow, you will need the following:
- An AWS account.
- An AWS IAM user with permissions to list and terminate EC2 instances (if notrun in dry run mode).
- One or more running EC2 instances that are configured to use the
termination_dateorlifetimetags.
Run the workflow
Follow these steps to run the workflow:
Add your AWS credentials as secrets:
- Click Edit > Secrets.
- Click Define new secret and use the following values:
-
KEY :
aws.accessKeyID - VALUE : Enter your AWS access key id associated with the account
-
KEY :
aws.secretAccessKey -
VALUE : Enter your AWS secret access key associated with the account
- Click Run workflow and wait for the workflow run page to appear.
-
Warning: If you run the workflow with the
dryRunparameter set tofalse, instances not in compliance with this workflow policy willimmediately be terminated.
Automatically running on a schedule
Policy-driven workflows are best run on a recurring schedule. To set up aschedule trigger for this workflow:
- Click Edit > Triggers.
Click Define new trigger and use the following values:
- Trigger type : Schedule
- Trigger integration : System
-
Interval : Intervals follow the ISO 8601 repeatingintervalformat. To run this workflow every 5 minutes indefinitely from now on,enter:
R/2020-01-01T00:00:00Z/PT5M. You can configure the interval at theend of this string to change the execution frequency.- Enter values for the parameter bindings that match your environment.
- Click Add trigger.
Within the next 5 minutes, you should see the workflow run automatically for thefirst time.
Top comments (0)