Delete snapshots after the packer build is terminated ?

1,961 views
Skip to first unread message

Vinaykumar Kadalagi

unread,
Jun 17, 2016, 7:49:37 AM6/17/16
to Packer
Hi I have been using packer since last few days. It is creating AMIs along with snaphots under EBS -> Snapshots. 
And they are not removed even after the build is successful or a failure. 
Is it suppose to remove it or no? If no, Why? And how can it be improved?

Carlos Tovar

unread,
Aug 24, 2016, 3:33:46 PM8/24/16
to Packer
I am running into the same issue.  Does anybody have any ideas?

Rickard von Essen

unread,
Aug 30, 2016, 1:37:49 AM8/30/16
to packe...@googlegroups.com

Quoting the AWS documentation
"Note that you can't delete a snapshot of the root device of an EBS volume used by a registered AMI. You must first deregister the AMI before you can delete the snapshot. For more information, see Deregistering Your AMI."
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-deleting-snapshot.html

If there are snapshots from failed builds I think it's a bug. Please file an issue.


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/86be55ae-82aa-401a-8390-3a86b476b873%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nirav Radia

unread,
Sep 2, 2016, 9:40:02 AM9/2/16
to Packer
I also had a similar problem. There is a CR for that also (https://github.com/mitchellh/packer/issues/3320)
For solving it currently, I gave a tag to AMI (and hence snapshot). Now before starting AMI generation process, I store snapshotid by AWS CLI command. 

previousSnapshotId=$(aws ec2 describe-images --region ${REGION} --owners self --filters "Name=tag:type,Values=${image_type}" "Name=tag:product_version,Values=${product_version}" --query 'Images[0].{Id:BlockDeviceMappings[0].Ebs.SnapshotId}' | jq .Id -r)

And then, I check if the result of the "packer build ..." command is 0, then I delete previous snapshot by ID.

packer build -var "source_ami=${latestCentOS}" -var "region=${REGION}" -var-file=variables.json ami-centos-with-product.json
  
  if [ "$?" = "0" ] && [ "$previousSnapshotId" != "null" ]; then
    echo "Deleting previous AMI snapshot. Id: ${previousSnapshotId}"
    aws ec2 delete-snapshot --region ${REGION} --snapshot-id ${previousSnapshotId}
  fi

Let me know if anyone has any better suggestion of doing it.
Reply all
Reply to author
Forward
0 new messages