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.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/packer-tool/15495fe0-26c6-41b3-b79a-5e9e5f99a681%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.