diff --git a/builder/amazon/common/ami_config.go b/builder/amazon/common/ami_config.go index 343d7f1bd..2069ab23f 100644 --- a/builder/amazon/common/ami_config.go +++ b/builder/amazon/common/ami_config.go @@ -29,6 +29,7 @@ type AMIConfig struct { SnapshotTags TagMap `mapstructure:"snapshot_tags"` SnapshotUsers []string `mapstructure:"snapshot_users"` SnapshotGroups []string `mapstructure:"snapshot_groups"` + AMISkipBuildRegion bool `mapstructure:"skip_save_build_region"` } func stringInSlice(s []string, searchstr string) bool { diff --git a/builder/amazon/common/step_ami_region_copy.go b/builder/amazon/common/step_ami_region_copy.go index 620715d27..bfc582e5a 100644 --- a/builder/amazon/common/step_ami_region_copy.go +++ b/builder/amazon/common/step_ami_region_copy.go @@ -21,8 +21,9 @@ type StepAMIRegionCopy struct { Name string OriginalRegion string - toDelete string - getRegionConn func(*AccessConfig, string) (ec2iface.EC2API, error) + toDelete string + getRegionConn func(*AccessConfig, string) (ec2iface.EC2API, error) + AMISkipBuildRegion bool } func (s *StepAMIRegionCopy) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -33,7 +34,9 @@ func (s *StepAMIRegionCopy) Run(ctx context.Context, state multistep.StateBag) m ami := amis[s.OriginalRegion] // Always copy back into original region to preserve the ami name s.toDelete = ami - s.Regions = append(s.Regions, s.OriginalRegion) + if !s.AMISkipBuildRegion { + s.Regions = append(s.Regions, s.OriginalRegion) + } if s.EncryptBootVolume != nil && *s.EncryptBootVolume { // encrypt_boot is true, so we have to copy the temporary diff --git a/builder/amazon/common/step_pre_validate.go b/builder/amazon/common/step_pre_validate.go index f8ba32b16..592f9d539 100644 --- a/builder/amazon/common/step_pre_validate.go +++ b/builder/amazon/common/step_pre_validate.go @@ -18,8 +18,9 @@ import ( // the build before actually doing any time consuming work // type StepPreValidate struct { - DestAmiName string - ForceDeregister bool + DestAmiName string + ForceDeregister bool + AMISkipBuildRegion bool } func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -76,6 +77,10 @@ func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) mul ui.Say("Force Deregister flag found, skipping prevalidating AMI Name") return multistep.ActionContinue } + if s.AMISkipBuildRegion { + ui.Say("skip_build_region was set; not prevalidating AMI name") + return multistep.ActionContinue + } ec2conn := state.Get("ec2").(*ec2.EC2) diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index d847f9d57..fef470ef5 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -154,8 +154,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack // Build the steps steps := []multistep.Step{ &awscommon.StepPreValidate{ - DestAmiName: b.config.AMIName, - ForceDeregister: b.config.AMIForceDeregister, + DestAmiName: b.config.AMIName, + ForceDeregister: b.config.AMIForceDeregister, + AMISkipBuildRegion: b.config.AMISkipBuildRegion, }, &awscommon.StepSourceAMIInfo{ SourceAmi: b.config.SourceAmi, @@ -220,15 +221,18 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack AMIName: b.config.AMIName, Regions: b.config.AMIRegions, }, - &stepCreateAMI{}, + &stepCreateAMI{ + AMISkipBuildRegion: b.config.AMISkipBuildRegion, + }, &awscommon.StepAMIRegionCopy{ - AccessConfig: &b.config.AccessConfig, - Regions: b.config.AMIRegions, - AMIKmsKeyId: b.config.AMIKmsKeyId, - RegionKeyIds: b.config.AMIRegionKMSKeyIDs, - EncryptBootVolume: b.config.AMIEncryptBootVolume, - Name: b.config.AMIName, - OriginalRegion: *ec2conn.Config.Region, + AccessConfig: &b.config.AccessConfig, + Regions: b.config.AMIRegions, + AMIKmsKeyId: b.config.AMIKmsKeyId, + RegionKeyIds: b.config.AMIRegionKMSKeyIDs, + EncryptBootVolume: b.config.AMIEncryptBootVolume, + Name: b.config.AMIName, + OriginalRegion: *ec2conn.Config.Region, + AMISkipBuildRegion: b.config.AMISkipBuildRegion, }, &awscommon.StepModifyAMIAttributes{ Description: b.config.AMIDescription, diff --git a/builder/amazon/ebs/step_create_ami.go b/builder/amazon/ebs/step_create_ami.go index 68d8c7084..c1d0da0b8 100644 --- a/builder/amazon/ebs/step_create_ami.go +++ b/builder/amazon/ebs/step_create_ami.go @@ -14,7 +14,8 @@ import ( ) type stepCreateAMI struct { - image *ec2.Image + image *ec2.Image + AMISkipBuildRegion bool } func (s *stepCreateAMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -25,9 +26,9 @@ func (s *stepCreateAMI) Run(ctx context.Context, state multistep.StateBag) multi // Create the image amiName := config.AMIName - if config.AMIEncryptBootVolume != nil { - // encrypt_boot was set, so we will create a temporary image - // and then create a copy of it with the correct encrypt_boot + if config.AMIEncryptBootVolume != nil || s.AMISkipBuildRegion { + // Create a temporary image and then create a copy of it with the + // correct encrypt_boot amiName = random.AlphaNum(7) }