Merge pull request #7521 from hashicorp/fix_7514
delete unencrypted temporary snapshot when encrypt_boot is true. Also…
This commit is contained in:
commit
90227bb768
|
@ -32,6 +32,9 @@ func (s *StepAMIRegionCopy) Run(ctx context.Context, state multistep.StateBag) m
|
||||||
// AMI with required encryption setting.
|
// AMI with required encryption setting.
|
||||||
// temp image was created by stepCreateAMI.
|
// temp image was created by stepCreateAMI.
|
||||||
s.Regions = append(s.Regions, *ec2conn.Config.Region)
|
s.Regions = append(s.Regions, *ec2conn.Config.Region)
|
||||||
|
if s.RegionKeyIds == nil {
|
||||||
|
s.RegionKeyIds = make(map[string]string)
|
||||||
|
}
|
||||||
s.RegionKeyIds[*ec2conn.Config.Region] = s.AMIKmsKeyId
|
s.RegionKeyIds[*ec2conn.Config.Region] = s.AMIKmsKeyId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
awscommon "github.com/hashicorp/packer/builder/amazon/common"
|
awscommon "github.com/hashicorp/packer/builder/amazon/common"
|
||||||
"github.com/hashicorp/packer/common/random"
|
"github.com/hashicorp/packer/common/random"
|
||||||
|
@ -106,10 +107,49 @@ func (s *stepCreateAMI) Cleanup(state multistep.StateBag) {
|
||||||
ec2conn := state.Get("ec2").(*ec2.EC2)
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
ui.Say("Deregistering the AMI because cancellation, error or it was temporary (encrypt_boot was set)...")
|
ui.Say("Deregistering the AMI and deleting associated snapshots because " +
|
||||||
deregisterOpts := &ec2.DeregisterImageInput{ImageId: s.image.ImageId}
|
"of cancellation, error or it was temporary (encrypt_boot was set)...")
|
||||||
if _, err := ec2conn.DeregisterImage(deregisterOpts); err != nil {
|
|
||||||
ui.Error(fmt.Sprintf("Error deregistering AMI, may still be around: %s", err))
|
resp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{
|
||||||
|
ImageIds: []*string{s.image.ImageId},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
err := fmt.Errorf("Error describing AMI: %s", err)
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deregister image by name.
|
||||||
|
for _, i := range resp.Images {
|
||||||
|
_, err := ec2conn.DeregisterImage(&ec2.DeregisterImageInput{
|
||||||
|
ImageId: i.ImageId,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
err := fmt.Errorf("Error deregistering existing AMI: %s", err)
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ui.Say(fmt.Sprintf("Deregistered AMI id: %s", *i.ImageId))
|
||||||
|
|
||||||
|
// Delete snapshot(s) by image
|
||||||
|
for _, b := range i.BlockDeviceMappings {
|
||||||
|
if b.Ebs != nil && aws.StringValue(b.Ebs.SnapshotId) != "" {
|
||||||
|
_, err := ec2conn.DeleteSnapshot(&ec2.DeleteSnapshotInput{
|
||||||
|
SnapshotId: b.Ebs.SnapshotId,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
err := fmt.Errorf("Error deleting existing snapshot: %s", err)
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ui.Say(fmt.Sprintf("Deleted snapshot: %s", *b.Ebs.SnapshotId))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue