Skip to content

Commit

Permalink
resource/aws_launch_configuration: Fix regression from version 2.22.0…
Browse files Browse the repository at this point in the history
… with instance store AMIs returning an unexpected error (#9810)

New test prior to fix:

```
--- FAIL: TestAccAWSLaunchConfiguration_withInstanceStoreAMI (13.84s)
    testing.go:640: Step 0 error: errors during apply:

        Error: Instance store backed AMIs do not provide a root device name - Use an EBS AMI
```

Output from acceptance testing:

```
--- PASS: TestAccAWSLaunchConfiguration_withSpotPrice (19.05s)
--- PASS: TestAccAWSLaunchConfiguration_withInstanceStoreAMI (20.40s)
--- PASS: TestAccAWSLaunchConfiguration_withBlockDevices (20.90s)
--- PASS: TestAccAWSLaunchConfiguration_withEncryption (21.66s)
--- PASS: TestAccAWSLaunchConfiguration_ebs_noDevice (21.67s)
--- PASS: TestAccAWSLaunchConfiguration_withIAMProfile (29.41s)
--- PASS: TestAccAWSLaunchConfiguration_encryptedRootBlockDevice (34.32s)
--- PASS: TestAccAWSLaunchConfiguration_basic (35.35s)
--- PASS: TestAccAWSLaunchConfiguration_userData (37.26s)
--- PASS: TestAccAWSLaunchConfiguration_updateEbsBlockDevices (37.48s)
--- PASS: TestAccAWSLaunchConfiguration_updateRootBlockDevice (39.25s)
--- PASS: TestAccAWSLaunchConfiguration_withVpcClassicLink (39.58s)
```
  • Loading branch information
jc-asdf committed Feb 12, 2020
1 parent 678636a commit d59c9f3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
23 changes: 14 additions & 9 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ func fetchRootDeviceName(ami string, conn *ec2.EC2) (*string, error) {

// Instance store backed AMIs do not provide a root device name.
if *image.RootDeviceType == ec2.DeviceTypeInstanceStore {
return nil, fmt.Errorf("Instance store backed AMIs do not provide a root device name - Use an EBS AMI")
return nil, nil
}

// Some AMIs have a RootDeviceName like "/dev/sda1" that does not appear as a
Expand Down Expand Up @@ -1642,15 +1642,20 @@ func readBlockDeviceMappingsFromConfig(
log.Print("[WARN] IOPs is only valid for storate type io1 for EBS Volumes")
}

dn, err := fetchRootDeviceName(d.Get("ami").(string), conn)
if err != nil {
return nil, fmt.Errorf("Expected 1 AMI for ID: %s (%s)", d.Get("ami").(string), err)
}
if dn, err := fetchRootDeviceName(d.Get("ami").(string), conn); err == nil {
if dn == nil {
return nil, fmt.Errorf(
"Expected 1 AMI for ID: %s, got none",
d.Get("ami").(string))
}

blockDevices = append(blockDevices, &ec2.BlockDeviceMapping{
DeviceName: dn,
Ebs: ebs,
})
blockDevices = append(blockDevices, &ec2.BlockDeviceMapping{
DeviceName: dn,
Ebs: ebs,
})
} else {
return nil, err
}
}
}

Expand Down
48 changes: 48 additions & 0 deletions aws/resource_aws_launch_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ func TestAccAWSLaunchConfiguration_withBlockDevices(t *testing.T) {
})
}

func TestAccAWSLaunchConfiguration_withInstanceStoreAMI(t *testing.T) {
var conf autoscaling.LaunchConfiguration
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationConfigWithInstanceStoreAMI(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
),
},
},
})
}

func TestAccAWSLaunchConfiguration_updateRootBlockDevice(t *testing.T) {
var conf autoscaling.LaunchConfiguration
resourceName := "aws_launch_configuration.test"
Expand Down Expand Up @@ -549,6 +568,35 @@ data "aws_ami" "ubuntu" {
`)
}

func testAccAWSLaunchConfigurationConfig_instanceStoreAMI() string {
return fmt.Sprintf(`
data "aws_ami" "ubuntu_instance_store" {
most_recent = true
owners = ["099720109477"] # Canonical
# Latest Ubuntu 18.04 LTS amd64 instance-store HVM AMI
filter {
name = "name"
values = ["ubuntu/images/hvm-instance/ubuntu-bionic-18.04-amd64-server*"]
}
}
`)
}

func testAccAWSLaunchConfigurationConfigWithInstanceStoreAMI(rInt int) string {
return testAccAWSLaunchConfigurationConfig_instanceStoreAMI() + fmt.Sprintf(`
resource "aws_launch_configuration" "bar" {
name_prefix = "tf-acc-test-%d"
image_id = "${data.aws_ami.ubuntu_instance_store.id}"
# When the instance type is updated, the new type must support ephemeral storage.
instance_type = "m1.small"
user_data = "foobar-user-data"
associate_public_ip_address = false
}
`, rInt)
}

func testAccAWSLaunchConfigurationConfigWithRootBlockDevice(rInt int) string {
return testAccAWSLaunchConfigurationConfig_ami() + fmt.Sprintf(`
resource "aws_launch_configuration" "test" {
Expand Down

0 comments on commit d59c9f3

Please sign in to comment.