Skip to content

Instantly share code, notes, and snippets.

@lucasproclc
Forked from willgarcia/aws-cleanup.sh
Created September 2, 2021 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasproclc/562d5ac1762e494f5e7f1102325e3d3b to your computer and use it in GitHub Desktop.
Save lucasproclc/562d5ac1762e494f5e7f1102325e3d3b to your computer and use it in GitHub Desktop.
Delete all AWS resources (lambdas, cloudfront distribs, ec2, lb, api-gateways, ...)
#!/bin/bash
set -e
AWS_FUNCTIONS=$(aws lambda list-functions --query "Functions[].FunctionArn")
for func in $AWS_FUNCTIONS
do
CMD="aws lambda delete-function --function-name $func"
echo $CMD
$CMD
done
echo "-- Lambda functions deleted"
AWS_CLOUDFRONT_DISTRIBS=$(aws cloudfront list-distributions --query DistributionList.Items[].Id)
for elem in $AWS_CLOUDFRONT_DISTRIBS
do
aws cloudfront get-distribution-config --id $elem --output json > distrib-config.json
ETAG=$(aws cloudfront get-distribution-config --id E2ZPP4ZMA4Z76V --output json | jq .ETag)
cat distrib-config.json | jq ".DistributionConfig.Enabled = false" | jq .DistributionConfig > distrib-config-disabled.json
CMD="aws cloudfront update-distribution --id $elem --distribution-config file://distrib-config-disabled.json --if-match $ETAG"
echo $CMD
$CMD
STATUS=$(aws cloudfront get-distribution --id E2ZPP4ZMA4Z76V --output json | jq .Distribution.Status)
while [ $STATUS = '"InProgress"' ]
do
sleep 10
echo "-- Waiting for the distribution to be disabled"
done
CMD="aws cloudfront delete-distribution --id $elem --if-match $ETAG"
echo $CMD
$CMD
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment