From 9216b97367ba6f34081b3bbe2a9a76cdd083f355 Mon Sep 17 00:00:00 2001 From: Ilia Lazebnik Date: Thu, 25 Mar 2021 21:59:45 +0200 Subject: [PATCH] resource/aws_ami_from_instance: Tag on create (#17968) Output from acceptance testing in AWS Commercial: ``` --- PASS: TestAccAWSAMIFromInstance_disappears (259.64s) --- PASS: TestAccAWSAMIFromInstance_basic (276.96s) --- PASS: TestAccAWSAMIFromInstance_tags (309.87s) ``` Output from acceptance testing in AWS GovCloud (US): ``` --- PASS: TestAccAWSAMIFromInstance_disappears (289.51s) --- PASS: TestAccAWSAMIFromInstance_basic (307.13s) --- PASS: TestAccAWSAMIFromInstance_tags (356.92s) ``` --- .changelog/17968.txt | 3 +++ aws/resource_aws_ami_from_instance.go | 16 +++++---------- aws/resource_aws_ami_from_instance_test.go | 24 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 .changelog/17968.txt diff --git a/.changelog/17968.txt b/.changelog/17968.txt new file mode 100644 index 00000000000..daf69ea76e6 --- /dev/null +++ b/.changelog/17968.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_ami_from_instance: Tag on create. +``` \ No newline at end of file diff --git a/aws/resource_aws_ami_from_instance.go b/aws/resource_aws_ami_from_instance.go index 693b3069e31..f5da16dd874 100644 --- a/aws/resource_aws_ami_from_instance.go +++ b/aws/resource_aws_ami_from_instance.go @@ -8,7 +8,6 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) func resourceAwsAmiFromInstance() *schema.Resource { @@ -223,10 +222,11 @@ func resourceAwsAmiFromInstanceCreate(d *schema.ResourceData, meta interface{}) client := meta.(*AWSClient).ec2conn req := &ec2.CreateImageInput{ - Description: aws.String(d.Get("description").(string)), - InstanceId: aws.String(d.Get("source_instance_id").(string)), - Name: aws.String(d.Get("name").(string)), - NoReboot: aws.Bool(d.Get("snapshot_without_reboot").(bool)), + Description: aws.String(d.Get("description").(string)), + InstanceId: aws.String(d.Get("source_instance_id").(string)), + Name: aws.String(d.Get("name").(string)), + NoReboot: aws.Bool(d.Get("snapshot_without_reboot").(bool)), + TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeImage), } res, err := client.CreateImage(req) @@ -237,12 +237,6 @@ func resourceAwsAmiFromInstanceCreate(d *schema.ResourceData, meta interface{}) d.SetId(aws.StringValue(res.ImageId)) d.Set("manage_ebs_snapshots", true) - if v := d.Get("tags").(map[string]interface{}); len(v) > 0 { - if err := keyvaluetags.Ec2CreateTags(client, d.Id(), v); err != nil { - return fmt.Errorf("error adding tags: %s", err) - } - } - _, err = resourceAwsAmiWaitForAvailable(d.Timeout(schema.TimeoutCreate), d.Id(), client) if err != nil { return err diff --git a/aws/resource_aws_ami_from_instance_test.go b/aws/resource_aws_ami_from_instance_test.go index 3a35cfdc919..98cc9c5a9ed 100644 --- a/aws/resource_aws_ami_from_instance_test.go +++ b/aws/resource_aws_ami_from_instance_test.go @@ -34,6 +34,7 @@ func TestAccAWSAMIFromInstance_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "image_type", "machine"), resource.TestCheckResourceAttr(resourceName, "hypervisor", "xen"), testAccCheckResourceAttrAccountID(resourceName, "owner_id"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), ), }, }, @@ -80,6 +81,29 @@ func TestAccAWSAMIFromInstance_tags(t *testing.T) { }) } +func TestAccAWSAMIFromInstance_disappears(t *testing.T) { + var image ec2.Image + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_ami_from_instance.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAMIFromInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAMIFromInstanceConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAMIFromInstanceExists(resourceName, &image), + testAccCheckResourceDisappears(testAccProvider, resourceAwsAmiFromInstance(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccCheckAWSAMIFromInstanceExists(resourceName string, image *ec2.Image) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName]