Created
March 4, 2023 06:22
-
-
Save guessi/fef9167ffaecc929a21d4b1722130d74 to your computer and use it in GitHub Desktop.
Helper script for cleaning up inactive task definition revisions across regions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# On Feb 27, 2023, Amazon ECS supports deletion of inactive task definition revisions: | |
# | |
# Announcement: | |
# - https://aws.amazon.com/about-aws/whats-new/2023/02/amazon-ecs-deletion-inactive-task-definition-revisions/ | |
# | |
# Blog Post: | |
# - https://aws.amazon.com/blogs/containers/announcing-amazon-ecs-task-definition-deletion/ | |
# | |
# Developer Guide: | |
# - https://docs.aws.amazon.com/AmazonECS/latest/developerguide/delete-task-definition-v2.html | |
# | |
# Here's the sample script could helps you cleanup all INACTIVE task definition revisions at once. | |
# | |
# Disclaimer: | |
# - You should always test the script under your test environment, DO NOT use it on production without tests. Use it with caution. | |
# | |
# Usage: | |
# | |
# #### run with DRY_RUN mode on | |
# $ ./delete-inactive_task_definition_revisions.sh | |
# | |
# #### explicitly run with DRY_RUN mode on | |
# $ DRY_RUN=true ./delete-inactive_task_definition_revisions.sh | |
# | |
# #### explicitly run with DRY_RUN mode off (will send out delete target resource) | |
# $ DRY_RUN=false ./delete-inactive_task_definition_revisions.sh | |
# | |
declare DRY_RUN=${DRY_RUN:-true} | |
echo "Searching for AWS Regions... " | |
regions=($(aws ec2 describe-regions --no-cli-pager --filters 'Name=opt-in-status,Values=opt-in-not-required' --query 'Regions[*][RegionName]' --output text)) | |
echo "Found ${#regions[@]} AWS regions" | |
echo | |
for region in "${regions[@]}"; do | |
echo "Processing region: ${region}" | |
printf "Searching for Inactive Task Definition Revisions... " | |
inactive_task_definition_revisions=($(aws ecs list-task-definitions --no-cli-pager --region "${region}" --status "INACTIVE" --query "taskDefinitionArns[]" --output text)) | |
echo "Found ${#inactive_task_definition_revisions[@]} Inactive Task Definition(s)" | |
for inactive_task_definition_revision in "${inactive_task_definition_revisions[@]}"; do | |
if [ "${DRY_RUN}" != "false" ]; then | |
printf "*** \e[1;32m[DRY_RUN: ON]\e[m Found \"%s\", skipped ***\n" "${inactive_task_definition_revision}" | |
else | |
printf "*** \e[1;31m[DRY_RUN: OFF]\e[m Found \"%s\", deleting ***\n" "${inactive_task_definition_revision}" | |
aws ecs delete-task-definitions --no-cli-pager --region "${region}" --task-definitions "${inactive_task_definition_revision}" --output json | |
fi | |
done | |
echo | |
sleep 0.3 # To avoid being blocked by throttle | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment