diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index f356676c3..328b17d29 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -46,6 +46,7 @@ type Config struct { SourceAmi string `mapstructure:"source_ami"` SourceAmiFilter awscommon.AmiFilterOptions `mapstructure:"source_ami_filter"` RootVolumeTags awscommon.TagMap `mapstructure:"root_volume_tags"` + Architecture string `mapstructure:"ami_architecture"` ctx interpolate.Context } @@ -81,6 +82,10 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, err } + if b.config.Architecture == "" { + b.config.Architecture == "x86_64" + } + if b.config.PackerConfig.PackerForce { b.config.AMIForceDeregister = true } @@ -180,6 +185,16 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs, errors.New("If root_device_name is specified, ami_block_device_mappings must be specified")) } } + valid := false + for validArch, _ := range []string{"x86_64", "arm64"} { + if validArch == b.config.Architecture { + matched = true + break + } + } + if !valid { + errs = packer.MultiErrorAppend(errs, errors.New(`The only valid ami_architecture values are "x86_64" and "arm64"`)) + } if errs != nil && len(errs.Errors) > 0 { return warns, errs @@ -276,6 +291,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack RootVolumeSize: b.config.RootVolumeSize, EnableAMISriovNetSupport: b.config.AMISriovNetSupport, EnableAMIENASupport: b.config.AMIENASupport, + Architecture: b.config.Architecture, }, &awscommon.StepAMIRegionCopy{ AccessConfig: &b.config.AccessConfig, diff --git a/builder/amazon/chroot/step_register_ami.go b/builder/amazon/chroot/step_register_ami.go index da60e917a..52aa8a442 100644 --- a/builder/amazon/chroot/step_register_ami.go +++ b/builder/amazon/chroot/step_register_ami.go @@ -16,6 +16,7 @@ type StepRegisterAMI struct { RootVolumeSize int64 EnableAMIENASupport *bool EnableAMISriovNetSupport bool + Architecture string } func (s *StepRegisterAMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -117,7 +118,7 @@ func buildBaseRegisterOpts(config *Config, sourceImage *ec2.Image, rootVolumeSiz if config.FromScratch { return &ec2.RegisterImageInput{ Name: &config.AMIName, - Architecture: aws.String(ec2.ArchitectureValuesX8664), + Architecture: aws.String(s.Architecture), RootDeviceName: aws.String(rootDeviceName), VirtualizationType: aws.String(config.AMIVirtType), BlockDeviceMappings: newMappings, diff --git a/builder/amazon/ebssurrogate/builder.go b/builder/amazon/ebssurrogate/builder.go index 842881246..5153577d6 100644 --- a/builder/amazon/ebssurrogate/builder.go +++ b/builder/amazon/ebssurrogate/builder.go @@ -28,6 +28,7 @@ type Config struct { RootDevice RootBlockDevice `mapstructure:"ami_root_device"` VolumeRunTags awscommon.TagMap `mapstructure:"run_volume_tags"` + Architecture string `mapstructure:"ami_architecture"` ctx interpolate.Context } @@ -92,6 +93,19 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { "you use an AMI that already has either SR-IOV or ENA enabled.")) } + if b.config.Architecture == "" { + b.config.Architecture == "x86_64" + } + valid := false + for validArch, _ := range []string{"x86_64", "arm64"} { + if validArch == b.config.Architecture { + matched = true + break + } + } + if !valid { + errs = packer.MultiErrorAppend(errs, errors.New(`The only valid ami_architecture values are "x86_64" and "arm64"`)) + } if errs != nil && len(errs.Errors) > 0 { return nil, errs } @@ -244,6 +258,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack LaunchDevices: launchDevices, EnableAMISriovNetSupport: b.config.AMISriovNetSupport, EnableAMIENASupport: b.config.AMIENASupport, + Architecture: b.config.Architecture, }, &awscommon.StepAMIRegionCopy{ AccessConfig: &b.config.AccessConfig, diff --git a/builder/amazon/ebssurrogate/step_register_ami.go b/builder/amazon/ebssurrogate/step_register_ami.go index 60b653681..13c98d864 100644 --- a/builder/amazon/ebssurrogate/step_register_ami.go +++ b/builder/amazon/ebssurrogate/step_register_ami.go @@ -18,6 +18,7 @@ type StepRegisterAMI struct { LaunchDevices []*ec2.BlockDeviceMapping EnableAMIENASupport *bool EnableAMISriovNetSupport bool + Architecture string image *ec2.Image } @@ -33,7 +34,7 @@ func (s *StepRegisterAMI) Run(ctx context.Context, state multistep.StateBag) mul registerOpts := &ec2.RegisterImageInput{ Name: &config.AMIName, - Architecture: aws.String(ec2.ArchitectureValuesX8664), + Architecture: aws.String(s.Architecture), RootDeviceName: aws.String(s.RootDevice.DeviceName), VirtualizationType: aws.String(config.AMIVirtType), BlockDeviceMappings: blockDevices, diff --git a/website/source/docs/builders/amazon-chroot.html.md.erb b/website/source/docs/builders/amazon-chroot.html.md.erb index cba9a0b1e..749fbcaeb 100644 --- a/website/source/docs/builders/amazon-chroot.html.md.erb +++ b/website/source/docs/builders/amazon-chroot.html.md.erb @@ -75,6 +75,9 @@ each category, the available configuration keys are alphabetized. ### Optional: +- `ami_architecture` (string) - what architecture to use when registering the + final AMI; valid options are "x86_64" or "arm64". Defaults to "x86_64". + - `ami_description` (string) - The description to set for the resulting AMI(s). By default this description is empty. This is a [template engine](/docs/templates/engine.html), see [Build template @@ -141,8 +144,8 @@ each category, the available configuration keys are alphabetized. documentation on enabling enhanced networking](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking.html#enabling_enhanced_networking). -- `encrypt_boot` (boolean) - Whether or not to encrypt the resulting AMI when - copying a provisioned instance to an AMI. By default, Packer will keep the +- `encrypt_boot` (boolean) - Whether or not to encrypt the resulting AMI when + copying a provisioned instance to an AMI. By default, Packer will keep the encryption setting to what it was in the source image. Setting `false` will result in an unencrypted image, and `true` will result in an encrypted one. diff --git a/website/source/docs/builders/amazon-ebssurrogate.html.md.erb b/website/source/docs/builders/amazon-ebssurrogate.html.md.erb index 40d28645a..56b3dddae 100644 --- a/website/source/docs/builders/amazon-ebssurrogate.html.md.erb +++ b/website/source/docs/builders/amazon-ebssurrogate.html.md.erb @@ -61,6 +61,9 @@ builder. ### Optional: +- `ami_architecture` (string) - what architecture to use when registering the + final AMI; valid options are "x86_64" or "arm64". Defaults to "x86_64". + - `ami_block_device_mappings` (array of block device mappings) - Add one or more [block device mappings](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html)