Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_launch_template: Prevent crashes with empty configuration blocks for top-level attributes #7134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,9 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate
bdms := v.([]interface{})

for _, bdm := range bdms {
if bdm == nil {
continue
}
blockDeviceMapping, err := readBlockDeviceMappingFromConfig(bdm.(map[string]interface{}))
if err != nil {
return nil, err
Expand All @@ -1020,15 +1023,15 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate
if v, ok := d.GetOk("capacity_reservation_specification"); ok {
crs := v.([]interface{})

if len(crs) > 0 {
if len(crs) > 0 && crs[0] != nil {
opts.CapacityReservationSpecification = readCapacityReservationSpecificationFromConfig(crs[0].(map[string]interface{}))
}
}

if v, ok := d.GetOk("credit_specification"); ok && (strings.HasPrefix(instanceType, "t2") || strings.HasPrefix(instanceType, "t3")) {
cs := v.([]interface{})

if len(cs) > 0 {
if len(cs) > 0 && cs[0] != nil {
opts.CreditSpecification = readCreditSpecificationFromConfig(cs[0].(map[string]interface{}))
}
}
Expand All @@ -1046,15 +1049,15 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate
if v, ok := d.GetOk("iam_instance_profile"); ok {
iip := v.([]interface{})

if len(iip) > 0 {
if len(iip) > 0 && iip[0] != nil {
opts.IamInstanceProfile = readIamInstanceProfileFromConfig(iip[0].(map[string]interface{}))
}
}

if v, ok := d.GetOk("instance_market_options"); ok {
imo := v.([]interface{})

if len(imo) > 0 {
if len(imo) > 0 && imo[0] != nil {
instanceMarketOptions, err := readInstanceMarketOptionsFromConfig(imo[0].(map[string]interface{}))
if err != nil {
return nil, err
Expand All @@ -1068,14 +1071,17 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate
lsList := v.(*schema.Set).List()

for _, ls := range lsList {
if ls == nil {
continue
}
licenseSpecifications = append(licenseSpecifications, readLicenseSpecificationFromConfig(ls.(map[string]interface{})))
}
opts.LicenseSpecifications = licenseSpecifications
}

if v, ok := d.GetOk("monitoring"); ok {
m := v.([]interface{})
if len(m) > 0 {
if len(m) > 0 && m[0] != nil {
mData := m[0].(map[string]interface{})
monitoring := &ec2.LaunchTemplatesMonitoringRequest{
Enabled: aws.Bool(mData["enabled"].(bool)),
Expand All @@ -1089,6 +1095,9 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate
niList := v.([]interface{})

for _, ni := range niList {
if ni == nil {
continue
}
niData := ni.(map[string]interface{})
networkInterface := readNetworkInterfacesFromConfig(niData)
networkInterfaces = append(networkInterfaces, networkInterface)
Expand All @@ -1099,7 +1108,7 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate
if v, ok := d.GetOk("placement"); ok {
p := v.([]interface{})

if len(p) > 0 {
if len(p) > 0 && p[0] != nil {
opts.Placement = readPlacementFromConfig(p[0].(map[string]interface{}))
}
}
Expand All @@ -1109,6 +1118,9 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate
t := v.([]interface{})

for _, ts := range t {
if ts == nil {
continue
}
tsData := ts.(map[string]interface{})
tags := tagsFromMap(tsData["tags"].(map[string]interface{}))
tagSpecification := &ec2.LaunchTemplateTagSpecificationRequest{
Expand Down
32 changes: 32 additions & 0 deletions aws/resource_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,28 @@ func TestAccAWSLaunchTemplate_creditSpecification_t3(t *testing.T) {
})
}

// Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/6757
func TestAccAWSLaunchTemplate_IamInstanceProfile_EmptyConfigurationBlock(t *testing.T) {
var template1 ec2.LaunchTemplate
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_launch_template.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchTemplateConfigIamInstanceProfileEmptyConfigurationBlock(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resourceName, &template1),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSLaunchTemplate_networkInterface(t *testing.T) {
var template ec2.LaunchTemplate
resName := "aws_launch_template.test"
Expand Down Expand Up @@ -880,6 +902,16 @@ resource "aws_launch_template" "foo" {
`, instanceType, rName, cpuCredits)
}

func testAccAWSLaunchTemplateConfigIamInstanceProfileEmptyConfigurationBlock(rName string) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
name = %q

iam_instance_profile {}
}
`, rName)
}

func testAccAWSLaunchTemplateConfig_licenseSpecification(rInt int) string {
return fmt.Sprintf(`
resource "aws_licensemanager_license_configuration" "example" {
Expand Down