From c585c5960251f300ecf691b43131a600339a1b47 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Thu, 23 May 2024 15:09:59 -0400 Subject: [PATCH] common: move deprecation_time to AMIConfig Since the depreciation_time attribute implies being able to deprecate an AMI after a specific date, it should apply to any builder able to produce AMIs, that is everything but ebsvolume. So this commit moves that to common, so all the builders (ebs, ebssurrogate, chroot and instance) are able to support it. --- .web-docs/components/builder/chroot/README.md | 4 + .web-docs/components/builder/ebs/README.md | 8 +- .../components/builder/ebssurrogate/README.md | 4 + .../components/builder/instance/README.md | 4 + builder/chroot/builder.go | 4 + builder/chroot/builder.hcl2spec.go | 2 + builder/common/ami_config.go | 13 +++ .../step_enable_deprecation.go | 13 ++- builder/ebs/builder.go | 13 +-- builder/ebs/builder.hcl2spec.go | 4 +- builder/ebssurrogate/builder.go | 4 + builder/ebssurrogate/builder.hcl2spec.go | 2 + builder/ebssurrogate/builder_acc_test.go | 95 +++++++++++++++++++ builder/instance/builder.go | 4 + builder/instance/builder.hcl2spec.go | 2 + .../builder/common/AMIConfig-not-required.mdx | 4 + .../builder/ebs/Config-not-required.mdx | 4 - 17 files changed, 155 insertions(+), 29 deletions(-) rename builder/{ebs => common}/step_enable_deprecation.go (81%) diff --git a/.web-docs/components/builder/chroot/README.md b/.web-docs/components/builder/chroot/README.md index 4dc614fd7..654b99a13 100644 --- a/.web-docs/components/builder/chroot/README.md +++ b/.web-docs/components/builder/chroot/README.md @@ -387,6 +387,10 @@ builders. [IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for more information. Defaults to legacy. +- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ. + If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute. + You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now. + diff --git a/.web-docs/components/builder/ebs/README.md b/.web-docs/components/builder/ebs/README.md index cc6a8b4d6..abd1386e8 100644 --- a/.web-docs/components/builder/ebs/README.md +++ b/.web-docs/components/builder/ebs/README.md @@ -77,10 +77,6 @@ necessary for this build to succeed and can be found further down the page. make sure you don't set this for *nix guests; behavior may be unpredictable. -- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ. - If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute. - You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now. - - `fast_launch` (FastLaunchConfig) - The configuration for fast launch support. Fast launch is only relevant for Windows AMIs, and should not be used @@ -242,6 +238,10 @@ necessary for this build to succeed and can be found further down the page. [IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for more information. Defaults to legacy. +- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ. + If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute. + You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now. + diff --git a/.web-docs/components/builder/ebssurrogate/README.md b/.web-docs/components/builder/ebssurrogate/README.md index fbd29b5e9..1c515997c 100644 --- a/.web-docs/components/builder/ebssurrogate/README.md +++ b/.web-docs/components/builder/ebssurrogate/README.md @@ -249,6 +249,10 @@ necessary for this build to succeed and can be found further down the page. [IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for more information. Defaults to legacy. +- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ. + If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute. + You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now. + diff --git a/.web-docs/components/builder/instance/README.md b/.web-docs/components/builder/instance/README.md index 2b5722f66..b470612ad 100644 --- a/.web-docs/components/builder/instance/README.md +++ b/.web-docs/components/builder/instance/README.md @@ -261,6 +261,10 @@ necessary for this build to succeed and can be found further down the page. [IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for more information. Defaults to legacy. +- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ. + If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute. + You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now. + diff --git a/builder/chroot/builder.go b/builder/chroot/builder.go index ba8150eae..5113afab4 100644 --- a/builder/chroot/builder.go +++ b/builder/chroot/builder.go @@ -531,6 +531,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) Name: b.config.AMIName, OriginalRegion: *ec2conn.Config.Region, }, + &awscommon.StepEnableDeprecation{ + AccessConfig: &b.config.AccessConfig, + DeprecationTime: b.config.DeprecationTime, + }, &awscommon.StepModifyAMIAttributes{ Description: b.config.AMIDescription, Users: b.config.AMIUsers, diff --git a/builder/chroot/builder.hcl2spec.go b/builder/chroot/builder.hcl2spec.go index e4d9139d1..1efd7075d 100644 --- a/builder/chroot/builder.hcl2spec.go +++ b/builder/chroot/builder.hcl2spec.go @@ -41,6 +41,7 @@ type FlatConfig struct { AMIRegionKMSKeyIDs map[string]string `mapstructure:"region_kms_key_ids" required:"false" cty:"region_kms_key_ids" hcl:"region_kms_key_ids"` AMISkipBuildRegion *bool `mapstructure:"skip_save_build_region" cty:"skip_save_build_region" hcl:"skip_save_build_region"` AMIIMDSSupport *string `mapstructure:"imds_support" required:"false" cty:"imds_support" hcl:"imds_support"` + DeprecationTime *string `mapstructure:"deprecate_at" cty:"deprecate_at" hcl:"deprecate_at"` SnapshotTags map[string]string `mapstructure:"snapshot_tags" required:"false" cty:"snapshot_tags" hcl:"snapshot_tags"` SnapshotTag []config.FlatKeyValue `mapstructure:"snapshot_tag" required:"false" cty:"snapshot_tag" hcl:"snapshot_tag"` SnapshotUsers []string `mapstructure:"snapshot_users" required:"false" cty:"snapshot_users" hcl:"snapshot_users"` @@ -129,6 +130,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "region_kms_key_ids": &hcldec.AttrSpec{Name: "region_kms_key_ids", Type: cty.Map(cty.String), Required: false}, "skip_save_build_region": &hcldec.AttrSpec{Name: "skip_save_build_region", Type: cty.Bool, Required: false}, "imds_support": &hcldec.AttrSpec{Name: "imds_support", Type: cty.String, Required: false}, + "deprecate_at": &hcldec.AttrSpec{Name: "deprecate_at", Type: cty.String, Required: false}, "snapshot_tags": &hcldec.AttrSpec{Name: "snapshot_tags", Type: cty.Map(cty.String), Required: false}, "snapshot_tag": &hcldec.BlockListSpec{TypeName: "snapshot_tag", Nested: hcldec.ObjectSpec((*config.FlatKeyValue)(nil).HCL2Spec())}, "snapshot_users": &hcldec.AttrSpec{Name: "snapshot_users", Type: cty.List(cty.String), Required: false}, diff --git a/builder/common/ami_config.go b/builder/common/ami_config.go index ea010b3d1..9d38cce5c 100644 --- a/builder/common/ami_config.go +++ b/builder/common/ami_config.go @@ -9,6 +9,7 @@ import ( "fmt" "log" "regexp" + "time" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/packer-plugin-sdk/template/config" @@ -155,6 +156,10 @@ type AMIConfig struct { // [IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) // for more information. Defaults to legacy. AMIIMDSSupport string `mapstructure:"imds_support" required:"false"` + // The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ. + // If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute. + // You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now. + DeprecationTime string `mapstructure:"deprecate_at"` SnapshotConfig `mapstructure:",squash"` } @@ -260,6 +265,14 @@ func (c *AMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context ) } + if c.DeprecationTime != "" { + if _, err := time.Parse(time.RFC3339, c.DeprecationTime); err != nil { + errs = append(errs, fmt.Errorf( + "deprecate_at is not a valid time: %q. Expect time format: YYYY-MM-DDTHH:MM:SSZ", + c.DeprecationTime)) + } + } + if len(errs) > 0 { return errs } diff --git a/builder/ebs/step_enable_deprecation.go b/builder/common/step_enable_deprecation.go similarity index 81% rename from builder/ebs/step_enable_deprecation.go rename to builder/common/step_enable_deprecation.go index 9b923bba2..7d7dfe84a 100644 --- a/builder/ebs/step_enable_deprecation.go +++ b/builder/common/step_enable_deprecation.go @@ -1,7 +1,7 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -package ebs +package common import ( "context" @@ -9,18 +9,17 @@ import ( "time" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/hashicorp/packer-plugin-amazon/builder/common" "github.com/hashicorp/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" ) -type stepEnableDeprecation struct { - AccessConfig *common.AccessConfig +type StepEnableDeprecation struct { + AccessConfig *AccessConfig DeprecationTime string AMISkipCreateImage bool } -func (s *stepEnableDeprecation) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { +func (s *StepEnableDeprecation) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packersdk.Ui) if s.AMISkipCreateImage || s.DeprecationTime == "" { ui.Say("Skipping Enable AMI deprecation...") @@ -39,7 +38,7 @@ func (s *stepEnableDeprecation) Run(ctx context.Context, state multistep.StateBa for region, ami := range amis { ui.Say(fmt.Sprintf("Enabling deprecation on AMI (%s) in region %q ...", ami, region)) - conn, err := common.GetRegionConn(s.AccessConfig, region) + conn, err := GetRegionConn(s.AccessConfig, region) if err != nil { err := fmt.Errorf("failed to connect to region %s: %s", region, err) state.Put("error", err.Error()) @@ -60,6 +59,6 @@ func (s *stepEnableDeprecation) Run(ctx context.Context, state multistep.StateBa } return multistep.ActionContinue } -func (s *stepEnableDeprecation) Cleanup(state multistep.StateBag) { +func (s *StepEnableDeprecation) Cleanup(state multistep.StateBag) { // No cleanup... } diff --git a/builder/ebs/builder.go b/builder/ebs/builder.go index b6c1da1b3..85367703f 100644 --- a/builder/ebs/builder.go +++ b/builder/ebs/builder.go @@ -14,7 +14,6 @@ package ebs import ( "context" "fmt" - "time" "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/iam" @@ -78,10 +77,6 @@ type Config struct { // make sure you don't set this for *nix guests; behavior may be // unpredictable. NoEphemeral bool `mapstructure:"no_ephemeral" required:"false"` - // The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ. - // If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute. - // You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now. - DeprecationTime string `mapstructure:"deprecate_at"` // The configuration for fast launch support. // // Fast launch is only relevant for Windows AMIs, and should not be used @@ -171,12 +166,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { "you use an AMI that already has either SR-IOV or ENA enabled.")) } - if b.config.DeprecationTime != "" { - if _, err := time.Parse(time.RFC3339, b.config.DeprecationTime); err != nil { - errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("deprecate_at is not a valid time. Expect time format: YYYY-MM-DDTHH:MM:SSZ")) - } - } - if b.config.RunConfig.SpotPriceAutoProduct != "" { warns = append(warns, "spot_price_auto_product is deprecated and no "+ "longer necessary for Packer builds. In future versions of "+ @@ -446,7 +435,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) EnableFastLaunch: b.config.FastLaunch.UseFastLaunch, MaxInstances: b.config.FastLaunch.MaxParallelLaunches, }, - &stepEnableDeprecation{ + &awscommon.StepEnableDeprecation{ AccessConfig: &b.config.AccessConfig, DeprecationTime: b.config.DeprecationTime, AMISkipCreateImage: b.config.AMISkipCreateImage, diff --git a/builder/ebs/builder.hcl2spec.go b/builder/ebs/builder.hcl2spec.go index 0fe91903b..710e365fa 100644 --- a/builder/ebs/builder.hcl2spec.go +++ b/builder/ebs/builder.hcl2spec.go @@ -57,6 +57,7 @@ type FlatConfig struct { AMIRegionKMSKeyIDs map[string]string `mapstructure:"region_kms_key_ids" required:"false" cty:"region_kms_key_ids" hcl:"region_kms_key_ids"` AMISkipBuildRegion *bool `mapstructure:"skip_save_build_region" cty:"skip_save_build_region" hcl:"skip_save_build_region"` AMIIMDSSupport *string `mapstructure:"imds_support" required:"false" cty:"imds_support" hcl:"imds_support"` + DeprecationTime *string `mapstructure:"deprecate_at" cty:"deprecate_at" hcl:"deprecate_at"` SnapshotTags map[string]string `mapstructure:"snapshot_tags" required:"false" cty:"snapshot_tags" hcl:"snapshot_tags"` SnapshotTag []config.FlatKeyValue `mapstructure:"snapshot_tag" required:"false" cty:"snapshot_tag" hcl:"snapshot_tag"` SnapshotUsers []string `mapstructure:"snapshot_users" required:"false" cty:"snapshot_users" hcl:"snapshot_users"` @@ -162,7 +163,6 @@ type FlatConfig struct { VolumeRunTags map[string]string `mapstructure:"run_volume_tags" cty:"run_volume_tags" hcl:"run_volume_tags"` VolumeRunTag []config.FlatNameValue `mapstructure:"run_volume_tag" required:"false" cty:"run_volume_tag" hcl:"run_volume_tag"` NoEphemeral *bool `mapstructure:"no_ephemeral" required:"false" cty:"no_ephemeral" hcl:"no_ephemeral"` - DeprecationTime *string `mapstructure:"deprecate_at" cty:"deprecate_at" hcl:"deprecate_at"` FastLaunch *FlatFastLaunchConfig `mapstructure:"fast_launch" required:"false" cty:"fast_launch" hcl:"fast_launch"` } @@ -223,6 +223,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "region_kms_key_ids": &hcldec.AttrSpec{Name: "region_kms_key_ids", Type: cty.Map(cty.String), Required: false}, "skip_save_build_region": &hcldec.AttrSpec{Name: "skip_save_build_region", Type: cty.Bool, Required: false}, "imds_support": &hcldec.AttrSpec{Name: "imds_support", Type: cty.String, Required: false}, + "deprecate_at": &hcldec.AttrSpec{Name: "deprecate_at", Type: cty.String, Required: false}, "snapshot_tags": &hcldec.AttrSpec{Name: "snapshot_tags", Type: cty.Map(cty.String), Required: false}, "snapshot_tag": &hcldec.BlockListSpec{TypeName: "snapshot_tag", Nested: hcldec.ObjectSpec((*config.FlatKeyValue)(nil).HCL2Spec())}, "snapshot_users": &hcldec.AttrSpec{Name: "snapshot_users", Type: cty.List(cty.String), Required: false}, @@ -328,7 +329,6 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "run_volume_tags": &hcldec.AttrSpec{Name: "run_volume_tags", Type: cty.Map(cty.String), Required: false}, "run_volume_tag": &hcldec.BlockListSpec{TypeName: "run_volume_tag", Nested: hcldec.ObjectSpec((*config.FlatNameValue)(nil).HCL2Spec())}, "no_ephemeral": &hcldec.AttrSpec{Name: "no_ephemeral", Type: cty.Bool, Required: false}, - "deprecate_at": &hcldec.AttrSpec{Name: "deprecate_at", Type: cty.String, Required: false}, "fast_launch": &hcldec.BlockSpec{TypeName: "fast_launch", Nested: hcldec.ObjectSpec((*FlatFastLaunchConfig)(nil).HCL2Spec())}, } return s diff --git a/builder/ebssurrogate/builder.go b/builder/ebssurrogate/builder.go index c35945def..123b451fb 100644 --- a/builder/ebssurrogate/builder.go +++ b/builder/ebssurrogate/builder.go @@ -494,6 +494,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) OriginalRegion: *ec2conn.Config.Region, AMISkipBuildRegion: b.config.AMISkipBuildRegion, }, + &awscommon.StepEnableDeprecation{ + AccessConfig: &b.config.AccessConfig, + DeprecationTime: b.config.DeprecationTime, + }, &awscommon.StepModifyAMIAttributes{ Description: b.config.AMIDescription, Users: b.config.AMIUsers, diff --git a/builder/ebssurrogate/builder.hcl2spec.go b/builder/ebssurrogate/builder.hcl2spec.go index 3031fbb49..b4b4cf148 100644 --- a/builder/ebssurrogate/builder.hcl2spec.go +++ b/builder/ebssurrogate/builder.hcl2spec.go @@ -197,6 +197,7 @@ type FlatConfig struct { AMIRegionKMSKeyIDs map[string]string `mapstructure:"region_kms_key_ids" required:"false" cty:"region_kms_key_ids" hcl:"region_kms_key_ids"` AMISkipBuildRegion *bool `mapstructure:"skip_save_build_region" cty:"skip_save_build_region" hcl:"skip_save_build_region"` AMIIMDSSupport *string `mapstructure:"imds_support" required:"false" cty:"imds_support" hcl:"imds_support"` + DeprecationTime *string `mapstructure:"deprecate_at" cty:"deprecate_at" hcl:"deprecate_at"` SnapshotTags map[string]string `mapstructure:"snapshot_tags" required:"false" cty:"snapshot_tags" hcl:"snapshot_tags"` SnapshotTag []config.FlatKeyValue `mapstructure:"snapshot_tag" required:"false" cty:"snapshot_tag" hcl:"snapshot_tag"` SnapshotUsers []string `mapstructure:"snapshot_users" required:"false" cty:"snapshot_users" hcl:"snapshot_users"` @@ -365,6 +366,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "region_kms_key_ids": &hcldec.AttrSpec{Name: "region_kms_key_ids", Type: cty.Map(cty.String), Required: false}, "skip_save_build_region": &hcldec.AttrSpec{Name: "skip_save_build_region", Type: cty.Bool, Required: false}, "imds_support": &hcldec.AttrSpec{Name: "imds_support", Type: cty.String, Required: false}, + "deprecate_at": &hcldec.AttrSpec{Name: "deprecate_at", Type: cty.String, Required: false}, "snapshot_tags": &hcldec.AttrSpec{Name: "snapshot_tags", Type: cty.Map(cty.String), Required: false}, "snapshot_tag": &hcldec.BlockListSpec{TypeName: "snapshot_tag", Nested: hcldec.ObjectSpec((*config.FlatKeyValue)(nil).HCL2Spec())}, "snapshot_users": &hcldec.AttrSpec{Name: "snapshot_users", Type: cty.List(cty.String), Required: false}, diff --git a/builder/ebssurrogate/builder_acc_test.go b/builder/ebssurrogate/builder_acc_test.go index e277726ce..e123c1332 100644 --- a/builder/ebssurrogate/builder_acc_test.go +++ b/builder/ebssurrogate/builder_acc_test.go @@ -10,10 +10,23 @@ import ( "testing" "time" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/packer-plugin-amazon/builder/common" amazon_acc "github.com/hashicorp/packer-plugin-amazon/builder/ebs/acceptance" "github.com/hashicorp/packer-plugin-sdk/acctest" ) +func testEC2Conn(region string) (*ec2.EC2, error) { + access := &common.AccessConfig{RawRegion: region} + session, err := access.Session() + if err != nil { + return nil, err + } + + return ec2.New(session), nil +} + func TestAccBuilder_EbssurrogateBasic(t *testing.T) { ami := amazon_acc.AMIHelper{ Region: "us-east-1", @@ -178,6 +191,59 @@ func TestAccBuilder_EbssurrogateUseCreateImageOptional(t *testing.T) { acctest.TestPlugin(t, testCase) } +func TestAccBuilder_EbssurrogateWithAMIDeprecate(t *testing.T) { + ami := amazon_acc.AMIHelper{ + Region: "us-east-1", + Name: fmt.Sprintf("ebssurrogate-deprecate-at-acctest-%d", time.Now().Unix()), + } + testCase := &acctest.PluginTestCase{ + Name: "ebssurrogate - deprecate at set", + Template: fmt.Sprintf(testBuilderAcc_WithDeprecateAt, ami.Name, time.Now().Add(time.Hour).UTC().Format("2006-01-02T15:04:05Z")), + Teardown: func() error { + return ami.CleanUpAmi() + }, + Check: func(buildCommand *exec.Cmd, logfile string) error { + if buildCommand.ProcessState != nil { + if buildCommand.ProcessState.ExitCode() != 0 { + return fmt.Errorf("Bad exit code. Logfile: %s", logfile) + } + + conn, err := testEC2Conn("us-east-1") + if err != nil { + return fmt.Errorf("failed to get connection to us-east-1: %s", err) + } + + out, err := conn.DescribeImages(&ec2.DescribeImagesInput{ + Filters: []*ec2.Filter{{ + Name: aws.String("name"), + Values: []*string{&ami.Name}, + }}, + }) + if err != nil { + return fmt.Errorf("unable to describe images: %s", err) + } + + if len(out.Images) != 1 { + return fmt.Errorf("got %d images, should have been one", len(out.Images)) + } + + img := out.Images[0] + if img.DeprecationTime == nil { + return fmt.Errorf("no depreciation time set for image %s", ami.Name) + } + + if *img.DeprecationTime == "" { + return fmt.Errorf("no depreciation time set for image %s", ami.Name) + } + + return nil + } + return nil + }, + } + acctest.TestPlugin(t, testCase) +} + const testBuilderAccBasic = ` source "amazon-ebssurrogate" "test" { ami_name = "%s" @@ -352,3 +418,32 @@ build { sources = ["amazon-ebssurrogate.test"] } ` + +const testBuilderAcc_WithDeprecateAt = ` +source "amazon-ebssurrogate" "test" { + ami_name = "%s" + region = "us-east-1" + instance_type = "m3.medium" + source_ami = "ami-76b2a71e" + ssh_username = "ubuntu" + launch_block_device_mappings { + device_name = "/dev/xvda" + delete_on_termination = true + volume_size = 8 + volume_type = "gp2" + } + ami_virtualization_type = "hvm" + ami_root_device { + source_device_name = "/dev/xvda" + device_name = "/dev/sda1" + delete_on_termination = true + volume_size = 8 + volume_type = "gp2" + } + deprecate_at = "%s" +} + +build { + sources = ["amazon-ebssurrogate.test"] +} +` diff --git a/builder/instance/builder.go b/builder/instance/builder.go index a018cd28f..94da5624b 100644 --- a/builder/instance/builder.go +++ b/builder/instance/builder.go @@ -456,6 +456,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) Name: b.config.AMIName, OriginalRegion: *ec2conn.Config.Region, }, + &awscommon.StepEnableDeprecation{ + AccessConfig: &b.config.AccessConfig, + DeprecationTime: b.config.DeprecationTime, + }, &awscommon.StepModifyAMIAttributes{ Description: b.config.AMIDescription, Users: b.config.AMIUsers, diff --git a/builder/instance/builder.hcl2spec.go b/builder/instance/builder.hcl2spec.go index f10374475..9816d1fb4 100644 --- a/builder/instance/builder.hcl2spec.go +++ b/builder/instance/builder.hcl2spec.go @@ -57,6 +57,7 @@ type FlatConfig struct { AMIRegionKMSKeyIDs map[string]string `mapstructure:"region_kms_key_ids" required:"false" cty:"region_kms_key_ids" hcl:"region_kms_key_ids"` AMISkipBuildRegion *bool `mapstructure:"skip_save_build_region" cty:"skip_save_build_region" hcl:"skip_save_build_region"` AMIIMDSSupport *string `mapstructure:"imds_support" required:"false" cty:"imds_support" hcl:"imds_support"` + DeprecationTime *string `mapstructure:"deprecate_at" cty:"deprecate_at" hcl:"deprecate_at"` SnapshotTags map[string]string `mapstructure:"snapshot_tags" required:"false" cty:"snapshot_tags" hcl:"snapshot_tags"` SnapshotTag []config.FlatKeyValue `mapstructure:"snapshot_tag" required:"false" cty:"snapshot_tag" hcl:"snapshot_tag"` SnapshotUsers []string `mapstructure:"snapshot_users" required:"false" cty:"snapshot_users" hcl:"snapshot_users"` @@ -227,6 +228,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "region_kms_key_ids": &hcldec.AttrSpec{Name: "region_kms_key_ids", Type: cty.Map(cty.String), Required: false}, "skip_save_build_region": &hcldec.AttrSpec{Name: "skip_save_build_region", Type: cty.Bool, Required: false}, "imds_support": &hcldec.AttrSpec{Name: "imds_support", Type: cty.String, Required: false}, + "deprecate_at": &hcldec.AttrSpec{Name: "deprecate_at", Type: cty.String, Required: false}, "snapshot_tags": &hcldec.AttrSpec{Name: "snapshot_tags", Type: cty.Map(cty.String), Required: false}, "snapshot_tag": &hcldec.BlockListSpec{TypeName: "snapshot_tag", Nested: hcldec.ObjectSpec((*config.FlatKeyValue)(nil).HCL2Spec())}, "snapshot_users": &hcldec.AttrSpec{Name: "snapshot_users", Type: cty.List(cty.String), Required: false}, diff --git a/docs-partials/builder/common/AMIConfig-not-required.mdx b/docs-partials/builder/common/AMIConfig-not-required.mdx index 645214cad..580414c4f 100644 --- a/docs-partials/builder/common/AMIConfig-not-required.mdx +++ b/docs-partials/builder/common/AMIConfig-not-required.mdx @@ -133,4 +133,8 @@ [IMDS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for more information. Defaults to legacy. +- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ. + If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute. + You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now. + diff --git a/docs-partials/builder/ebs/Config-not-required.mdx b/docs-partials/builder/ebs/Config-not-required.mdx index fc309d2f6..a92a726cc 100644 --- a/docs-partials/builder/ebs/Config-not-required.mdx +++ b/docs-partials/builder/ebs/Config-not-required.mdx @@ -40,10 +40,6 @@ make sure you don't set this for *nix guests; behavior may be unpredictable. -- `deprecate_at` (string) - The date and time to deprecate the AMI, in UTC, in the following format: YYYY-MM-DDTHH:MM:SSZ. - If you specify a value for seconds, Amazon EC2 rounds the seconds to the nearest minute. - You can’t specify a date in the past. The upper limit for DeprecateAt is 10 years from now. - - `fast_launch` (FastLaunchConfig) - The configuration for fast launch support. Fast launch is only relevant for Windows AMIs, and should not be used