From a4c2dbe85693acaae92a5e5b01bead4de18a67f5 Mon Sep 17 00:00:00 2001 From: shiva kumar Date: Fri, 13 Dec 2024 21:31:49 +0530 Subject: [PATCH] unused aws instace and vpcs cleanup Signed-off-by: shiva kumar --- .github/workflows/awscleanup.yaml | 86 +++++++++++++++++++++++++++++++ .github/workflows/image.yaml | 8 +-- 2 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/awscleanup.yaml diff --git a/.github/workflows/awscleanup.yaml b/.github/workflows/awscleanup.yaml new file mode 100644 index 00000000..6343699a --- /dev/null +++ b/.github/workflows/awscleanup.yaml @@ -0,0 +1,86 @@ +name: Daily Cleanup Bot + +# on: +# schedule: +# - cron: '0 8 * * *' + +on: + pull_request: + types: + - opened + - synchronize + branches: + - awsresourcecleanup + push: + branches: + - awsresourcecleanup + +jobs: + cleanup: + runs-on: linux-amd64-cpu4 + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up AWS CLI + uses: aws-actions/configure-aws-credentials@v3 + env: + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SSH_KEY: ${{ secrets.AWS_SSH_KEY }} + AWS_REGION: "us-west-1" + + - name: Identify resources running longer than 24 hours + id: identify-resources + run: | + # Find EC2 instances running longer than 24 hours + running_instances=$(aws ec2 describe-instances \ + --filters Name=instance-state-name,Values=running \ + --query "Reservations[*].Instances[?LaunchTime<=\`$(date -u -d '24 hours ago' +%Y-%m-%dT%H:%M:%SZ)\`].InstanceId" \ + --output text) + echo "Found instances: $running_instances" + echo "instances=$running_instances" >> $GITHUB_ENV + + # Find unused VPCs + vpcs=$(aws ec2 describe-vpcs \ + --query "Vpcs[?IsDefault==\`false\`].VpcId" \ + --output text) + + echo "Found VPCs: $vpcs" + echo "vpcs=$vpcs" >> $GITHUB_ENV + + - name: Terminate EC2 Instances + if: env.instances != '' + run: | + for instance in $instances; do + echo "Terminating instance: $instance" + #aws ec2 terminate-instances --instance-ids $instance + done + + - name: Clean up VPCs + if: env.vpcs != '' + run: | + for vpc in $vpcs; do + attempts=0 + # try 3 times with 5 minutes interval + while [ $attempts -lt 3 ]; do + echo "Attempting to delete VPC: $vpc (Attempt $((attempts+1)))" + if aws ec2 delete-vpc --vpc-id $vpc; then + echo "Successfully deleted VPC: $vpc" + break + else + echo "Failed to delete VPC: $vpc. Retrying in 5 minutes..." + attempts=$((attempts + 1)) + sleep 300 + fi + done + + if [ $attempts -eq 3 ]; then + echo "Failed to delete VPC: $vpc after 3 attempts. Skipping." + fi + done + + - name: Post cleanup + run: | + echo "Cleanup completed." \ No newline at end of file diff --git a/.github/workflows/image.yaml b/.github/workflows/image.yaml index 0dade847..ef05d429 100644 --- a/.github/workflows/image.yaml +++ b/.github/workflows/image.yaml @@ -21,12 +21,12 @@ on: - opened - synchronize branches: - - main - - release-* + - main-no + - release-no push: branches: - - main - - release-* + - main-no + - release-no jobs: image: