Skip to content

feat: cleanup ebs snapshots from testinfra ami's#2098

Draft
jfreeland wants to merge 1 commit intodevelopfrom
feat/cleanup-volumes
Draft

feat: cleanup ebs snapshots from testinfra ami's#2098
jfreeland wants to merge 1 commit intodevelopfrom
feat/cleanup-volumes

Conversation

@jfreeland
Copy link
Copy Markdown
Contributor

@jfreeland jfreeland commented Mar 30, 2026

WIP: I need to go validate this before we merge.

What kind of change does this PR introduce?

Delete the EBS snapshots backing testing AMI's that are built from the testinfra workflow. I think we're not deleting the snapshots (EBS volumes) when we're done. This has to be done separately in AWS.

@jfreeland jfreeland requested review from a team as code owners March 30, 2026 13:25
Copilot AI review requested due to automatic review settings March 30, 2026 13:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds cleanup logic to the Testinfra AMI build workflow to remove AMI artifacts created during the run, aiming to prevent lingering AWS resources (and associated costs) after the workflow completes.

Changes:

  • Fetch EBS snapshot IDs associated with the “stage 2” AMIs created by the workflow.
  • Deregister stage 2 AMIs and attempt to delete their backing EBS snapshots.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +149 to +153
SNAPSHOT_IDS=$(aws ec2 describe-images \
--region ap-southeast-1 \
--image-ids "$ami_id" \
--query 'Images[*].BlockDeviceMappings[*].Ebs.SnapshotId' \
--output text)
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description/title mention cleaning up EBS volumes, but this change deletes EBS snapshots associated with the AMI. If the intent is snapshot cleanup (likely the actual cost driver), please update the PR description/title for accuracy; if volumes are also being left behind, add a corresponding describe-volumes/delete-volume cleanup based on the same execution-id tags.

Copilot uses AI. Check for mistakes.
Comment on lines +149 to +158
SNAPSHOT_IDS=$(aws ec2 describe-images \
--region ap-southeast-1 \
--image-ids "$ami_id" \
--query 'Images[*].BlockDeviceMappings[*].Ebs.SnapshotId' \
--output text)
echo "Deregistering stage 2 AMI: $ami_id"
aws ec2 deregister-image --region ap-southeast-1 --image-id "$ami_id" || true
for snap_id in $SNAPSHOT_IDS; do
echo "Deleting snapshot: $snap_id"
aws ec2 delete-snapshot --region ap-southeast-1 --snapshot-id "$snap_id" || true
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--query 'Images[*].BlockDeviceMappings[*].Ebs.SnapshotId' --output text can yield None/empty values for non-EBS mappings; the for snap_id in $SNAPSHOT_IDS loop will then attempt to delete an invalid snapshot id. Filter out nulls in the query (or guard in bash) so only real snapshot IDs are passed to delete-snapshot.

Copilot uses AI. Check for mistakes.
Comment on lines 155 to +159
aws ec2 deregister-image --region ap-southeast-1 --image-id "$ami_id" || true
for snap_id in $SNAPSHOT_IDS; do
echo "Deleting snapshot: $snap_id"
aws ec2 delete-snapshot --region ap-southeast-1 --snapshot-id "$snap_id" || true
done
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aws ec2 delete-snapshot ... || true will silently leave snapshots behind when deletion fails (e.g., eventual-consistency right after deregistration, snapshots still referenced, or IAM permission issues), which undermines the goal of this cleanup. Consider adding a short retry/backoff (and/or collecting failures and failing the step at the end) so leaks are surfaced and cleanup is more reliable.

Copilot uses AI. Check for mistakes.
@jfreeland jfreeland changed the title feat: cleanup ebs volumes from testinfra ami's feat: cleanup ebs snapshots from testinfra ami's Mar 30, 2026
@jfreeland jfreeland marked this pull request as draft March 30, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants