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_instance: Prevent panic when updating credit_specification to empty configuration block #10212

Merged
merged 1 commit into from
Sep 24, 2019
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
2 changes: 1 addition & 1 deletion aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
}

if d.HasChange("credit_specification") && !d.IsNewResource() {
if v, ok := d.GetOk("credit_specification"); ok {
if v, ok := d.GetOk("credit_specification"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
creditSpecification := v.([]interface{})[0].(map[string]interface{})
log.Printf("[DEBUG] Modifying credit specification for Instance (%s)", d.Id())
_, err := conn.ModifyInstanceCreditSpecification(&ec2.ModifyInstanceCreditSpecificationInput{
Expand Down
126 changes: 126 additions & 0 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,53 @@ func TestAccAWSInstance_getPasswordData_trueToFalse(t *testing.T) {
})
}

func TestAccAWSInstance_CreditSpecification_Empty_NonBurstable(t *testing.T) {
var instance ec2.Instance
resourceName := "aws_instance.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig_CreditSpecification_Empty_NonBurstable(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resourceName, &instance),
),
},
},
})
}

// Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/10203
func TestAccAWSInstance_CreditSpecification_UnspecifiedToEmpty_NonBurstable(t *testing.T) {
var instance ec2.Instance
resourceName := "aws_instance.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig_CreditSpecification_Unspecified_NonBurstable(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resourceName, &instance),
),
},
{
Config: testAccInstanceConfig_CreditSpecification_Empty_NonBurstable(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(resourceName, &instance),
),
},
},
})
}

func TestAccAWSInstance_creditSpecification_unspecifiedDefaultsToStandard(t *testing.T) {
var instance ec2.Instance
resName := "aws_instance.foo"
Expand Down Expand Up @@ -3939,6 +3986,85 @@ resource "aws_instance" "foo" {
`, rInt, val)
}

func testAccInstanceConfig_CreditSpecification_Empty_NonBurstable(rName string) string {
return fmt.Sprintf(`
data "aws_ami" "amzn-ami-minimal-hvm-ebs" {
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["amzn-ami-minimal-hvm-*"]
}

filter {
name = "root-device-type"
values = ["ebs"]
}
}


resource "aws_vpc" "test" {
cidr_block = "172.16.0.0/16"

tags = {
Name = %[1]q
}
}

resource "aws_subnet" "test" {
cidr_block = "172.16.0.0/24"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
instance_type = "m5.large"
subnet_id = "${aws_subnet.test.id}"

credit_specification {}
}
`, rName)
}

func testAccInstanceConfig_CreditSpecification_Unspecified_NonBurstable(rName string) string {
return fmt.Sprintf(`
data "aws_ami" "amzn-ami-minimal-hvm-ebs" {
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["amzn-ami-minimal-hvm-*"]
}

filter {
name = "root-device-type"
values = ["ebs"]
}
}

resource "aws_vpc" "test" {
cidr_block = "172.16.0.0/16"

tags = {
Name = %[1]q
}
}

resource "aws_subnet" "test" {
cidr_block = "172.16.0.0/24"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
instance_type = "m5.large"
subnet_id = "${aws_subnet.test.id}"
}
`, rName)
}

func testAccInstanceConfig_creditSpecification_unspecified(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "my_vpc" {
Expand Down