Skip to content

Commit

Permalink
unused aws instace and vpcs cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shivakunv committed Dec 18, 2024
1 parent 992e9e1 commit 39bfc91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
37 changes: 10 additions & 27 deletions .github/workflows/awscleanup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ jobs:
- name: Identify resources running longer than 4 hours
id: identify-resources
run: |
# Find EC2 instances with names ci* running longer than 4 hours
running_instances=$(aws ec2 describe-instances \
--filters Name=instance-state-name,Values=running Name=tag:Name,Values=ci* \
--query "Reservations[*].Instances[?LaunchTime<=\`$(date -u -d '4 hours ago' +%Y-%m-%dT%H:%M:%SZ)\`].InstanceId" \
--output text | tr -d '\r' | tr '\n' ' ')
echo "Found instances: $running_instances"
echo "instances=$running_instances" >> $GITHUB_ENV
# Find vpcs with names ci*
vpcs=$(aws ec2 describe-vpcs \
--filters "Name=tag:Name,Values=ci*" \
Expand All @@ -49,29 +41,20 @@ jobs:
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
# Check for EC2 instances attached to VPC
instances_in_vpc=$(aws ec2 describe-instances \
--filters "Name=vpc-id,Values=$vpc" \
--query "Reservations[*].Instances[*].InstanceId" \
--output text)
# if no instance attached delete it
if [ -z "$instances_in_vpc" ]; then
scripts/awsvpcscleanup.sh $vpc
else
echo "EC2 instances are still attached to VPC: $vpc. Skipping deletion."
fi
response=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/${RUN_ID}/jobs")
echo "Response: $response"
status=$(echo "$response" | jq -r ".jobs[] | select(.name == \"$JOB_NAME\") | .status")
conclusion=$(echo "$response" | jq -r ".jobs[] | select(.name == \"$JOB_NAME\") | .conclusion")
echo "status=$status"
echo "conclusion=$conclusion"
scripts/awsvpcscleanup.sh $vpc
done
- name: Post cleanup
Expand Down
11 changes: 11 additions & 0 deletions scripts/awsvpcscleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ export vpc=$1

echo "Start Deleting VPC: $vpc resource"

aws resourcegroupstaggingapi get-resources --tag-filters Key=vpc-id,Values=$vpcid

aws_resources=$(aws resourcegroupstaggingapi get-resources --tag-filters Key=vpc-id,Values=$vpcid)

exit 1
# Delete Instance
for instance in $instances; do
echo "Terminating instance: $instance"
aws ec2 terminate-instances --instance-ids "$instance"
done

# Delete Internet Gateway
internet_gateways=$(aws ec2 describe-internet-gateways \
--filters Name=attachment.vpc-id,Values=$vpc \
Expand Down

0 comments on commit 39bfc91

Please sign in to comment.