From ebad082c7e22dfa8af45af4439f5f4d351d6bff7 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 3 Dec 2019 09:14:46 -0500 Subject: [PATCH 01/17] Remove 'TestAccAWSInstance_multipleRegions', it seems not to test anything not tested elsewhere. --- aws/resource_aws_instance_test.go | 51 ------------------------------- 1 file changed, 51 deletions(-) diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index d7464e3ec12..e47a14fd026 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -990,32 +990,6 @@ func TestAccAWSInstance_ipv6_supportAddressCountWithIpv4(t *testing.T) { }) } -func TestAccAWSInstance_multipleRegions(t *testing.T) { - var v ec2.Instance - resourceName := "aws_instance.test" - - // record the initialized providers so that we can use them to - // check for the instances in each region - var providers []*schema.Provider - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), - CheckDestroy: testAccCheckWithProviders(testAccCheckInstanceDestroyWithProvider, &providers), - Steps: []resource.TestStep{ - { - Config: testAccInstanceConfigMultipleRegions, - Check: resource.ComposeTestCheckFunc( - testAccCheckInstanceExistsWithProvider(resourceName, &v, - testAccAwsRegionProviderFunc("us-west-2", &providers)), - testAccCheckInstanceExistsWithProvider("aws_instance.test2", &v, - testAccAwsRegionProviderFunc("us-east-1", &providers)), - ), - }, - }, - }) -} - func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" @@ -3106,31 +3080,6 @@ resource "aws_instance" "test" { `, rName) } -const testAccInstanceConfigMultipleRegions = ` -provider "aws" { - alias = "west" - region = "us-west-2" -} - -provider "aws" { - alias = "east" - region = "us-east-1" -} -resource "aws_instance" "test" { - # us-west-2 - provider = "aws.west" - ami = "ami-4fccb37f" - instance_type = "m1.small" -} - -resource "aws_instance" "test2" { - # us-east-1 - provider = "aws.east" - ami = "ami-8c6ea9e4" - instance_type = "m1.small" -} -` - const testAccCheckInstanceConfigTags = ` resource "aws_instance" "test" { ami = "ami-4fccb37f" From dcaf88be47e427ad0aeaac4fa929e38513e0375f Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 3 Dec 2019 09:15:55 -0500 Subject: [PATCH 02/17] Remove 'os.SetEnv' in 'TestAccAWSInstance_inEc2Classic' as it is not safe when running 'make test'. --- aws/resource_aws_instance_test.go | 58 ++++++++++++++++++------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index e47a14fd026..0fa946f7970 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -3,7 +3,6 @@ package aws import ( "fmt" "log" - "os" "reflect" "regexp" "testing" @@ -199,29 +198,36 @@ func TestAccAWSInstance_inDefaultVpcBySgId(t *testing.T) { } func TestAccAWSInstance_inEc2Classic(t *testing.T) { - resourceName := "aws_instance.test" - rInt := acctest.RandInt() var v ec2.Instance + resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) - // EC2 Classic enabled - oldvar := os.Getenv("AWS_DEFAULT_REGION") - os.Setenv("AWS_DEFAULT_REGION", "us-east-1") - defer os.Setenv("AWS_DEFAULT_REGION", oldvar) + var providers []*schema.Provider + skipIfNotEc2Classic := func() (bool, error) { + provider := testAccAwsRegionProviderFunc("us-east-1", &providers)() + if provider == nil { + return true, fmt.Errorf("No suitable provider found") + } + + return !hasEc2Classic(provider.Meta().(*AWSClient).supportedplatforms), nil + } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckInstanceDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories(&providers), + CheckDestroy: testAccCheckWithProviders(testAccCheckInstanceDestroyWithProvider, &providers), Steps: []resource.TestStep{ { - Config: testAccInstanceConfigInEc2Classic(rInt), + SkipFunc: skipIfNotEc2Classic, + Config: testAccInstanceConfigInEc2Classic(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckInstanceExists( - resourceName, &v), + testAccCheckInstanceExistsWithProvider(resourceName, &v, + testAccAwsRegionProviderFunc("us-east-1", &providers)), ), }, { - Config: testAccInstanceConfigInEc2Classic(rInt), + SkipFunc: skipIfNotEc2Classic, + Config: testAccInstanceConfigInEc2Classic(rName), ResourceName: resourceName, ImportState: true, ImportStateVerify: true, @@ -2758,13 +2764,11 @@ resource "aws_instance" "test" { `, rName) } -func testAccInstanceConfigInEc2Classic(rInt int) string { - return fmt.Sprintf(` -provider "aws" { - region = "us-east-1" -} - +func testAccInstanceConfigInEc2Classic(rName string) string { + return testAccUsEast1RegionProviderConfig() + fmt.Sprintf(` data "aws_ami" "ubuntu" { + provider = "aws.us-east-1" + most_recent = true filter { @@ -2780,17 +2784,21 @@ data "aws_ami" "ubuntu" { owners = ["099720109477"] # Canonical } -resource "aws_security_group" "sg" { - name = "tf_acc_test_%d" - description = "Test security group" +resource "aws_security_group" "test" { + provider = "aws.us-east-1" + + name = %[1]q + description = %[1]q } resource "aws_instance" "test" { + provider = "aws.us-east-1" + ami = "${data.aws_ami.ubuntu.id}" instance_type = "m3.medium" - security_groups = ["${aws_security_group.sg.name}"] + security_groups = ["${aws_security_group.test.name}"] } -`, rInt) +`, rName) } func testAccInstanceConfig_pre(rInt int) string { From a434947d1af76a3f6aae37f200bc71d31dfaca75 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 3 Dec 2019 10:54:12 -0500 Subject: [PATCH 03/17] Revert "Remove 'os.SetEnv' in 'TestAccAWSInstance_inEc2Classic' as it is not safe when running 'make test'." This reverts commit e731fb3325810518714e93514870580f91633d92. --- aws/resource_aws_instance_test.go | 58 +++++++++++++------------------ 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 0fa946f7970..e47a14fd026 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "os" "reflect" "regexp" "testing" @@ -198,36 +199,29 @@ func TestAccAWSInstance_inDefaultVpcBySgId(t *testing.T) { } func TestAccAWSInstance_inEc2Classic(t *testing.T) { - var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rInt := acctest.RandInt() + var v ec2.Instance - var providers []*schema.Provider - skipIfNotEc2Classic := func() (bool, error) { - provider := testAccAwsRegionProviderFunc("us-east-1", &providers)() - if provider == nil { - return true, fmt.Errorf("No suitable provider found") - } - - return !hasEc2Classic(provider.Meta().(*AWSClient).supportedplatforms), nil - } + // EC2 Classic enabled + oldvar := os.Getenv("AWS_DEFAULT_REGION") + os.Setenv("AWS_DEFAULT_REGION", "us-east-1") + defer os.Setenv("AWS_DEFAULT_REGION", oldvar) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), - CheckDestroy: testAccCheckWithProviders(testAccCheckInstanceDestroyWithProvider, &providers), + PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - SkipFunc: skipIfNotEc2Classic, - Config: testAccInstanceConfigInEc2Classic(rName), + Config: testAccInstanceConfigInEc2Classic(rInt), Check: resource.ComposeTestCheckFunc( - testAccCheckInstanceExistsWithProvider(resourceName, &v, - testAccAwsRegionProviderFunc("us-east-1", &providers)), + testAccCheckInstanceExists( + resourceName, &v), ), }, { - SkipFunc: skipIfNotEc2Classic, - Config: testAccInstanceConfigInEc2Classic(rName), + Config: testAccInstanceConfigInEc2Classic(rInt), ResourceName: resourceName, ImportState: true, ImportStateVerify: true, @@ -2764,11 +2758,13 @@ resource "aws_instance" "test" { `, rName) } -func testAccInstanceConfigInEc2Classic(rName string) string { - return testAccUsEast1RegionProviderConfig() + fmt.Sprintf(` -data "aws_ami" "ubuntu" { - provider = "aws.us-east-1" +func testAccInstanceConfigInEc2Classic(rInt int) string { + return fmt.Sprintf(` +provider "aws" { + region = "us-east-1" +} +data "aws_ami" "ubuntu" { most_recent = true filter { @@ -2784,21 +2780,17 @@ data "aws_ami" "ubuntu" { owners = ["099720109477"] # Canonical } -resource "aws_security_group" "test" { - provider = "aws.us-east-1" - - name = %[1]q - description = %[1]q +resource "aws_security_group" "sg" { + name = "tf_acc_test_%d" + description = "Test security group" } resource "aws_instance" "test" { - provider = "aws.us-east-1" - ami = "${data.aws_ami.ubuntu.id}" instance_type = "m3.medium" - security_groups = ["${aws_security_group.test.name}"] + security_groups = ["${aws_security_group.sg.name}"] } -`, rName) +`, rInt) } func testAccInstanceConfig_pre(rInt int) string { From 4c577e1f5700e042018d304a29590e1816600418 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 3 Dec 2019 11:43:48 -0500 Subject: [PATCH 04/17] EC2_CLASSIC_REGION must be set to run EC2-Classic acceptance tests. --- aws/resource_aws_instance_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index e47a14fd026..f1291ab875e 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -199,13 +199,19 @@ func TestAccAWSInstance_inDefaultVpcBySgId(t *testing.T) { } func TestAccAWSInstance_inEc2Classic(t *testing.T) { + key := "EC2_CLASSIC_REGION" + ec2ClassicRegion := os.Getenv(key) + if ec2ClassicRegion == "" { + t.Skipf("%s must be set to run EC2-Classic acceptance tests", key) + } + resourceName := "aws_instance.test" rInt := acctest.RandInt() var v ec2.Instance // EC2 Classic enabled oldvar := os.Getenv("AWS_DEFAULT_REGION") - os.Setenv("AWS_DEFAULT_REGION", "us-east-1") + os.Setenv("AWS_DEFAULT_REGION", ec2ClassicRegion) defer os.Setenv("AWS_DEFAULT_REGION", oldvar) resource.ParallelTest(t, resource.TestCase{ From 471143752d809d712093c5f931617a0dff1581bd Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 3 Dec 2019 19:12:57 -0500 Subject: [PATCH 05/17] d/aws_instance: Region-agnostic acceptance tests. --- aws/data_source_aws_instance_test.go | 226 +++++++++++++++++++-------- aws/resource_aws_instance_test.go | 32 +++- 2 files changed, 181 insertions(+), 77 deletions(-) diff --git a/aws/data_source_aws_instance_test.go b/aws/data_source_aws_instance_test.go index 45353709683..7c06ef2667f 100644 --- a/aws/data_source_aws_instance_test.go +++ b/aws/data_source_aws_instance_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "os" "regexp" "testing" @@ -12,13 +13,14 @@ import ( func TestAccAWSInstanceDataSource_basic(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig, + Config: testAccInstanceDataSourceConfig_basic(rName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), @@ -32,16 +34,17 @@ func TestAccAWSInstanceDataSource_basic(t *testing.T) { } func TestAccAWSInstanceDataSource_tags(t *testing.T) { - rInt := acctest.RandInt() resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rInt := acctest.RandInt() resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_Tags(rInt), + Config: testAccInstanceDataSourceConfig_Tags(rName, rInt), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), @@ -55,13 +58,14 @@ func TestAccAWSInstanceDataSource_tags(t *testing.T) { func TestAccAWSInstanceDataSource_AzUserData(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_AzUserData, + Config: testAccInstanceDataSourceConfig_AzUserData(rName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), @@ -77,13 +81,14 @@ func TestAccAWSInstanceDataSource_AzUserData(t *testing.T) { func TestAccAWSInstanceDataSource_gp2IopsDevice(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_gp2IopsDevice, + Config: testAccInstanceDataSourceConfig_gp2IopsDevice(rName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -100,13 +105,14 @@ func TestAccAWSInstanceDataSource_gp2IopsDevice(t *testing.T) { func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_blockDevices, + Config: testAccInstanceDataSourceConfig_blockDevices(rName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -124,12 +130,14 @@ func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) { // Test to verify that ebs_block_device kms_key_id does not elicit a panic func TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId(t *testing.T) { + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId, + Config: testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId(rName), }, }, }) @@ -137,12 +145,14 @@ func TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId(t *testing.T) { // Test to verify that root_block_device kms_key_id does not elicit a panic func TestAccAWSInstanceDataSource_RootBlockDevice_KmsKeyId(t *testing.T) { + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId, + Config: testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId(rName), }, }, }) @@ -151,13 +161,14 @@ func TestAccAWSInstanceDataSource_RootBlockDevice_KmsKeyId(t *testing.T) { func TestAccAWSInstanceDataSource_rootInstanceStore(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_rootInstanceStore, + Config: testAccInstanceDataSourceConfig_rootInstanceStore(rName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -194,7 +205,7 @@ func TestAccAWSInstanceDataSource_privateIP(t *testing.T) { func TestAccAWSInstanceDataSource_keyPair(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-test-key-%d", acctest.RandInt()) + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -255,17 +266,28 @@ func TestAccAWSInstanceDataSource_PlacementGroup(t *testing.T) { }) } -func TestAccAWSInstanceDataSource_SecurityGroups(t *testing.T) { - rInt := acctest.RandInt() +func TestAccAWSInstanceDataSource_Ec2ClassicSecurityGroups(t *testing.T) { + key := "EC2_CLASSIC_REGION" + ec2ClassicRegion := os.Getenv(key) + if ec2ClassicRegion == "" { + t.Skipf("%s must be set to run EC2-Classic acceptance tests", key) + } + resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + + // EC2 Classic enabled + oldvar := os.Getenv("AWS_DEFAULT_REGION") + os.Setenv("AWS_DEFAULT_REGION", ec2ClassicRegion) + defer os.Setenv("AWS_DEFAULT_REGION", oldvar) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_SecurityGroups(rInt), + Config: testAccInstanceDataSourceConfig_Ec2ClassicSecurityGroups(rName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -445,13 +467,14 @@ func TestAccAWSInstanceDataSource_creditSpecification(t *testing.T) { } // Lookup based on InstanceID -const testAccInstanceDataSourceConfig = ` +func testAccInstanceDataSourceConfig_basic(rName string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - # us-west-2 - ami = "ami-4fccb37f" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" instance_type = "m1.small" + tags = { - Name = "HelloWorld" + Name = %[1]q } } @@ -461,72 +484,79 @@ data "aws_instance" "test" { values = ["${aws_instance.test.id}"] } } -` +`, rName) +} // Use the tags attribute to filter -func testAccInstanceDataSourceConfig_Tags(rInt int) string { - return fmt.Sprintf(` +func testAccInstanceDataSourceConfig_Tags(rName string, rInt int) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - # us-west-2 - ami = "ami-4fccb37f" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" instance_type = "m1.small" tags = { - Name = "HelloWorld" - TestSeed = "%d" + Name = %[1]q + TestSeed = "%[2]d" } } data "aws_instance" "test" { instance_tags = { Name = "${aws_instance.test.tags["Name"]}" - TestSeed = "%d" + TestSeed = "%[2]d" } } -`, rInt, rInt) +`, rName, rInt) } // filter on tag, populate more attributes -const testAccInstanceDataSourceConfig_AzUserData = ` +func testAccInstanceDataSourceConfig_AzUserData(rName string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - # us-west-2 - ami = "ami-4fccb37f" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" availability_zone = "us-west-2a" + instance_type = "m1.small" + user_data = "test:-with-character's" - instance_type = "m1.small" - user_data = "test:-with-character's" tags = { - TFAccTest = "YesThisIsATest" + Name = %[1]q } } data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -` +`, rName) +} // GP2IopsDevice -const testAccInstanceDataSourceConfig_gp2IopsDevice = ` +func testAccInstanceDataSourceConfig_gp2IopsDevice(rName string) string { + return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - # us-west-2 - ami = "ami-55a7ea65" + ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" instance_type = "m3.medium" + root_block_device { volume_type = "gp2" volume_size = 11 } + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -` +`, rName) +} // Block Device -const testAccInstanceDataSourceConfig_blockDevices = ` +func testAccInstanceDataSourceConfig_blockDevices(rName string) string { + return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - # us-west-2 - ami = "ami-55a7ea65" + ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" instance_type = "m3.medium" root_block_device { @@ -555,21 +585,26 @@ resource "aws_instance" "test" { device_name = "/dev/sde" virtual_name = "ephemeral0" } + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -` +`, rName) +} -const testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId = ` +func testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId(rName string) string { + return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` resource "aws_kms_key" "test" { deletion_window_in_days = 7 } resource "aws_instance" "test" { - # us-west-2 - ami = "ami-55a7ea65" + ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" instance_type = "m3.medium" root_block_device { @@ -582,21 +617,26 @@ resource "aws_instance" "test" { kms_key_id = "${aws_kms_key.test.arn}" volume_size = 9 } + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -` +`, rName) +} -const testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId = ` +func testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId(rName string) string { + return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` resource "aws_kms_key" "test" { deletion_window_in_days = 7 } resource "aws_instance" "test" { - # us-west-2 - ami = "ami-55a7ea65" + ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" instance_type = "m3.medium" root_block_device { @@ -605,22 +645,34 @@ resource "aws_instance" "test" { volume_type = "gp2" volume_size = 11 } + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -` +`, rName) +} -const testAccInstanceDataSourceConfig_rootInstanceStore = ` +func testAccInstanceDataSourceConfig_rootInstanceStore(rName string) string { + return testAccLatestAmazonLinuxHvmInstanceStoreAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - ami = "ami-44c36524" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-instance-store.id}" instance_type = "m3.medium" + + tags = { + Name = %[1]q + } } + data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -` +`, rName) +} func testAccInstanceDataSourceConfig_privateIP(rName string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` @@ -629,12 +681,16 @@ resource "aws_instance" "test" { instance_type = "t2.micro" subnet_id = "${aws_subnet.test.id}" private_ip = "10.1.1.42" + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`) +`, rName) } func testAccInstanceDataSourceConfig_keyPair(rName string) string { @@ -678,12 +734,16 @@ resource "aws_instance" "test" { tenancy = "dedicated" # pre-encoded base64 data user_data = "3dc39dda39be1205215e776bad998da361a5955d" + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`) +`, rName) } func testAccInstanceDataSourceConfig_PlacementGroup(rName string) string { @@ -703,6 +763,10 @@ resource "aws_instance" "test" { # pre-encoded base64 data user_data = "3dc39dda39be1205215e776bad998da361a5955d" + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { @@ -711,15 +775,11 @@ data "aws_instance" "test" { `, rName) } -func testAccInstanceDataSourceConfig_SecurityGroups(rInt int) string { - return fmt.Sprintf(` -provider "aws" { - region = "us-east-1" -} - -resource "aws_security_group" "tf_test_foo" { - name = "tf_test_foo-%d" - description = "foo" +func testAccInstanceDataSourceConfig_Ec2ClassicSecurityGroups(rName string) string { + return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` +resource "aws_security_group" "test" { + name = %[1]q + description = %[1]q ingress { protocol = "icmp" @@ -727,19 +787,27 @@ resource "aws_security_group" "tf_test_foo" { to_port = -1 self = true } + + tags = { + Name = %[1]q + } } resource "aws_instance" "test" { - ami = "ami-408c7f28" + ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" instance_type = "m1.small" - security_groups = ["${aws_security_group.tf_test_foo.name}"] + security_groups = ["${aws_security_group.test.name}"] user_data = "foo:-with-character's" + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rInt) +`, rName) } func testAccInstanceDataSourceConfig_VPCSecurityGroups(rName string) string { @@ -753,12 +821,16 @@ resource "aws_instance" "test" { vpc_security_group_ids = ["${aws_security_group.test.id}"] subnet_id = "${aws_subnet.test.id}" depends_on = ["aws_internet_gateway.test"] + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`) +`, rName) } func testAccInstanceDataSourceConfig_getPasswordData(rName string, val bool) string { @@ -772,6 +844,10 @@ resource "aws_instance" "test" { ami = "${data.aws_ami.win2016core-ami.id}" instance_type = "t2.medium" key_name = "${aws_key_pair.test.key_name}" + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { @@ -794,6 +870,10 @@ resource "aws_instance" "test" { echo "hello world" EUD + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { @@ -809,6 +889,10 @@ resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" instance_type = "t2.micro" subnet_id = "${aws_subnet.test.id}" + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { @@ -828,10 +912,14 @@ resource "aws_instance" "test" { credit_specification { cpu_credits = "unlimited" } + + tags = { + Name = %[1]q + } } data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`) +`, rName) } diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index f1291ab875e..52d73c6625c 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -205,9 +205,9 @@ func TestAccAWSInstance_inEc2Classic(t *testing.T) { t.Skipf("%s must be set to run EC2-Classic acceptance tests", key) } + var v ec2.Instance resourceName := "aws_instance.test" rInt := acctest.RandInt() - var v ec2.Instance // EC2 Classic enabled oldvar := os.Getenv("AWS_DEFAULT_REGION") @@ -2766,10 +2766,6 @@ resource "aws_instance" "test" { func testAccInstanceConfigInEc2Classic(rInt int) string { return fmt.Sprintf(` -provider "aws" { - region = "us-east-1" -} - data "aws_ami" "ubuntu" { most_recent = true @@ -4029,13 +4025,34 @@ data "aws_ami" "amzn-ami-minimal-hvm-ebs" { `) } +// testAccLatestAmazonLinuxHvmInstanceStoreAmiConfig returns the configuration for a data source that +// describes the latest Amazon Linux AMI using HVM virtualization and an instance store root device. +// The data source is named 'amzn-ami-minimal-hvm-instance-store'. +func testAccLatestAmazonLinuxHvmInstanceStoreAmiConfig() string { + return fmt.Sprintf(` +data "aws_ami" "amzn-ami-minimal-hvm-instance-store" { + most_recent = true + owners = ["amazon"] + + filter { + name = "name" + values = ["amzn-ami-minimal-hvm-*"] + } + + filter { + name = "root-device-type" + values = ["instance-store"] + } +} +`) +} + // testAccLatestAmazonLinuxPvEbsAmiConfig returns the configuration for a data source that // describes the latest Amazon Linux AMI using PV virtualization and an EBS root device. // The data source is named 'amzn-ami-minimal-pv-ebs'. -/* func testAccLatestAmazonLinuxPvEbsAmiConfig() string { return fmt.Sprintf(` -data "aws_ami" "amzn-ami-minimal-hvm-ebs" { +data "aws_ami" "amzn-ami-minimal-pv-ebs" { most_recent = true owners = ["amazon"] @@ -4051,7 +4068,6 @@ data "aws_ami" "amzn-ami-minimal-hvm-ebs" { } `) } -*/ // testAccLatestWindowsServer2016CoreAmiConfig returns the configuration for a data source that // describes the latest Microsoft Windows Server 2016 Core AMI. From 8dff39d8c742fb4cb967100cc8f4258cca4d3ffd Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 4 Dec 2019 10:28:11 -0500 Subject: [PATCH 06/17] d/aws_instances: Region-agnostic acceptance tests. --- aws/data_source_aws_instances_test.go | 121 +++++++++----------------- 1 file changed, 41 insertions(+), 80 deletions(-) diff --git a/aws/data_source_aws_instances_test.go b/aws/data_source_aws_instances_test.go index 8d0bd9f89e9..00fbc74756e 100644 --- a/aws/data_source_aws_instances_test.go +++ b/aws/data_source_aws_instances_test.go @@ -9,17 +9,20 @@ import ( ) func TestAccAWSInstancesDataSource_basic(t *testing.T) { + datasourceName := "data.aws_instances.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstancesDataSourceConfig_ids, + Config: testAccInstancesDataSourceConfig_ids(rName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_instances.test", "ids.#", "3"), - resource.TestCheckResourceAttr("data.aws_instances.test", "private_ips.#", "3"), + resource.TestCheckResourceAttr(datasourceName, "ids.#", "3"), + resource.TestCheckResourceAttr(datasourceName, "private_ips.#", "3"), // Public IP values are flakey for new EC2 instances due to eventual consistency - resource.TestCheckResourceAttrSet("data.aws_instances.test", "public_ips.#"), + resource.TestCheckResourceAttrSet(datasourceName, "public_ips.#"), ), }, }, @@ -27,15 +30,17 @@ func TestAccAWSInstancesDataSource_basic(t *testing.T) { } func TestAccAWSInstancesDataSource_tags(t *testing.T) { - rInt := acctest.RandInt() + datasourceName := "data.aws_instances.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstancesDataSourceConfig_tags(rInt), + Config: testAccInstancesDataSourceConfig_tags(rName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_instances.test", "ids.#", "2"), + resource.TestCheckResourceAttr(datasourceName, "ids.#", "2"), ), }, }, @@ -43,85 +48,56 @@ func TestAccAWSInstancesDataSource_tags(t *testing.T) { } func TestAccAWSInstancesDataSource_instance_state_names(t *testing.T) { - rInt := acctest.RandInt() + datasourceName := "data.aws_instances.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstancesDataSourceConfig_instance_state_names(rInt), + Config: testAccInstancesDataSourceConfig_instance_state_names(rName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_instances.test", "ids.#", "2"), + resource.TestCheckResourceAttr(datasourceName, "ids.#", "2"), ), }, }, }) } -const testAccInstancesDataSourceConfig_ids = ` -data "aws_ami" "ubuntu" { - most_recent = true - - filter { - name = "name" - values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] - } - - filter { - name = "virtualization-type" - values = ["hvm"] - } - - owners = ["099720109477"] # Canonical -} - +func testAccInstancesDataSourceConfig_ids(rName string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { count = 3 - ami = "${data.aws_ami.ubuntu.id}" + + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" instance_type = "t2.micro" + tags = { - Name = "TfAccTest" + Name = %[1]q } } data "aws_instances" "test" { filter { - name = "instance-id" - values = [ - "${aws_instance.test.*.id[0]}", - "${aws_instance.test.*.id[1]}", - "${aws_instance.test.*.id[2]}", - ] + name = "instance-id" + values = ["${aws_instance.test.*.id}"] } } -` - -func testAccInstancesDataSourceConfig_tags(rInt int) string { - return fmt.Sprintf(` -data "aws_ami" "ubuntu" { - most_recent = true - - filter { - name = "name" - values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] - } - - filter { - name = "virtualization-type" - values = ["hvm"] - } - - owners = ["099720109477"] # Canonical +`, rName) } +func testAccInstancesDataSourceConfig_tags(rName string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - count = 2 - ami = "${data.aws_ami.ubuntu.id}" + count = 2 + + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" instance_type = "t2.micro" tags = { - Name = "tf-acc-test-ec2-instance-data-source-%d" - SecondTag = "tf-acc-test-ec2-instance-data-source-%d" + Name = %[1]q + SecondTag = %[1]q } } @@ -131,34 +107,19 @@ data "aws_instances" "test" { SecondTag = "${aws_instance.test.1.tags["Name"]}" } } -`, rInt, rInt) -} - -func testAccInstancesDataSourceConfig_instance_state_names(rInt int) string { - return fmt.Sprintf(` -data "aws_ami" "ubuntu" { - most_recent = true - - filter { - name = "name" - values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] - } - - filter { - name = "virtualization-type" - values = ["hvm"] - } - - owners = ["099720109477"] # Canonical +`, rName) } +func testAccInstancesDataSourceConfig_instance_state_names(rName string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - count = 2 - ami = "${data.aws_ami.ubuntu.id}" + count = 2 + + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" instance_type = "t2.micro" tags = { - Name = "tf-acc-test-ec2-instance-data-source-%d" + Name = %[1]q } } @@ -169,5 +130,5 @@ data "aws_instances" "test" { instance_state_names = ["pending", "running"] } -`, rInt) +`, rName) } From a0333eb043ed9b97bf0fc2ef09d9c343375f3aae Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 4 Dec 2019 10:55:49 -0500 Subject: [PATCH 07/17] Add 'testAccPreCheckHasDefaultVpc'. --- aws/resource_aws_instance_test.go | 40 ++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 52d73c6625c..8c51400f1b7 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -152,7 +152,7 @@ func TestAccAWSInstance_inDefaultVpcBySgName(t *testing.T) { rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpc(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ @@ -178,7 +178,7 @@ func TestAccAWSInstance_inDefaultVpcBySgId(t *testing.T) { rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpc(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ @@ -2727,6 +2727,10 @@ resource "aws_security_group" "test" { name = %[1]q description = %[1]q vpc_id = "${data.aws_vpc.default.id}" + + tags = { + Name = %[1]q + } } resource "aws_instance" "test" { @@ -2734,6 +2738,10 @@ resource "aws_instance" "test" { instance_type = "t2.micro" security_groups = ["${aws_security_group.test.name}"] availability_zone = "${data.aws_availability_zones.current.names[0]}" + + tags = { + Name = %[1]q + } } `, rName) } @@ -2753,6 +2761,10 @@ resource "aws_security_group" "test" { name = %[1]q description = %[1]q vpc_id = "${data.aws_vpc.default.id}" + + tags = { + Name = %[1]q + } } resource "aws_instance" "test" { @@ -2760,6 +2772,10 @@ resource "aws_instance" "test" { instance_type = "t2.micro" vpc_security_group_ids = ["${aws_security_group.test.id}"] availability_zone = "${data.aws_availability_zones.current.names[0]}" + + tags = { + Name = %[1]q + } } `, rName) } @@ -4195,7 +4211,7 @@ data "aws_ami" "amzn-ami-minimal-hvm-ebs" { name = "name" values = ["amzn-ami-minimal-hvm-*"] } - + filter { name = "root-device-type" values = ["ebs"] @@ -4233,3 +4249,21 @@ resource "aws_instance" "test" { } `, hibernation) } + +// testAccPreCheckHasDefaultVpc checks that the test region has a default VPC. +func testAccPreCheckHasDefaultVpc(t *testing.T) { + conn := testAccProvider.Meta().(*AWSClient).ec2conn + + resp, err := conn.DescribeAccountAttributes(&ec2.DescribeAccountAttributesInput{ + AttributeNames: aws.StringSlice([]string{ec2.AccountAttributeNameDefaultVpc}), + }) + if testAccPreCheckSkipError(err) || + len(resp.AccountAttributes) == 0 || + len(resp.AccountAttributes[0].AttributeValues) == 0 || + aws.StringValue(resp.AccountAttributes[0].AttributeValues[0].AttributeValue) == "none" { + t.Skip("skipping tests; this region does not have a default VPC") + } + if err != nil { + t.Fatalf("error describing EC2 account attributes: %s", err) + } +} From ec148a8f57d9ae77a80ffb1956033d600dbb2798 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 4 Dec 2019 12:29:33 -0500 Subject: [PATCH 08/17] r/aws_instance: Region-agnostic acceptance tests (EC2-Classic and default VPC). --- aws/resource_aws_instance_test.go | 159 +++++++++++------------------- 1 file changed, 57 insertions(+), 102 deletions(-) diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 8c51400f1b7..c573d3414e4 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -207,7 +207,7 @@ func TestAccAWSInstance_inEc2Classic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rInt := acctest.RandInt() + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) // EC2 Classic enabled oldvar := os.Getenv("AWS_DEFAULT_REGION") @@ -220,14 +220,13 @@ func TestAccAWSInstance_inEc2Classic(t *testing.T) { CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigInEc2Classic(rInt), + Config: testAccInstanceConfigInEc2Classic(rName), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), ), }, { - Config: testAccInstanceConfigInEc2Classic(rInt), ResourceName: resourceName, ImportState: true, ImportStateVerify: true, @@ -239,21 +238,16 @@ func TestAccAWSInstance_inEc2Classic(t *testing.T) { func TestAccAWSInstance_basic(t *testing.T) { var v ec2.Instance - var vol *ec2.Volume resourceName := "aws_instance.test" - rInt := acctest.RandInt() + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) - testCheck := func(rInt int) func(*terraform.State) error { + testCheck := func() func(*terraform.State) error { return func(*terraform.State) error { - if *v.Placement.AvailabilityZone != "us-west-2a" { - return fmt.Errorf("bad availability zone: %#v", *v.Placement.AvailabilityZone) - } - if len(v.SecurityGroups) == 0 { return fmt.Errorf("no security groups: %#v", v.SecurityGroups) } - if *v.SecurityGroups[0].GroupName != fmt.Sprintf("tf_test_%d", rInt) { - return fmt.Errorf("no security groups: %#v", v.SecurityGroups) + if *v.SecurityGroups[0].GroupName != rName { + return fmt.Errorf("incorrect security group name: %#v", v.SecurityGroups) } return nil @@ -261,31 +255,19 @@ func TestAccAWSInstance_basic(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // TODO Does the instance run in default VPC even if in us-east-1 and have EC2-Classic? + // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-classic-platform.html + PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpc(t) }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ - // Create a volume to cover #1249 - { - // Need a resource in this config so the provisioner will be available - Config: testAccInstanceConfig_pre(rInt), - Check: func(*terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).ec2conn - var err error - vol, err = conn.CreateVolume(&ec2.CreateVolumeInput{ - AvailabilityZone: aws.String("us-west-2a"), - Size: aws.Int64(int64(5)), - }) - return err - }, - }, { - Config: testAccInstanceConfig(rInt), + Config: testAccInstanceConfigBasic(rName), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), - testCheck(rInt), + testCheck(), resource.TestCheckResourceAttr( resourceName, "user_data", @@ -307,27 +289,8 @@ func TestAccAWSInstance_basic(t *testing.T) { // that the user data hash stuff is working without generating // an incorrect diff. { - Config: testAccInstanceConfig(rInt), - Check: resource.ComposeTestCheckFunc( - testAccCheckInstanceExists( - resourceName, &v), - testCheck(rInt), - resource.TestCheckResourceAttr( - resourceName, - "user_data", - "3dc39dda39be1205215e776bad998da361a5955d"), - resource.TestCheckResourceAttr( - resourceName, "ebs_block_device.#", "0"), - ), - }, - // Clean up volume created above - { - Config: testAccInstanceConfig(rInt), - Check: func(*terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).ec2conn - _, err := conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeId: vol.VolumeId}) - return err - }, + Config: testAccInstanceConfigBasic(rName), + PlanOnly: true, }, }, }) @@ -2393,9 +2356,9 @@ func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits_t2Tot3Taint(t *t } func TestAccAWSInstance_disappears(t *testing.T) { - var conf ec2.Instance - rInt := acctest.RandInt() + var v ec2.Instance resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2403,10 +2366,10 @@ func TestAccAWSInstance_disappears(t *testing.T) { CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig(rInt), + Config: testAccInstanceConfigBasic(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckInstanceExists(resourceName, &conf), - testAccCheckInstanceDisappears(&conf), + testAccCheckInstanceExists(resourceName, &v), + testAccCheckInstanceDisappears(&v), ), ExpectNonEmptyPlan: true, }, @@ -2780,58 +2743,47 @@ resource "aws_instance" "test" { `, rName) } -func testAccInstanceConfigInEc2Classic(rInt int) string { - return fmt.Sprintf(` -data "aws_ami" "ubuntu" { - most_recent = true - - filter { - name = "name" - values = ["ubuntu/images/ubuntu-trusty-14.04-amd64-server-*"] - } +func testAccInstanceConfigInEc2Classic(rName string) string { + return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` +resource "aws_security_group" "test" { + name = %[1]q + description = %[1]q - filter { - name = "virtualization-type" - values = ["paravirtual"] + tags = { + Name = %[1]q } - - owners = ["099720109477"] # Canonical -} - -resource "aws_security_group" "sg" { - name = "tf_acc_test_%d" - description = "Test security group" } resource "aws_instance" "test" { - ami = "${data.aws_ami.ubuntu.id}" + ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" instance_type = "m3.medium" - security_groups = ["${aws_security_group.sg.name}"] + security_groups = ["${aws_security_group.test.name}"] + + tags = { + Name = %[1]q + } } -`, rInt) +`, rName) } -func testAccInstanceConfig_pre(rInt int) string { - return fmt.Sprintf(` -resource "aws_security_group" "tf_test_test" { - name = "tf_test_%d" - description = "test" +func testAccInstanceConfigBasic(rName string) string { + return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` +data "aws_availability_zones" "current" {} - ingress { - protocol = "icmp" - from_port = -1 - to_port = -1 - cidr_blocks = ["0.0.0.0/0"] +# Ensure that there is at least 1 EBS volume in the current region. +# See https://github.com/hashicorp/terraform/issues/1249. +resource "aws_ebs_volume" "test" { + availability_zone = "${data.aws_availability_zones.current.names[0]}" + size = 5 + + tags = { + Name = %[1]q } } -`, rInt) -} -func testAccInstanceConfig(rInt int) string { - return fmt.Sprintf(` -resource "aws_security_group" "tf_test_test" { - name = "tf_test_%d" - description = "test" +resource "aws_security_group" "test" { + name = %[1]q + description = %[1]q ingress { protocol = "icmp" @@ -2839,18 +2791,21 @@ resource "aws_security_group" "tf_test_test" { to_port = -1 cidr_blocks = ["0.0.0.0/0"] } + + tags = { + Name = %[1]q + } } resource "aws_instance" "test" { - # us-west-2 - ami = "ami-4fccb37f" - availability_zone = "us-west-2a" - - instance_type = "m1.small" - security_groups = ["${aws_security_group.tf_test_test.name}"] - user_data = "foo:-with-character's" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + availability_zone = "${data.aws_availability_zones.current.names[0]}" + instance_type = "m1.small" + security_groups = ["${aws_security_group.test.name}"] + user_data = "foo:-with-character's" + # Explictly no tags to test creation without. } -`, rInt) +`, rName) } func testAccInstanceConfigWithUserDataBase64(rInt int) string { From 4063839b7d8dc879c8bea8c0c6e7fc5105e3030d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 4 Dec 2019 13:08:33 -0500 Subject: [PATCH 09/17] r/aws_instance: Region-agnostic acceptance tests. --- aws/resource_aws_instance_test.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index c573d3414e4..9f4ee8e8a57 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -297,9 +297,10 @@ func TestAccAWSInstance_basic(t *testing.T) { } func TestAccAWSInstance_EbsBlockDevice_KmsKeyArn(t *testing.T) { - var instance ec2.Instance - kmsKeyResourceName := "aws_kms_key.test" + var v ec2.Instance resourceName := "aws_instance.test" + kmsKeyResourceName := "aws_kms_key.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -307,9 +308,9 @@ func TestAccAWSInstance_EbsBlockDevice_KmsKeyArn(t *testing.T) { CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigEbsBlockDeviceKmsKeyArn, + Config: testAccInstanceConfigEbsBlockDeviceKmsKeyArn(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckInstanceExists(resourceName, &instance), + testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "ebs_block_device.#", "1"), resource.TestCheckResourceAttr(resourceName, "ebs_block_device.2634515331.encrypted", "true"), resource.TestCheckResourceAttrPair(resourceName, "ebs_block_device.2634515331.kms_key_id", kmsKeyResourceName, "arn"), @@ -3063,14 +3064,18 @@ resource "aws_instance" "test" { } ` -const testAccInstanceConfigEbsBlockDeviceKmsKeyArn = ` +func testAccInstanceConfigEbsBlockDeviceKmsKeyArn(rName string) string { + return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` resource "aws_kms_key" "test" { deletion_window_in_days = 7 + + tags = { + Name = %[1]q + } } resource "aws_instance" "test" { - # us-west-2 - ami = "ami-55a7ea65" + ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" # In order to attach an encrypted volume to an instance you need to have an # m3.medium or larger. See "Supported Instance Types" in: @@ -3089,8 +3094,13 @@ resource "aws_instance" "test" { kms_key_id = "${aws_kms_key.test.arn}" volume_size = 12 } + + tags = { + Name = %[1]q + } +} +`, rName) } -` const testAccInstanceConfigRootBlockDeviceKmsKeyArn = ` resource "aws_vpc" "test" { From e3d71326ee54cc8b23fd88290105f21818c24485 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 4 Dec 2019 17:23:46 -0500 Subject: [PATCH 10/17] d/aws_instances: Fix typo in acceptance tests. --- aws/data_source_aws_instances_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_instances_test.go b/aws/data_source_aws_instances_test.go index 00fbc74756e..aecb1b71a7b 100644 --- a/aws/data_source_aws_instances_test.go +++ b/aws/data_source_aws_instances_test.go @@ -81,7 +81,7 @@ resource "aws_instance" "test" { data "aws_instances" "test" { filter { name = "instance-id" - values = ["${aws_instance.test.*.id}"] + values = "${aws_instance.test.*.id}" } } `, rName) From 22c746e74c057733c20a2a150de3e0533293bf25 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 4 Dec 2019 18:21:47 -0500 Subject: [PATCH 11/17] Add 'testAccPreCheckHasDefaultVpcOrEc2Classic'. --- aws/data_source_aws_instances_test.go | 12 ++++++------ aws/resource_aws_instance_test.go | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/aws/data_source_aws_instances_test.go b/aws/data_source_aws_instances_test.go index aecb1b71a7b..9f3a9ecec12 100644 --- a/aws/data_source_aws_instances_test.go +++ b/aws/data_source_aws_instances_test.go @@ -13,7 +13,7 @@ func TestAccAWSInstancesDataSource_basic(t *testing.T) { rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpcOrEc2Classic(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { @@ -34,7 +34,7 @@ func TestAccAWSInstancesDataSource_tags(t *testing.T) { rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpcOrEc2Classic(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { @@ -52,7 +52,7 @@ func TestAccAWSInstancesDataSource_instance_state_names(t *testing.T) { rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpcOrEc2Classic(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { @@ -71,7 +71,7 @@ resource "aws_instance" "test" { count = 3 ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = "t1.micro" tags = { Name = %[1]q @@ -93,7 +93,7 @@ resource "aws_instance" "test" { count = 2 ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = "t1.micro" tags = { Name = %[1]q @@ -116,7 +116,7 @@ resource "aws_instance" "test" { count = 2 ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = "t1.micro" tags = { Name = %[1]q diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 9f4ee8e8a57..b15628423fa 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -4217,6 +4217,23 @@ resource "aws_instance" "test" { // testAccPreCheckHasDefaultVpc checks that the test region has a default VPC. func testAccPreCheckHasDefaultVpc(t *testing.T) { + if !testAccHasDefaultVpc(t) { + region := testAccProvider.Meta().(*AWSClient).region + t.Skipf("skipping tests; %s does not have a default VPC", region) + } +} + +// testAccPreCheckHasDefaultVpcOrEc2Classic checks that the test region has a default VPC or has the EC2-Classic platform. +// This check is useful to ensure that an instance can be launched without specifying a subnet. +func testAccPreCheckHasDefaultVpcOrEc2Classic(t *testing.T) { + client := testAccProvider.Meta().(*AWSClient) + + if !testAccHasDefaultVpc(t) && !hasEc2Classic(client.supportedplatforms) { + t.Skipf("skipping tests; %s does not have a default VPC or EC2-Classic", client.region) + } +} + +func testAccHasDefaultVpc(t *testing.T) bool { conn := testAccProvider.Meta().(*AWSClient).ec2conn resp, err := conn.DescribeAccountAttributes(&ec2.DescribeAccountAttributesInput{ @@ -4226,9 +4243,11 @@ func testAccPreCheckHasDefaultVpc(t *testing.T) { len(resp.AccountAttributes) == 0 || len(resp.AccountAttributes[0].AttributeValues) == 0 || aws.StringValue(resp.AccountAttributes[0].AttributeValues[0].AttributeValue) == "none" { - t.Skip("skipping tests; this region does not have a default VPC") + return false } if err != nil { t.Fatalf("error describing EC2 account attributes: %s", err) } + + return true } From 7cfd5759cd052f9b93d799e293f1b6ca70278f08 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 4 Dec 2019 21:12:08 -0500 Subject: [PATCH 12/17] Add 'testAccPreCheckOffersEc2InstanceType'. --- aws/data_source_aws_instances_test.go | 50 +++++++++++++++++++-------- aws/resource_aws_instance_test.go | 18 ++++++++++ 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/aws/data_source_aws_instances_test.go b/aws/data_source_aws_instances_test.go index 9f3a9ecec12..646c17ee2ed 100644 --- a/aws/data_source_aws_instances_test.go +++ b/aws/data_source_aws_instances_test.go @@ -11,13 +11,21 @@ import ( func TestAccAWSInstancesDataSource_basic(t *testing.T) { datasourceName := "data.aws_instances.test" rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpcOrEc2Classic(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + // t2.micro can't be launched in EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccEC2VPCOnlyPreCheck(t) + testAccPreCheckHasDefaultVpc(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstancesDataSourceConfig_ids(rName), + Config: testAccInstancesDataSourceConfig_ids(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "ids.#", "3"), resource.TestCheckResourceAttr(datasourceName, "private_ips.#", "3"), @@ -32,13 +40,19 @@ func TestAccAWSInstancesDataSource_basic(t *testing.T) { func TestAccAWSInstancesDataSource_tags(t *testing.T) { datasourceName := "data.aws_instances.test" rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpcOrEc2Classic(t) }, + PreCheck: func() { + testAccPreCheck(t) + testAccEC2VPCOnlyPreCheck(t) + testAccPreCheckHasDefaultVpc(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstancesDataSourceConfig_tags(rName), + Config: testAccInstancesDataSourceConfig_tags(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "ids.#", "2"), ), @@ -50,13 +64,19 @@ func TestAccAWSInstancesDataSource_tags(t *testing.T) { func TestAccAWSInstancesDataSource_instance_state_names(t *testing.T) { datasourceName := "data.aws_instances.test" rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpcOrEc2Classic(t) }, + PreCheck: func() { + testAccPreCheck(t) + testAccEC2VPCOnlyPreCheck(t) + testAccPreCheckHasDefaultVpc(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstancesDataSourceConfig_instance_state_names(rName), + Config: testAccInstancesDataSourceConfig_instance_state_names(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "ids.#", "2"), ), @@ -65,13 +85,13 @@ func TestAccAWSInstancesDataSource_instance_state_names(t *testing.T) { }) } -func testAccInstancesDataSourceConfig_ids(rName string) string { +func testAccInstancesDataSourceConfig_ids(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { count = 3 ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t1.micro" + instance_type = %[2]q tags = { Name = %[1]q @@ -84,16 +104,16 @@ data "aws_instances" "test" { values = "${aws_instance.test.*.id}" } } -`, rName) +`, rName, instanceType) } -func testAccInstancesDataSourceConfig_tags(rName string) string { +func testAccInstancesDataSourceConfig_tags(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { count = 2 ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t1.micro" + instance_type = %[2]q tags = { Name = %[1]q @@ -107,16 +127,16 @@ data "aws_instances" "test" { SecondTag = "${aws_instance.test.1.tags["Name"]}" } } -`, rName) +`, rName, instanceType) } -func testAccInstancesDataSourceConfig_instance_state_names(rName string) string { +func testAccInstancesDataSourceConfig_instance_state_names(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { count = 2 ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t1.micro" + instance_type = %[2]q tags = { Name = %[1]q @@ -130,5 +150,5 @@ data "aws_instances" "test" { instance_state_names = ["pending", "running"] } -`, rName) +`, rName, instanceType) } diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index b15628423fa..e51d031a8db 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -4215,6 +4215,24 @@ resource "aws_instance" "test" { `, hibernation) } +// testAccPreCheckOffersEc2InstanceType checks that the test region offers the specified EC2 instance type. +func testAccPreCheckOffersEc2InstanceType(t *testing.T, instanceType string) { + client := testAccProvider.Meta().(*AWSClient) + + resp, err := client.ec2conn.DescribeInstanceTypeOfferings(&ec2.DescribeInstanceTypeOfferingsInput{ + Filters: buildEC2AttributeFilterList(map[string]string{ + "instance-type": instanceType, + }), + LocationType: aws.String(ec2.LocationTypeRegion), + }) + if testAccPreCheckSkipError(err) || len(resp.InstanceTypeOfferings) == 0 { + t.Skipf("skipping tests; %s does not offer EC2 instance type: %s", client.region, instanceType) + } + if err != nil { + t.Fatalf("error describing EC2 instance type offerings: %s", err) + } +} + // testAccPreCheckHasDefaultVpc checks that the test region has a default VPC. func testAccPreCheckHasDefaultVpc(t *testing.T) { if !testAccHasDefaultVpc(t) { From 07f2bca27e3e5c2171c29c7f9b336b771d5a476d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 5 Dec 2019 09:10:16 -0500 Subject: [PATCH 13/17] Change acceptance test resource name. --- aws/data_source_aws_instance_test.go | 38 ++++----- aws/data_source_aws_instances_test.go | 6 +- aws/resource_aws_instance_test.go | 106 +++++++++++++------------- 3 files changed, 75 insertions(+), 75 deletions(-) diff --git a/aws/data_source_aws_instance_test.go b/aws/data_source_aws_instance_test.go index 7c06ef2667f..d62d200c86b 100644 --- a/aws/data_source_aws_instance_test.go +++ b/aws/data_source_aws_instance_test.go @@ -13,7 +13,7 @@ import ( func TestAccAWSInstanceDataSource_basic(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -36,7 +36,7 @@ func TestAccAWSInstanceDataSource_basic(t *testing.T) { func TestAccAWSInstanceDataSource_tags(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) rInt := acctest.RandInt() resource.ParallelTest(t, resource.TestCase{ @@ -58,7 +58,7 @@ func TestAccAWSInstanceDataSource_tags(t *testing.T) { func TestAccAWSInstanceDataSource_AzUserData(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -81,7 +81,7 @@ func TestAccAWSInstanceDataSource_AzUserData(t *testing.T) { func TestAccAWSInstanceDataSource_gp2IopsDevice(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -105,7 +105,7 @@ func TestAccAWSInstanceDataSource_gp2IopsDevice(t *testing.T) { func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -130,7 +130,7 @@ func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) { // Test to verify that ebs_block_device kms_key_id does not elicit a panic func TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId(t *testing.T) { - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -145,7 +145,7 @@ func TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId(t *testing.T) { // Test to verify that root_block_device kms_key_id does not elicit a panic func TestAccAWSInstanceDataSource_RootBlockDevice_KmsKeyId(t *testing.T) { - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -161,7 +161,7 @@ func TestAccAWSInstanceDataSource_RootBlockDevice_KmsKeyId(t *testing.T) { func TestAccAWSInstanceDataSource_rootInstanceStore(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -184,7 +184,7 @@ func TestAccAWSInstanceDataSource_rootInstanceStore(t *testing.T) { func TestAccAWSInstanceDataSource_privateIP(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -205,7 +205,7 @@ func TestAccAWSInstanceDataSource_privateIP(t *testing.T) { func TestAccAWSInstanceDataSource_keyPair(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -227,7 +227,7 @@ func TestAccAWSInstanceDataSource_keyPair(t *testing.T) { func TestAccAWSInstanceDataSource_VPC(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -250,7 +250,7 @@ func TestAccAWSInstanceDataSource_VPC(t *testing.T) { func TestAccAWSInstanceDataSource_PlacementGroup(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -275,7 +275,7 @@ func TestAccAWSInstanceDataSource_Ec2ClassicSecurityGroups(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) // EC2 Classic enabled oldvar := os.Getenv("AWS_DEFAULT_REGION") @@ -303,7 +303,7 @@ func TestAccAWSInstanceDataSource_Ec2ClassicSecurityGroups(t *testing.T) { func TestAccAWSInstanceDataSource_VPCSecurityGroups(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -324,7 +324,7 @@ func TestAccAWSInstanceDataSource_VPCSecurityGroups(t *testing.T) { func TestAccAWSInstanceDataSource_getPasswordData_trueToFalse(t *testing.T) { datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -350,7 +350,7 @@ func TestAccAWSInstanceDataSource_getPasswordData_trueToFalse(t *testing.T) { func TestAccAWSInstanceDataSource_getPasswordData_falseToTrue(t *testing.T) { datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -376,7 +376,7 @@ func TestAccAWSInstanceDataSource_getPasswordData_falseToTrue(t *testing.T) { func TestAccAWSInstanceDataSource_GetUserData(t *testing.T) { datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -410,7 +410,7 @@ func TestAccAWSInstanceDataSource_GetUserData(t *testing.T) { func TestAccAWSInstanceDataSource_GetUserData_NoUserData(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -447,7 +447,7 @@ func TestAccAWSInstanceDataSource_GetUserData_NoUserData(t *testing.T) { func TestAccAWSInstanceDataSource_creditSpecification(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, diff --git a/aws/data_source_aws_instances_test.go b/aws/data_source_aws_instances_test.go index 646c17ee2ed..9b01f127bec 100644 --- a/aws/data_source_aws_instances_test.go +++ b/aws/data_source_aws_instances_test.go @@ -10,7 +10,7 @@ import ( func TestAccAWSInstancesDataSource_basic(t *testing.T) { datasourceName := "data.aws_instances.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -39,7 +39,7 @@ func TestAccAWSInstancesDataSource_basic(t *testing.T) { func TestAccAWSInstancesDataSource_tags(t *testing.T) { datasourceName := "data.aws_instances.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -63,7 +63,7 @@ func TestAccAWSInstancesDataSource_tags(t *testing.T) { func TestAccAWSInstancesDataSource_instance_state_names(t *testing.T) { datasourceName := "data.aws_instances.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index e51d031a8db..1c5f3de32bf 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -149,7 +149,7 @@ func TestFetchRootDevice(t *testing.T) { func TestAccAWSInstance_inDefaultVpcBySgName(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpc(t) }, @@ -175,7 +175,7 @@ func TestAccAWSInstance_inDefaultVpcBySgName(t *testing.T) { func TestAccAWSInstance_inDefaultVpcBySgId(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpc(t) }, @@ -207,7 +207,7 @@ func TestAccAWSInstance_inEc2Classic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) // EC2 Classic enabled oldvar := os.Getenv("AWS_DEFAULT_REGION") @@ -239,7 +239,7 @@ func TestAccAWSInstance_inEc2Classic(t *testing.T) { func TestAccAWSInstance_basic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) testCheck := func() func(*terraform.State) error { return func(*terraform.State) error { @@ -300,7 +300,7 @@ func TestAccAWSInstance_EbsBlockDevice_KmsKeyArn(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" kmsKeyResourceName := "aws_kms_key.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -716,7 +716,7 @@ func TestAccAWSInstance_noAMIEphemeralDevices(t *testing.T) { func TestAccAWSInstance_sourceDestCheck(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) testCheck := func(enabled bool) resource.TestCheckFunc { return func(*terraform.State) error { @@ -770,7 +770,7 @@ func TestAccAWSInstance_sourceDestCheck(t *testing.T) { func TestAccAWSInstance_disableApiTermination(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) checkDisableApiTermination := func(expected bool) resource.TestCheckFunc { return func(*terraform.State) error { @@ -822,7 +822,7 @@ func TestAccAWSInstance_disableApiTermination(t *testing.T) { func TestAccAWSInstance_vpc(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -855,7 +855,7 @@ func TestAccAWSInstance_vpc(t *testing.T) { func TestAccAWSInstance_placementGroup(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -887,7 +887,7 @@ func TestAccAWSInstance_placementGroup(t *testing.T) { func TestAccAWSInstance_ipv6_supportAddressCount(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -915,7 +915,7 @@ func TestAccAWSInstance_ipv6_supportAddressCount(t *testing.T) { } func TestAccAWSInstance_ipv6AddressCountAndSingleAddressCausesError(t *testing.T) { - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -933,7 +933,7 @@ func TestAccAWSInstance_ipv6AddressCountAndSingleAddressCausesError(t *testing.T func TestAccAWSInstance_ipv6_supportAddressCountWithIpv4(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -963,7 +963,7 @@ func TestAccAWSInstance_ipv6_supportAddressCountWithIpv4(t *testing.T) { func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -991,7 +991,7 @@ func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { func TestAccAWSInstance_NetworkInstanceRemovingAllSecurityGroups(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1034,7 +1034,7 @@ func TestAccAWSInstance_NetworkInstanceRemovingAllSecurityGroups(t *testing.T) { func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1181,8 +1181,8 @@ func TestAccAWSInstance_volumeTagsComputed(t *testing.T) { func TestAccAWSInstance_instanceProfileChange(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) - rName2 := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName2 := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) testCheckInstanceProfile := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -1238,7 +1238,7 @@ func TestAccAWSInstance_instanceProfileChange(t *testing.T) { func TestAccAWSInstance_withIamInstanceProfile(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) testCheckInstanceProfile := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -1275,7 +1275,7 @@ func TestAccAWSInstance_withIamInstanceProfile(t *testing.T) { func TestAccAWSInstance_privateIP(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) testCheckPrivateIP := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -1312,7 +1312,7 @@ func TestAccAWSInstance_privateIP(t *testing.T) { func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) testCheckPrivateIP := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -1352,7 +1352,7 @@ func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { func TestAccAWSInstance_keyPairCheck(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) testCheckKeyPair := func(keyName string) resource.TestCheckFunc { return func(*terraform.State) error { @@ -1388,7 +1388,7 @@ func TestAccAWSInstance_keyPairCheck(t *testing.T) { func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1425,7 +1425,7 @@ func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) { func TestAccAWSInstance_forceNewAndTagsDrift(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1494,7 +1494,7 @@ func TestAccAWSInstance_primaryNetworkInterface(t *testing.T) { var eni ec2.NetworkInterface resourceName := "aws_instance.test" eniResourceName := "aws_network_interface.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1524,7 +1524,7 @@ func TestAccAWSInstance_primaryNetworkInterfaceSourceDestCheck(t *testing.T) { var eni ec2.NetworkInterface resourceName := "aws_instance.test" eniResourceName := "aws_network_interface.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1557,7 +1557,7 @@ func TestAccAWSInstance_addSecondaryInterface(t *testing.T) { resourceName := "aws_instance.test" eniPrimaryResourceName := "aws_network_interface.primary" eniSecondaryResourceName := "aws_network_interface.secondary" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1595,7 +1595,7 @@ func TestAccAWSInstance_addSecurityGroupNetworkInterface(t *testing.T) { var before ec2.Instance var after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1629,7 +1629,7 @@ func TestAccAWSInstance_addSecurityGroupNetworkInterface(t *testing.T) { func TestAccAWSInstance_associatePublic_defaultPrivate(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1657,7 +1657,7 @@ func TestAccAWSInstance_associatePublic_defaultPrivate(t *testing.T) { func TestAccAWSInstance_associatePublic_defaultPublic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1685,7 +1685,7 @@ func TestAccAWSInstance_associatePublic_defaultPublic(t *testing.T) { func TestAccAWSInstance_associatePublic_explicitPublic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1713,7 +1713,7 @@ func TestAccAWSInstance_associatePublic_explicitPublic(t *testing.T) { func TestAccAWSInstance_associatePublic_explicitPrivate(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1741,7 +1741,7 @@ func TestAccAWSInstance_associatePublic_explicitPrivate(t *testing.T) { func TestAccAWSInstance_associatePublic_overridePublic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1769,7 +1769,7 @@ func TestAccAWSInstance_associatePublic_overridePublic(t *testing.T) { func TestAccAWSInstance_associatePublic_overridePrivate(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1796,7 +1796,7 @@ func TestAccAWSInstance_associatePublic_overridePrivate(t *testing.T) { func TestAccAWSInstance_getPasswordData_falseToTrue(t *testing.T) { var before, after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1832,7 +1832,7 @@ func TestAccAWSInstance_getPasswordData_falseToTrue(t *testing.T) { func TestAccAWSInstance_getPasswordData_trueToFalse(t *testing.T) { var before, after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1869,7 +1869,7 @@ func TestAccAWSInstance_getPasswordData_trueToFalse(t *testing.T) { func TestAccAWSInstance_CreditSpecification_Empty_NonBurstable(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1896,7 +1896,7 @@ func TestAccAWSInstance_CreditSpecification_Empty_NonBurstable(t *testing.T) { func TestAccAWSInstance_CreditSpecification_UnspecifiedToEmpty_NonBurstable(t *testing.T) { var instance ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1927,7 +1927,7 @@ func TestAccAWSInstance_CreditSpecification_UnspecifiedToEmpty_NonBurstable(t *t func TestAccAWSInstance_creditSpecification_unspecifiedDefaultsToStandard(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1954,7 +1954,7 @@ func TestAccAWSInstance_creditSpecification_unspecifiedDefaultsToStandard(t *tes func TestAccAWSInstance_creditSpecification_standardCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1989,7 +1989,7 @@ func TestAccAWSInstance_creditSpecification_standardCpuCredits(t *testing.T) { func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2024,7 +2024,7 @@ func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits(t *testing.T) { func TestAccAWSInstance_creditSpecification_unknownCpuCredits_t2(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2051,7 +2051,7 @@ func TestAccAWSInstance_creditSpecification_unknownCpuCredits_t2(t *testing.T) { func TestAccAWSInstance_creditSpecification_unknownCpuCredits_t3(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2078,7 +2078,7 @@ func TestAccAWSInstance_creditSpecification_unknownCpuCredits_t3(t *testing.T) { func TestAccAWSInstance_creditSpecification_updateCpuCredits(t *testing.T) { var first, second, third ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2121,7 +2121,7 @@ func TestAccAWSInstance_creditSpecification_updateCpuCredits(t *testing.T) { func TestAccAWSInstance_creditSpecification_isNotAppliedToNonBurstable(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2147,7 +2147,7 @@ func TestAccAWSInstance_creditSpecification_isNotAppliedToNonBurstable(t *testin func TestAccAWSInstance_creditSpecificationT3_unspecifiedDefaultsToUnlimited(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2174,7 +2174,7 @@ func TestAccAWSInstance_creditSpecificationT3_unspecifiedDefaultsToUnlimited(t * func TestAccAWSInstance_creditSpecificationT3_standardCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2209,7 +2209,7 @@ func TestAccAWSInstance_creditSpecificationT3_standardCpuCredits(t *testing.T) { func TestAccAWSInstance_creditSpecificationT3_unlimitedCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2244,7 +2244,7 @@ func TestAccAWSInstance_creditSpecificationT3_unlimitedCpuCredits(t *testing.T) func TestAccAWSInstance_creditSpecificationT3_updateCpuCredits(t *testing.T) { var first, second, third ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2287,7 +2287,7 @@ func TestAccAWSInstance_creditSpecificationT3_updateCpuCredits(t *testing.T) { func TestAccAWSInstance_creditSpecification_standardCpuCredits_t2Tot3Taint(t *testing.T) { var before, after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2323,7 +2323,7 @@ func TestAccAWSInstance_creditSpecification_standardCpuCredits_t2Tot3Taint(t *te func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits_t2Tot3Taint(t *testing.T) { var before, after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2359,7 +2359,7 @@ func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits_t2Tot3Taint(t *t func TestAccAWSInstance_disappears(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2381,7 +2381,7 @@ func TestAccAWSInstance_disappears(t *testing.T) { func TestAccAWSInstance_UserData_EmptyStringToUnspecified(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2413,7 +2413,7 @@ func TestAccAWSInstance_UserData_EmptyStringToUnspecified(t *testing.T) { func TestAccAWSInstance_UserData_UnspecifiedToEmptyString(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandStringFromCharSet(12, acctest.CharSetAlphaNum)) + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, From 19447d6a6f3a3d996d284b57c3cb813fb3458e1d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 5 Dec 2019 10:54:35 -0500 Subject: [PATCH 14/17] d/aws_instance: Region-agnostic acceptance tests. --- aws/data_source_aws_instance_test.go | 305 +++++++++++++++++--------- aws/data_source_aws_instances_test.go | 4 +- aws/resource_aws_instance_test.go | 2 +- 3 files changed, 199 insertions(+), 112 deletions(-) diff --git a/aws/data_source_aws_instance_test.go b/aws/data_source_aws_instance_test.go index d62d200c86b..9b816df8627 100644 --- a/aws/data_source_aws_instance_test.go +++ b/aws/data_source_aws_instance_test.go @@ -14,13 +14,19 @@ func TestAccAWSInstanceDataSource_basic(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_basic(rName), + Config: testAccInstanceDataSourceConfig_basic(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), @@ -38,13 +44,19 @@ func TestAccAWSInstanceDataSource_tags(t *testing.T) { datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) rInt := acctest.RandInt() + instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_Tags(rName, rInt), + Config: testAccInstanceDataSourceConfig_Tags(rName, rInt, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), @@ -59,13 +71,19 @@ func TestAccAWSInstanceDataSource_AzUserData(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_AzUserData(rName), + Config: testAccInstanceDataSourceConfig_AzUserData(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), @@ -82,13 +100,19 @@ func TestAccAWSInstanceDataSource_gp2IopsDevice(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_gp2IopsDevice(rName), + Config: testAccInstanceDataSourceConfig_gp2IopsDevice(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -106,13 +130,19 @@ func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_blockDevices(rName), + Config: testAccInstanceDataSourceConfig_blockDevices(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -131,13 +161,19 @@ func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) { // Test to verify that ebs_block_device kms_key_id does not elicit a panic func TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId(t *testing.T) { rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId(rName), + Config: testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId(rName, instanceType), }, }, }) @@ -146,13 +182,19 @@ func TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId(t *testing.T) { // Test to verify that root_block_device kms_key_id does not elicit a panic func TestAccAWSInstanceDataSource_RootBlockDevice_KmsKeyId(t *testing.T) { rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId(rName), + Config: testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId(rName, instanceType), }, }, }) @@ -162,13 +204,19 @@ func TestAccAWSInstanceDataSource_rootInstanceStore(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_rootInstanceStore(rName), + Config: testAccInstanceDataSourceConfig_rootInstanceStore(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -185,13 +233,14 @@ func TestAccAWSInstanceDataSource_privateIP(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_privateIP(rName), + Config: testAccInstanceDataSourceConfig_privateIP(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -206,13 +255,19 @@ func TestAccAWSInstanceDataSource_keyPair(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_keyPair(rName), + Config: testAccInstanceDataSourceConfig_keyPair(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"), @@ -228,13 +283,14 @@ func TestAccAWSInstanceDataSource_VPC(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_VPC(rName), + Config: testAccInstanceDataSourceConfig_VPC(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -251,13 +307,14 @@ func TestAccAWSInstanceDataSource_PlacementGroup(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "c3.large" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_PlacementGroup(rName), + Config: testAccInstanceDataSourceConfig_PlacementGroup(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "placement_group", resourceName, "placement_group"), ), @@ -276,6 +333,7 @@ func TestAccAWSInstanceDataSource_Ec2ClassicSecurityGroups(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" // EC2 Classic enabled oldvar := os.Getenv("AWS_DEFAULT_REGION") @@ -283,11 +341,15 @@ func TestAccAWSInstanceDataSource_Ec2ClassicSecurityGroups(t *testing.T) { defer os.Setenv("AWS_DEFAULT_REGION", oldvar) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) }, + PreCheck: func() { + testAccPreCheck(t) + testAccEC2ClassicPreCheck(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_Ec2ClassicSecurityGroups(rName), + Config: testAccInstanceDataSourceConfig_Ec2ClassicSecurityGroups(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -304,13 +366,14 @@ func TestAccAWSInstanceDataSource_VPCSecurityGroups(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_VPCSecurityGroups(rName), + Config: testAccInstanceDataSourceConfig_VPCSecurityGroups(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"), resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), @@ -325,20 +388,28 @@ func TestAccAWSInstanceDataSource_VPCSecurityGroups(t *testing.T) { func TestAccAWSInstanceDataSource_getPasswordData_trueToFalse(t *testing.T) { datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic, + // but t2.medium can't be launched in EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccEC2VPCOnlyPreCheck(t) + testAccPreCheckHasDefaultVpc(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_getPasswordData(rName, true), + Config: testAccInstanceDataSourceConfig_getPasswordData(rName, true, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "get_password_data", "true"), resource.TestCheckResourceAttrSet(datasourceName, "password_data"), ), }, { - Config: testAccInstanceDataSourceConfig_getPasswordData(rName, false), + Config: testAccInstanceDataSourceConfig_getPasswordData(rName, false, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "get_password_data", "false"), resource.TestCheckNoResourceAttr(datasourceName, "password_data"), @@ -351,20 +422,28 @@ func TestAccAWSInstanceDataSource_getPasswordData_trueToFalse(t *testing.T) { func TestAccAWSInstanceDataSource_getPasswordData_falseToTrue(t *testing.T) { datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic, + // but t2.medium can't be launched in EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccEC2VPCOnlyPreCheck(t) + testAccPreCheckHasDefaultVpc(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_getPasswordData(rName, false), + Config: testAccInstanceDataSourceConfig_getPasswordData(rName, false, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "get_password_data", "false"), resource.TestCheckNoResourceAttr(datasourceName, "password_data"), ), }, { - Config: testAccInstanceDataSourceConfig_getPasswordData(rName, true), + Config: testAccInstanceDataSourceConfig_getPasswordData(rName, true, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "get_password_data", "true"), resource.TestCheckResourceAttrSet(datasourceName, "password_data"), @@ -377,27 +456,28 @@ func TestAccAWSInstanceDataSource_getPasswordData_falseToTrue(t *testing.T) { func TestAccAWSInstanceDataSource_GetUserData(t *testing.T) { datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfigGetUserData(rName, true), + Config: testAccInstanceDataSourceConfigGetUserData(rName, true, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "get_user_data", "true"), resource.TestCheckResourceAttr(datasourceName, "user_data_base64", "IyEvYmluL2Jhc2gKCmVjaG8gImhlbGxvIHdvcmxkIgo="), ), }, { - Config: testAccInstanceDataSourceConfigGetUserData(rName, false), + Config: testAccInstanceDataSourceConfigGetUserData(rName, false, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "get_user_data", "false"), resource.TestCheckNoResourceAttr(datasourceName, "user_data_base64"), ), }, { - Config: testAccInstanceDataSourceConfigGetUserData(rName, true), + Config: testAccInstanceDataSourceConfigGetUserData(rName, true, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "get_user_data", "true"), resource.TestCheckResourceAttr(datasourceName, "user_data_base64", "IyEvYmluL2Jhc2gKCmVjaG8gImhlbGxvIHdvcmxkIgo="), @@ -411,13 +491,14 @@ func TestAccAWSInstanceDataSource_GetUserData_NoUserData(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfigGetUserDataNoUserData(rName, true), + Config: testAccInstanceDataSourceConfigGetUserDataNoUserData(rName, true, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "get_user_data", "true"), resource.TestCheckNoResourceAttr(datasourceName, "user_data_base64"), @@ -425,7 +506,7 @@ func TestAccAWSInstanceDataSource_GetUserData_NoUserData(t *testing.T) { ), }, { - Config: testAccInstanceDataSourceConfigGetUserDataNoUserData(rName, false), + Config: testAccInstanceDataSourceConfigGetUserDataNoUserData(rName, false, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "get_user_data", "false"), resource.TestCheckNoResourceAttr(datasourceName, "user_data_base64"), @@ -433,7 +514,7 @@ func TestAccAWSInstanceDataSource_GetUserData_NoUserData(t *testing.T) { ), }, { - Config: testAccInstanceDataSourceConfigGetUserDataNoUserData(rName, true), + Config: testAccInstanceDataSourceConfigGetUserDataNoUserData(rName, true, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(datasourceName, "get_user_data", "true"), resource.TestCheckNoResourceAttr(datasourceName, "user_data_base64"), @@ -448,14 +529,15 @@ func TestAccAWSInstanceDataSource_creditSpecification(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccInstanceDataSourceConfig_creditSpecification(rName), + Config: testAccInstanceDataSourceConfig_creditSpecification(rName, instanceType), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), resource.TestCheckResourceAttrPair(datasourceName, "credit_specification.#", resourceName, "credit_specification.#"), @@ -467,11 +549,11 @@ func TestAccAWSInstanceDataSource_creditSpecification(t *testing.T) { } // Lookup based on InstanceID -func testAccInstanceDataSourceConfig_basic(rName string) string { +func testAccInstanceDataSourceConfig_basic(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m1.small" + instance_type = %[2]q tags = { Name = %[1]q @@ -484,15 +566,15 @@ data "aws_instance" "test" { values = ["${aws_instance.test.id}"] } } -`, rName) +`, rName, instanceType) } // Use the tags attribute to filter -func testAccInstanceDataSourceConfig_Tags(rName string, rInt int) string { +func testAccInstanceDataSourceConfig_Tags(rName string, rInt int, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m1.small" + instance_type = %[3]q tags = { Name = %[1]q @@ -506,16 +588,21 @@ data "aws_instance" "test" { TestSeed = "%[2]d" } } -`, rName, rInt) +`, rName, rInt, instanceType) } // filter on tag, populate more attributes -func testAccInstanceDataSourceConfig_AzUserData(rName string) string { +func testAccInstanceDataSourceConfig_AzUserData(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` +data "aws_availability_zones" "current" { + # Exclude usw2-az4 (us-west-2d) as it has limited instance types. + blacklisted_zone_ids = ["usw2-az4"] +} + resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - availability_zone = "us-west-2a" - instance_type = "m1.small" + availability_zone = "${data.aws_availability_zones.current.names[0]}" + instance_type = %[2]q user_data = "test:-with-character's" tags = { @@ -526,15 +613,15 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } // GP2IopsDevice -func testAccInstanceDataSourceConfig_gp2IopsDevice(rName string) string { - return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` +func testAccInstanceDataSourceConfig_gp2IopsDevice(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" - instance_type = "m3.medium" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q root_block_device { volume_type = "gp2" @@ -549,15 +636,15 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } // Block Device -func testAccInstanceDataSourceConfig_blockDevices(rName string) string { - return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` +func testAccInstanceDataSourceConfig_blockDevices(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" - instance_type = "m3.medium" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q root_block_device { volume_type = "gp2" @@ -594,18 +681,18 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } -func testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId(rName string) string { - return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` +func testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_kms_key" "test" { deletion_window_in_days = 7 } resource "aws_instance" "test" { - ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" - instance_type = "m3.medium" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q root_block_device { volume_type = "gp2" @@ -626,18 +713,18 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } -func testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId(rName string) string { - return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` +func testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_kms_key" "test" { deletion_window_in_days = 7 } resource "aws_instance" "test" { - ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" - instance_type = "m3.medium" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q root_block_device { encrypted = true @@ -654,14 +741,14 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } -func testAccInstanceDataSourceConfig_rootInstanceStore(rName string) string { +func testAccInstanceDataSourceConfig_rootInstanceStore(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmInstanceStoreAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-instance-store.id}" - instance_type = "m3.medium" + instance_type = %[2]q tags = { Name = %[1]q @@ -671,14 +758,14 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } -func testAccInstanceDataSourceConfig_privateIP(rName string) string { +func testAccInstanceDataSourceConfig_privateIP(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" private_ip = "10.1.1.42" @@ -690,10 +777,10 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } -func testAccInstanceDataSourceConfig_keyPair(rName string) string { +func testAccInstanceDataSourceConfig_keyPair(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_key_pair" "test" { key_name = %[1]q @@ -702,7 +789,7 @@ resource "aws_key_pair" "test" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t1.micro" + instance_type = %[2]q key_name = "${aws_key_pair.test.key_name}" tags = { @@ -721,14 +808,14 @@ data "aws_instance" "test" { values = ["${aws_instance.test.key_name}"] } } -`, rName) +`, rName, instanceType) } -func testAccInstanceDataSourceConfig_VPC(rName string) string { +func testAccInstanceDataSourceConfig_VPC(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m1.small" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = true tenancy = "dedicated" @@ -743,10 +830,10 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } -func testAccInstanceDataSourceConfig_PlacementGroup(rName string) string { +func testAccInstanceDataSourceConfig_PlacementGroup(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_placement_group" "test" { name = %[1]q @@ -756,7 +843,7 @@ resource "aws_placement_group" "test" { # Limitations: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/placement-groups.html#concepts-placement-groups resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "c3.large" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = true placement_group = "${aws_placement_group.test.name}" @@ -772,11 +859,11 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } -func testAccInstanceDataSourceConfig_Ec2ClassicSecurityGroups(rName string) string { - return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` +func testAccInstanceDataSourceConfig_Ec2ClassicSecurityGroups(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_security_group" "test" { name = %[1]q description = %[1]q @@ -794,8 +881,8 @@ resource "aws_security_group" "test" { } resource "aws_instance" "test" { - ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" - instance_type = "m1.small" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q security_groups = ["${aws_security_group.test.name}"] user_data = "foo:-with-character's" @@ -807,17 +894,17 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } -func testAccInstanceDataSourceConfig_VPCSecurityGroups(rName string) string { +func testAccInstanceDataSourceConfig_VPCSecurityGroups(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + testAccAwsInstanceVpcSecurityGroupConfig(rName) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t1.micro" + instance_type = %[2]q vpc_security_group_ids = ["${aws_security_group.test.id}"] subnet_id = "${aws_subnet.test.id}" depends_on = ["aws_internet_gateway.test"] @@ -830,10 +917,10 @@ resource "aws_instance" "test" { data "aws_instance" "test" { instance_id = "${aws_instance.test.id}" } -`, rName) +`, rName, instanceType) } -func testAccInstanceDataSourceConfig_getPasswordData(rName string, val bool) string { +func testAccInstanceDataSourceConfig_getPasswordData(rName string, val bool, instanceType string) string { return testAccLatestWindowsServer2016CoreAmiConfig() + fmt.Sprintf(` resource "aws_key_pair" "test" { key_name = %[1]q @@ -842,7 +929,7 @@ resource "aws_key_pair" "test" { resource "aws_instance" "test" { ami = "${data.aws_ami.win2016core-ami.id}" - instance_type = "t2.medium" + instance_type = %[3]q key_name = "${aws_key_pair.test.key_name}" tags = { @@ -855,14 +942,14 @@ data "aws_instance" "test" { get_password_data = %[2]t } -`, rName, val) +`, rName, val, instanceType) } -func testAccInstanceDataSourceConfigGetUserData(rName string, getUserData bool) string { +func testAccInstanceDataSourceConfigGetUserData(rName string, val bool, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[3]q subnet_id = "${aws_subnet.test.id}" user_data = < Date: Thu, 5 Dec 2019 17:05:10 -0500 Subject: [PATCH 15/17] r/aws_instance: Region-agnostic acceptance tests. --- aws/data_source_aws_instance_test.go | 22 +- aws/provider_test.go | 7 + aws/resource_aws_instance_test.go | 1737 +++++++++++++++----------- 3 files changed, 1053 insertions(+), 713 deletions(-) diff --git a/aws/data_source_aws_instance_test.go b/aws/data_source_aws_instance_test.go index 9b816df8627..2046982aeeb 100644 --- a/aws/data_source_aws_instance_test.go +++ b/aws/data_source_aws_instance_test.go @@ -2,7 +2,6 @@ package aws import ( "fmt" - "os" "regexp" "testing" @@ -161,6 +160,7 @@ func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) { // Test to verify that ebs_block_device kms_key_id does not elicit a panic func TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId(t *testing.T) { rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ @@ -182,6 +182,7 @@ func TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId(t *testing.T) { // Test to verify that root_block_device kms_key_id does not elicit a panic func TestAccAWSInstanceDataSource_RootBlockDevice_KmsKeyId(t *testing.T) { rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ @@ -324,22 +325,11 @@ func TestAccAWSInstanceDataSource_PlacementGroup(t *testing.T) { } func TestAccAWSInstanceDataSource_Ec2ClassicSecurityGroups(t *testing.T) { - key := "EC2_CLASSIC_REGION" - ec2ClassicRegion := os.Getenv(key) - if ec2ClassicRegion == "" { - t.Skipf("%s must be set to run EC2-Classic acceptance tests", key) - } - resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) instanceType := "m1.small" - // EC2 Classic enabled - oldvar := os.Getenv("AWS_DEFAULT_REGION") - os.Setenv("AWS_DEFAULT_REGION", ec2ClassicRegion) - defer os.Setenv("AWS_DEFAULT_REGION", oldvar) - resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) @@ -688,6 +678,10 @@ func testAccInstanceDataSourceConfig_EbsBlockDevice_KmsKeyId(rName, instanceType return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_kms_key" "test" { deletion_window_in_days = 7 + + tags = { + Name = %[1]q + } } resource "aws_instance" "test" { @@ -720,6 +714,10 @@ func testAccInstanceDataSourceConfig_RootBlockDevice_KmsKeyId(rName, instanceTyp return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_kms_key" "test" { deletion_window_in_days = 7 + + tags = { + Name = %[1]q + } } resource "aws_instance" "test" { diff --git a/aws/provider_test.go b/aws/provider_test.go index 40d07ec69bd..c544b4089e8 100644 --- a/aws/provider_test.go +++ b/aws/provider_test.go @@ -408,6 +408,13 @@ func testAccMultipleRegionsPreCheck(t *testing.T) { } } +// testAccRegionPreCheck checks that the test region is the specified region. +func testAccRegionPreCheck(t *testing.T, region string) { + if testAccGetRegion() != region { + t.Skipf("skipping tests; AWS_DEFAULT_REGION (%s) does not equal %s", testAccGetRegion(), region) + } +} + func testAccOrganizationsAccountPreCheck(t *testing.T) { conn := testAccProvider.Meta().(*AWSClient).organizationsconn input := &organizations.DescribeOrganizationInput{} diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 0ebeb53030d..d992c3d4a7c 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -3,7 +3,6 @@ package aws import ( "fmt" "log" - "os" "reflect" "regexp" "testing" @@ -150,14 +149,19 @@ func TestAccAWSInstance_inDefaultVpcBySgName(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpc(t) }, + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpc(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigInDefaultVpcBySgName(rName), + Config: testAccInstanceConfigInDefaultVpcBySgName(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -176,14 +180,19 @@ func TestAccAWSInstance_inDefaultVpcBySgId(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpc(t) }, + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpc(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigInDefaultVpcBySgId(rName), + Config: testAccInstanceConfigInDefaultVpcBySgId(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -199,28 +208,22 @@ func TestAccAWSInstance_inDefaultVpcBySgId(t *testing.T) { } func TestAccAWSInstance_inEc2Classic(t *testing.T) { - key := "EC2_CLASSIC_REGION" - ec2ClassicRegion := os.Getenv(key) - if ec2ClassicRegion == "" { - t.Skipf("%s must be set to run EC2-Classic acceptance tests", key) - } - var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) - - // EC2 Classic enabled - oldvar := os.Getenv("AWS_DEFAULT_REGION") - os.Setenv("AWS_DEFAULT_REGION", ec2ClassicRegion) - defer os.Setenv("AWS_DEFAULT_REGION", oldvar) + instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) }, + PreCheck: func() { + testAccPreCheck(t) + testAccEC2ClassicPreCheck(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigInEc2Classic(rName), + Config: testAccInstanceConfigInEc2Classic(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -239,45 +242,72 @@ func TestAccAWSInstance_inEc2Classic(t *testing.T) { func TestAccAWSInstance_basic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" - testCheck := func() func(*terraform.State) error { - return func(*terraform.State) error { - if len(v.SecurityGroups) == 0 { - return fmt.Errorf("no security groups: %#v", v.SecurityGroups) - } - if *v.SecurityGroups[0].GroupName != rName { - return fmt.Errorf("incorrect security group name: %#v", v.SecurityGroups) - } + resource.ParallelTest(t, resource.TestCase{ + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, + IDRefreshName: resourceName, + Providers: testAccProviders, + CheckDestroy: testAccCheckInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccInstanceConfigBasic(instanceType), + Check: resource.ComposeTestCheckFunc( + testAccCheckInstanceExists( + resourceName, &v), + resource.TestCheckResourceAttr( + resourceName, + "instance_type", + instanceType), + testAccMatchResourceAttrRegionalARN( + resourceName, + "arn", + "ec2", + regexp.MustCompile(`instance/i-.+`)), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} - return nil - } - } +func TestAccAWSInstance_atLeastOneOtherEbsVolume(t *testing.T) { + var v ec2.Instance + resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ - // TODO Does the instance run in default VPC even if in us-east-1 and have EC2-Classic? - // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-classic-platform.html - PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasDefaultVpc(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigBasic(rName), + Config: testAccInstanceConfigAtLeastOneOtherEbsVolume(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), - testCheck(), resource.TestCheckResourceAttr( resourceName, "user_data", "3dc39dda39be1205215e776bad998da361a5955d"), resource.TestCheckResourceAttr( resourceName, "ebs_block_device.#", "0"), - resource.TestMatchResourceAttr( - resourceName, - "arn", - regexp.MustCompile(`^arn:[^:]+:ec2:[^:]+:\d{12}:instance/i-.+`)), ), }, { @@ -289,7 +319,7 @@ func TestAccAWSInstance_basic(t *testing.T) { // that the user data hash stuff is working without generating // an incorrect diff. { - Config: testAccInstanceConfigBasic(rName), + Config: testAccInstanceConfigAtLeastOneOtherEbsVolume(rName, instanceType), PlanOnly: true, }, }, @@ -301,14 +331,21 @@ func TestAccAWSInstance_EbsBlockDevice_KmsKeyArn(t *testing.T) { resourceName := "aws_instance.test" kmsKeyResourceName := "aws_kms_key.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. + instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigEbsBlockDeviceKmsKeyArn(rName), + Config: testAccInstanceConfigEbsBlockDeviceKmsKeyArn(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "ebs_block_device.#", "1"), @@ -321,19 +358,22 @@ func TestAccAWSInstance_EbsBlockDevice_KmsKeyArn(t *testing.T) { } func TestAccAWSInstance_RootBlockDevice_KmsKeyArn(t *testing.T) { - var instance ec2.Instance - kmsKeyResourceName := "aws_kms_key.test" + var v ec2.Instance resourceName := "aws_instance.test" + kmsKeyResourceName := "aws_kms_key.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. + instanceType := "t3.nano" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigRootBlockDeviceKmsKeyArn, + Config: testAccInstanceConfigRootBlockDeviceKmsKeyArn(rName, instanceType), Check: resource.ComposeTestCheckFunc( - testAccCheckInstanceExists(resourceName, &instance), + testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "root_block_device.#", "1"), resource.TestCheckResourceAttr(resourceName, "root_block_device.0.encrypted", "true"), resource.TestCheckResourceAttrPair(resourceName, "root_block_device.0.kms_key_id", kmsKeyResourceName, "arn"), @@ -351,16 +391,22 @@ func TestAccAWSInstance_RootBlockDevice_KmsKeyArn(t *testing.T) { func TestAccAWSInstance_userDataBase64(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rInt := acctest.RandInt() + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigWithUserDataBase64(rInt), + Config: testAccInstanceConfigWithUserDataBase64(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -383,6 +429,8 @@ func TestAccAWSInstance_userDataBase64(t *testing.T) { func TestAccAWSInstance_GP2IopsDevice(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m3.medium" testCheck := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -394,8 +442,8 @@ func TestAccAWSInstance_GP2IopsDevice(t *testing.T) { } // Check if the root block device exists. - if _, ok := blockDevices["/dev/sda1"]; !ok { - return fmt.Errorf("block device doesn't exist: /dev/sda1") + if _, ok := blockDevices["/dev/xvda"]; !ok { + return fmt.Errorf("block device doesn't exist: /dev/xvda") } return nil @@ -403,14 +451,19 @@ func TestAccAWSInstance_GP2IopsDevice(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, IDRefreshName: resourceName, IDRefreshIgnore: []string{"ephemeral_block_device", "user_data"}, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceGP2IopsDevice, + Config: testAccInstanceGP2IopsDevice(rName, instanceType), //Config: testAccInstanceConfigBlockDevices, Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( @@ -438,16 +491,23 @@ func TestAccAWSInstance_GP2IopsDevice(t *testing.T) { func TestAccAWSInstance_GP2WithIopsValue(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, IDRefreshName: resourceName, IDRefreshIgnore: []string{"ephemeral_block_device", "user_data"}, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceGP2WithIopsValue, + Config: testAccInstanceGP2WithIopsValue(rName, instanceType), Check: testAccCheckInstanceExists(resourceName, &v), }, { @@ -456,7 +516,7 @@ func TestAccAWSInstance_GP2WithIopsValue(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceGP2WithIopsValue, + Config: testAccInstanceGP2WithIopsValue(rName, instanceType), PlanOnly: true, ExpectNonEmptyPlan: false, }, @@ -467,6 +527,9 @@ func TestAccAWSInstance_GP2WithIopsValue(t *testing.T) { func TestAccAWSInstance_blockDevices(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. + instanceType := "m3.medium" testCheck := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -478,8 +541,8 @@ func TestAccAWSInstance_blockDevices(t *testing.T) { } // Check if the root block device exists. - if _, ok := blockDevices["/dev/sda1"]; !ok { - return fmt.Errorf("block device doesn't exist: /dev/sda1") + if _, ok := blockDevices["/dev/xvda"]; !ok { + return fmt.Errorf("block device doesn't exist: /dev/xvda") } // Check if the secondary block device exists. @@ -502,14 +565,19 @@ func TestAccAWSInstance_blockDevices(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, IDRefreshName: resourceName, IDRefreshIgnore: []string{"ephemeral_block_device"}, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigBlockDevices, + Config: testAccInstanceConfigBlockDevices(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -571,36 +639,32 @@ func TestAccAWSInstance_blockDevices(t *testing.T) { func TestAccAWSInstance_rootInstanceStore(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html#instance-store-volumes. + instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: ` - resource "aws_instance" "test" { - # us-west-2 - # Amazon Linux HVM Instance Store 64-bit (2016.09.0) - # https://aws.amazon.com/amazon-linux-ami - ami = "ami-44c36524" - - # Only certain instance types support ephemeral root instance stores. - # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html - instance_type = "m3.medium" - }`, + Config: testAccInstanceConfigRootInstanceStore(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), - resource.TestCheckResourceAttr( - resourceName, "ami", "ami-44c36524"), resource.TestCheckResourceAttr( resourceName, "ebs_block_device.#", "0"), resource.TestCheckResourceAttr( resourceName, "ebs_optimized", "false"), resource.TestCheckResourceAttr( - resourceName, "instance_type", "m3.medium"), + resourceName, "instance_type", instanceType), resource.TestCheckResourceAttr( resourceName, "root_block_device.#", "0"), ), @@ -617,6 +681,8 @@ func TestAccAWSInstance_rootInstanceStore(t *testing.T) { func TestAccAWSInstance_noAMIEphemeralDevices(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "c3.large" testCheck := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -646,42 +712,26 @@ func TestAccAWSInstance_noAMIEphemeralDevices(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, IDRefreshName: resourceName, IDRefreshIgnore: []string{"ephemeral_block_device"}, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: ` - resource "aws_instance" "test" { - # us-west-2 - ami = "ami-01f05461" // This AMI (Ubuntu) contains two ephemerals - - instance_type = "c3.large" - - root_block_device { - volume_type = "gp2" - volume_size = 11 - } - ephemeral_block_device { - device_name = "/dev/sdb" - no_device = true - } - ephemeral_block_device { - device_name = "/dev/sdc" - no_device = true - } - }`, + Config: testAccInstanceConfigNoAMIEphemeralDevices(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), - resource.TestCheckResourceAttr( - resourceName, "ami", "ami-01f05461"), resource.TestCheckResourceAttr( resourceName, "ebs_optimized", "false"), resource.TestCheckResourceAttr( - resourceName, "instance_type", "c3.large"), + resourceName, "instance_type", instanceType), resource.TestCheckResourceAttr( resourceName, "root_block_device.#", "1"), resource.TestCheckResourceAttr( @@ -717,6 +767,7 @@ func TestAccAWSInstance_sourceDestCheck(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" testCheck := func(enabled bool) resource.TestCheckFunc { return func(*terraform.State) error { @@ -732,13 +783,13 @@ func TestAccAWSInstance_sourceDestCheck(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigSourceDestDisable(rName), + Config: testAccInstanceConfigSourceDestDisable(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testCheck(false), @@ -750,14 +801,14 @@ func TestAccAWSInstance_sourceDestCheck(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfigSourceDestEnable(rName), + Config: testAccInstanceConfigSourceDestEnable(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testCheck(true), ), }, { - Config: testAccInstanceConfigSourceDestDisable(rName), + Config: testAccInstanceConfigSourceDestDisable(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testCheck(false), @@ -771,6 +822,7 @@ func TestAccAWSInstance_disableApiTermination(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" checkDisableApiTermination := func(expected bool) resource.TestCheckFunc { return func(*terraform.State) error { @@ -791,13 +843,13 @@ func TestAccAWSInstance_disableApiTermination(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigDisableAPITermination(rName, true), + Config: testAccInstanceConfigDisableAPITermination(rName, true, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), checkDisableApiTermination(true), @@ -809,7 +861,7 @@ func TestAccAWSInstance_disableApiTermination(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfigDisableAPITermination(rName, false), + Config: testAccInstanceConfigDisableAPITermination(rName, false, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), checkDisableApiTermination(false), @@ -823,16 +875,17 @@ func TestAccAWSInstance_vpc(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, IDRefreshName: resourceName, IDRefreshIgnore: []string{"associate_public_ip_address"}, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigVPC(rName), + Config: testAccInstanceConfigVPC(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -856,16 +909,17 @@ func TestAccAWSInstance_placementGroup(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "c3.large" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, IDRefreshName: resourceName, IDRefreshIgnore: []string{"associate_public_ip_address"}, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigPlacementGroup(rName), + Config: testAccInstanceConfigPlacementGroup(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -888,14 +942,15 @@ func TestAccAWSInstance_ipv6_supportAddressCount(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigIpv6Support(rName), + Config: testAccInstanceConfigIpv6Support(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -916,14 +971,15 @@ func TestAccAWSInstance_ipv6_supportAddressCount(t *testing.T) { func TestAccAWSInstance_ipv6AddressCountAndSingleAddressCausesError(t *testing.T) { rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigIpv6ErrorConfig(rName), + Config: testAccInstanceConfigIpv6ErrorConfig(rName, instanceType), ExpectError: regexp.MustCompile("Only 1 of `ipv6_address_count` or `ipv6_addresses` can be specified"), }, }, @@ -934,14 +990,15 @@ func TestAccAWSInstance_ipv6_supportAddressCountWithIpv4(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigIpv6SupportWithIpv4(rName), + Config: testAccInstanceConfigIpv6SupportWithIpv4(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -964,16 +1021,17 @@ func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, IDRefreshName: resourceName, IDRefreshIgnore: []string{"associate_public_ip_address"}, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceNetworkInstanceSecurityGroups(rName), + Config: testAccInstanceNetworkInstanceSecurityGroups(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -992,15 +1050,16 @@ func TestAccAWSInstance_NetworkInstanceRemovingAllSecurityGroups(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs(rName), + Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -1016,7 +1075,7 @@ func TestAccAWSInstance_NetworkInstanceRemovingAllSecurityGroups(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceNetworkInstanceVPCRemoveSecurityGroupIDs(rName), + Config: testAccInstanceNetworkInstanceVPCRemoveSecurityGroupIDs(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -1035,15 +1094,16 @@ func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs(rName), + Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists( resourceName, &v), @@ -1065,14 +1125,21 @@ func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) { func TestAccAWSInstance_tags(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccCheckInstanceConfigTags, + Config: testAccCheckInstanceConfigTags(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testAccCheckTags(&v.Tags, "test", "test2"), @@ -1086,7 +1153,7 @@ func TestAccAWSInstance_tags(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccCheckInstanceConfigTagsUpdate, + Config: testAccCheckInstanceConfigTagsUpdate(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testAccCheckTags(&v.Tags, "test", ""), @@ -1100,14 +1167,22 @@ func TestAccAWSInstance_tags(t *testing.T) { func TestAccAWSInstance_volumeTags(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. + instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccCheckInstanceConfigNoVolumeTags, + Config: testAccCheckInstanceConfigNoVolumeTags(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckNoResourceAttr( @@ -1121,29 +1196,29 @@ func TestAccAWSInstance_volumeTags(t *testing.T) { ImportStateVerifyIgnore: []string{"ephemeral_block_device"}, }, { - Config: testAccCheckInstanceConfigWithVolumeTags, + Config: testAccCheckInstanceConfigWithVolumeTags(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr( resourceName, "volume_tags.%", "1"), resource.TestCheckResourceAttr( - resourceName, "volume_tags.Name", "acceptance-test-volume-tag"), + resourceName, "volume_tags.Name", rName), ), }, { - Config: testAccCheckInstanceConfigWithVolumeTagsUpdate, + Config: testAccCheckInstanceConfigWithVolumeTagsUpdate(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr( resourceName, "volume_tags.%", "2"), resource.TestCheckResourceAttr( - resourceName, "volume_tags.Name", "acceptance-test-volume-tag"), + resourceName, "volume_tags.Name", rName), resource.TestCheckResourceAttr( resourceName, "volume_tags.Environment", "dev"), ), }, { - Config: testAccCheckInstanceConfigNoVolumeTags, + Config: testAccCheckInstanceConfigNoVolumeTags(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckNoResourceAttr( @@ -1157,14 +1232,23 @@ func TestAccAWSInstance_volumeTags(t *testing.T) { func TestAccAWSInstance_volumeTagsComputed(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic, + // but t2.medium can't be launched in EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccEC2VPCOnlyPreCheck(t) + testAccPreCheckHasDefaultVpc(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccCheckInstanceConfigWithAttachedVolume, + Config: testAccCheckInstanceConfigWithAttachedVolume(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), ), @@ -1183,6 +1267,7 @@ func TestAccAWSInstance_instanceProfileChange(t *testing.T) { resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) rName2 := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" testCheckInstanceProfile := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -1195,13 +1280,18 @@ func TestAccAWSInstance_instanceProfileChange(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigWithoutInstanceProfile(rName), + Config: testAccInstanceConfigWithoutInstanceProfile(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), ), @@ -1212,20 +1302,20 @@ func TestAccAWSInstance_instanceProfileChange(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfigWithInstanceProfile(rName), + Config: testAccInstanceConfigWithInstanceProfile(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testCheckInstanceProfile(), ), }, { - Config: testAccInstanceConfigWithInstanceProfile(rName), + Config: testAccInstanceConfigWithInstanceProfile(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckStopInstance(&v), // GH-8262 ), }, { - Config: testAccInstanceConfigWithInstanceProfile(rName2), + Config: testAccInstanceConfigWithInstanceProfile(rName2, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testCheckInstanceProfile(), @@ -1239,6 +1329,7 @@ func TestAccAWSInstance_withIamInstanceProfile(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" testCheckInstanceProfile := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -1251,13 +1342,18 @@ func TestAccAWSInstance_withIamInstanceProfile(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigWithInstanceProfile(rName), + Config: testAccInstanceConfigWithInstanceProfile(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testCheckInstanceProfile(), @@ -1276,6 +1372,7 @@ func TestAccAWSInstance_privateIP(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" testCheckPrivateIP := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -1288,13 +1385,13 @@ func TestAccAWSInstance_privateIP(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigPrivateIP(rName), + Config: testAccInstanceConfigPrivateIP(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testCheckPrivateIP(), @@ -1313,6 +1410,7 @@ func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" testCheckPrivateIP := func() resource.TestCheckFunc { return func(*terraform.State) error { @@ -1325,14 +1423,14 @@ func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, IDRefreshName: resourceName, IDRefreshIgnore: []string{"associate_public_ip_address"}, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP(rName), + Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testCheckPrivateIP(), @@ -1353,6 +1451,7 @@ func TestAccAWSInstance_keyPairCheck(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t1.micro" testCheckKeyPair := func(keyName string) resource.TestCheckFunc { return func(*terraform.State) error { @@ -1368,14 +1467,19 @@ func TestAccAWSInstance_keyPairCheck(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, IDRefreshName: resourceName, IDRefreshIgnore: []string{"source_dest_check"}, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigKeyPair(rName), + Config: testAccInstanceConfigKeyPair(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testCheckKeyPair(rName), @@ -1389,14 +1493,19 @@ func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { + testAccPreCheck(t) + testAccRegionPreCheck(t, "us-west-2") + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigRootBlockDeviceMismatch(rName), + Config: testAccInstanceConfigRootBlockDeviceMismatch(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr( @@ -1426,15 +1535,16 @@ func TestAccAWSInstance_forceNewAndTagsDrift(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.nano" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, IDRefreshName: resourceName, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigForceNewAndTagsDrift(rName), + Config: testAccInstanceConfigForceNewAndTagsDrift(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), driftTags(&v), @@ -1442,7 +1552,7 @@ func TestAccAWSInstance_forceNewAndTagsDrift(t *testing.T) { ExpectNonEmptyPlan: true, }, { - Config: testAccInstanceConfigForceNewAndTagsDrift_Update(rName), + Config: testAccInstanceConfigForceNewAndTagsDrift_Update(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), ), @@ -1460,14 +1570,23 @@ func TestAccAWSInstance_changeInstanceType(t *testing.T) { var before ec2.Instance var after ec2.Instance resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceTypeBefore := "m3.medium" + instanceTypeAfter := "m3.large" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceTypeBefore) + testAccPreCheckOffersEc2InstanceType(t, instanceTypeAfter) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigWithSmallInstanceType, + Config: testAccInstanceConfigWithSmallInstanceType(rName, instanceTypeBefore), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &before), ), @@ -1478,7 +1597,7 @@ func TestAccAWSInstance_changeInstanceType(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfigUpdateInstanceType, + Config: testAccInstanceConfigUpdateInstanceType(rName, instanceTypeAfter), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &after), testAccCheckInstanceNotRecreated( @@ -1495,14 +1614,15 @@ func TestAccAWSInstance_primaryNetworkInterface(t *testing.T) { resourceName := "aws_instance.test" eniResourceName := "aws_network_interface.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigPrimaryNetworkInterface(rName), + Config: testAccInstanceConfigPrimaryNetworkInterface(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &instance), testAccCheckAWSENIExists(eniResourceName, &eni), @@ -1525,14 +1645,15 @@ func TestAccAWSInstance_primaryNetworkInterfaceSourceDestCheck(t *testing.T) { resourceName := "aws_instance.test" eniResourceName := "aws_network_interface.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigPrimaryNetworkInterfaceSourceDestCheck(rName), + Config: testAccInstanceConfigPrimaryNetworkInterfaceSourceDestCheck(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &instance), testAccCheckAWSENIExists(eniResourceName, &eni), @@ -1558,14 +1679,15 @@ func TestAccAWSInstance_addSecondaryInterface(t *testing.T) { eniPrimaryResourceName := "aws_network_interface.primary" eniSecondaryResourceName := "aws_network_interface.secondary" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigAddSecondaryNetworkInterfaceBefore(rName), + Config: testAccInstanceConfigAddSecondaryNetworkInterfaceBefore(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &before), testAccCheckAWSENIExists(eniPrimaryResourceName, &eniPrimary), @@ -1579,7 +1701,7 @@ func TestAccAWSInstance_addSecondaryInterface(t *testing.T) { ImportStateVerifyIgnore: []string{"network_interface"}, }, { - Config: testAccInstanceConfigAddSecondaryNetworkInterfaceAfter(rName), + Config: testAccInstanceConfigAddSecondaryNetworkInterfaceAfter(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &after), testAccCheckAWSENIExists(eniSecondaryResourceName, &eniSecondary), @@ -1596,14 +1718,15 @@ func TestAccAWSInstance_addSecurityGroupNetworkInterface(t *testing.T) { var after ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigAddSecurityGroupBefore(rName), + Config: testAccInstanceConfigAddSecurityGroupBefore(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &before), resource.TestCheckResourceAttr(resourceName, "vpc_security_group_ids.#", "1"), @@ -1615,7 +1738,7 @@ func TestAccAWSInstance_addSecurityGroupNetworkInterface(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfigAddSecurityGroupAfter(rName), + Config: testAccInstanceConfigAddSecurityGroupAfter(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &after), resource.TestCheckResourceAttr(resourceName, "vpc_security_group_ids.#", "2"), @@ -1630,14 +1753,15 @@ func TestAccAWSInstance_associatePublic_defaultPrivate(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_associatePublic_defaultPrivate(rName), + Config: testAccInstanceConfig_associatePublic_defaultPrivate(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "associate_public_ip_address", "false"), @@ -1658,14 +1782,15 @@ func TestAccAWSInstance_associatePublic_defaultPublic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_associatePublic_defaultPublic(rName), + Config: testAccInstanceConfig_associatePublic_defaultPublic(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "associate_public_ip_address", "true"), @@ -1686,14 +1811,15 @@ func TestAccAWSInstance_associatePublic_explicitPublic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_associatePublic_explicitPublic(rName), + Config: testAccInstanceConfig_associatePublic_explicitPublic(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "associate_public_ip_address", "true"), @@ -1714,14 +1840,15 @@ func TestAccAWSInstance_associatePublic_explicitPrivate(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_associatePublic_explicitPrivate(rName), + Config: testAccInstanceConfig_associatePublic_explicitPrivate(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "associate_public_ip_address", "false"), @@ -1742,14 +1869,15 @@ func TestAccAWSInstance_associatePublic_overridePublic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_associatePublic_overridePublic(rName), + Config: testAccInstanceConfig_associatePublic_overridePublic(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "associate_public_ip_address", "true"), @@ -1770,14 +1898,15 @@ func TestAccAWSInstance_associatePublic_overridePrivate(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_associatePublic_overridePrivate(rName), + Config: testAccInstanceConfig_associatePublic_overridePrivate(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "associate_public_ip_address", "false"), @@ -1797,14 +1926,22 @@ func TestAccAWSInstance_getPasswordData_falseToTrue(t *testing.T) { var before, after ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic, + // but t2.medium can't be launched in EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccEC2VPCOnlyPreCheck(t) + testAccPreCheckHasDefaultVpc(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_getPasswordData(rName, false), + Config: testAccInstanceConfig_getPasswordData(rName, false, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &before), resource.TestCheckResourceAttr(resourceName, "get_password_data", "false"), @@ -1817,7 +1954,7 @@ func TestAccAWSInstance_getPasswordData_falseToTrue(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfig_getPasswordData(rName, true), + Config: testAccInstanceConfig_getPasswordData(rName, true, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &after), testAccCheckInstanceNotRecreated(t, &before, &after), @@ -1833,14 +1970,22 @@ func TestAccAWSInstance_getPasswordData_trueToFalse(t *testing.T) { var before, after ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.medium" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic, + // but t2.medium can't be launched in EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccEC2VPCOnlyPreCheck(t) + testAccPreCheckHasDefaultVpc(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_getPasswordData(rName, true), + Config: testAccInstanceConfig_getPasswordData(rName, true, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &before), resource.TestCheckResourceAttr(resourceName, "get_password_data", "true"), @@ -1854,7 +1999,7 @@ func TestAccAWSInstance_getPasswordData_trueToFalse(t *testing.T) { ImportStateVerifyIgnore: []string{"password_data", "get_password_data"}, }, { - Config: testAccInstanceConfig_getPasswordData(rName, false), + Config: testAccInstanceConfig_getPasswordData(rName, false, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &after), testAccCheckInstanceNotRecreated(t, &before, &after), @@ -1870,14 +2015,15 @@ func TestAccAWSInstance_CreditSpecification_Empty_NonBurstable(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m5.large" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_CreditSpecification_Empty_NonBurstable(rName), + Config: testAccInstanceConfig_CreditSpecification_Empty_NonBurstable(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), ), @@ -1897,14 +2043,15 @@ func TestAccAWSInstance_CreditSpecification_UnspecifiedToEmpty_NonBurstable(t *t var instance ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m5.large" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_CreditSpecification_Unspecified_NonBurstable(rName), + Config: testAccInstanceConfig_CreditSpecification_Unspecified_NonBurstable(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &instance), ), @@ -1915,7 +2062,7 @@ func TestAccAWSInstance_CreditSpecification_UnspecifiedToEmpty_NonBurstable(t *t ImportStateVerify: true, }, { - Config: testAccInstanceConfig_CreditSpecification_Empty_NonBurstable(rName), + Config: testAccInstanceConfig_CreditSpecification_Empty_NonBurstable(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &instance), ), @@ -1928,14 +2075,15 @@ func TestAccAWSInstance_creditSpecification_unspecifiedDefaultsToStandard(t *tes var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_unspecified(rName), + Config: testAccInstanceConfig_creditSpecification_unspecified(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -1955,14 +2103,15 @@ func TestAccAWSInstance_creditSpecification_standardCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_standardCpuCredits(rName), + Config: testAccInstanceConfig_creditSpecification_standardCpuCredits(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &first), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -1975,7 +2124,7 @@ func TestAccAWSInstance_creditSpecification_standardCpuCredits(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfig_creditSpecification_unspecified(rName), + Config: testAccInstanceConfig_creditSpecification_unspecified(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &second), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -1990,14 +2139,15 @@ func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits(rName), + Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &first), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2010,7 +2160,7 @@ func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfig_creditSpecification_unspecified(rName), + Config: testAccInstanceConfig_creditSpecification_unspecified(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &second), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2025,14 +2175,15 @@ func TestAccAWSInstance_creditSpecification_unknownCpuCredits_t2(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_unknownCpuCredits(rName, "t2.micro"), + Config: testAccInstanceConfig_creditSpecification_unknownCpuCredits(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2052,14 +2203,15 @@ func TestAccAWSInstance_creditSpecification_unknownCpuCredits_t3(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t3.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_unknownCpuCredits(rName, "t3.micro"), + Config: testAccInstanceConfig_creditSpecification_unknownCpuCredits(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2079,14 +2231,15 @@ func TestAccAWSInstance_creditSpecification_updateCpuCredits(t *testing.T) { var first, second, third ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_standardCpuCredits(rName), + Config: testAccInstanceConfig_creditSpecification_standardCpuCredits(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &first), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2099,7 +2252,7 @@ func TestAccAWSInstance_creditSpecification_updateCpuCredits(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits(rName), + Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &second), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2107,7 +2260,7 @@ func TestAccAWSInstance_creditSpecification_updateCpuCredits(t *testing.T) { ), }, { - Config: testAccInstanceConfig_creditSpecification_standardCpuCredits(rName), + Config: testAccInstanceConfig_creditSpecification_standardCpuCredits(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &third), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2122,14 +2275,15 @@ func TestAccAWSInstance_creditSpecification_isNotAppliedToNonBurstable(t *testin var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_isNotAppliedToNonBurstable(rName), + Config: testAccInstanceConfig_creditSpecification_isNotAppliedToNonBurstable(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), ), @@ -2148,14 +2302,15 @@ func TestAccAWSInstance_creditSpecificationT3_unspecifiedDefaultsToUnlimited(t * var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t3.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_unspecified_t3(rName), + Config: testAccInstanceConfig_creditSpecification_unspecified_t3(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2175,14 +2330,15 @@ func TestAccAWSInstance_creditSpecificationT3_standardCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t3.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_standardCpuCredits_t3(rName), + Config: testAccInstanceConfig_creditSpecification_standardCpuCredits_t3(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &first), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2195,7 +2351,7 @@ func TestAccAWSInstance_creditSpecificationT3_standardCpuCredits(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfig_creditSpecification_unspecified_t3(rName), + Config: testAccInstanceConfig_creditSpecification_unspecified_t3(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &second), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2210,14 +2366,15 @@ func TestAccAWSInstance_creditSpecificationT3_unlimitedCpuCredits(t *testing.T) var first, second ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t3.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits_t3(rName), + Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits_t3(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &first), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2230,7 +2387,7 @@ func TestAccAWSInstance_creditSpecificationT3_unlimitedCpuCredits(t *testing.T) ImportStateVerify: true, }, { - Config: testAccInstanceConfig_creditSpecification_unspecified_t3(rName), + Config: testAccInstanceConfig_creditSpecification_unspecified_t3(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &second), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2245,14 +2402,15 @@ func TestAccAWSInstance_creditSpecificationT3_updateCpuCredits(t *testing.T) { var first, second, third ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t3.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccPreCheckOffersEc2InstanceType(t, instanceType) }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_standardCpuCredits_t3(rName), + Config: testAccInstanceConfig_creditSpecification_standardCpuCredits_t3(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &first), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2265,7 +2423,7 @@ func TestAccAWSInstance_creditSpecificationT3_updateCpuCredits(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits_t3(rName), + Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits_t3(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &second), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2273,7 +2431,7 @@ func TestAccAWSInstance_creditSpecificationT3_updateCpuCredits(t *testing.T) { ), }, { - Config: testAccInstanceConfig_creditSpecification_standardCpuCredits_t3(rName), + Config: testAccInstanceConfig_creditSpecification_standardCpuCredits_t3(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &third), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2288,14 +2446,20 @@ func TestAccAWSInstance_creditSpecification_standardCpuCredits_t2Tot3Taint(t *te var before, after ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceTypeT2 := "t2.micro" + instanceTypeT3 := "t3.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckOffersEc2InstanceType(t, instanceTypeT2) + testAccPreCheckOffersEc2InstanceType(t, instanceTypeT3) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_standardCpuCredits(rName), + Config: testAccInstanceConfig_creditSpecification_standardCpuCredits(rName, instanceTypeT2), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &before), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2308,7 +2472,7 @@ func TestAccAWSInstance_creditSpecification_standardCpuCredits_t2Tot3Taint(t *te ImportStateVerify: true, }, { - Config: testAccInstanceConfig_creditSpecification_standardCpuCredits_t3(rName), + Config: testAccInstanceConfig_creditSpecification_standardCpuCredits_t3(rName, instanceTypeT3), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &after), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2324,14 +2488,20 @@ func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits_t2Tot3Taint(t *t var before, after ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceTypeT2 := "t2.micro" + instanceTypeT3 := "t3.micro" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckOffersEc2InstanceType(t, instanceTypeT2) + testAccPreCheckOffersEc2InstanceType(t, instanceTypeT3) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits(rName), + Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits(rName, instanceTypeT2), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &before), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2344,7 +2514,7 @@ func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits_t2Tot3Taint(t *t ImportStateVerify: true, }, { - Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits_t3(rName), + Config: testAccInstanceConfig_creditSpecification_unlimitedCpuCredits_t3(rName, instanceTypeT3), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &after), resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"), @@ -2359,15 +2529,20 @@ func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits_t2Tot3Taint(t *t func TestAccAWSInstance_disappears(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + // No subnet_id specified requires default VPC or EC2-Classic. + PreCheck: func() { + testAccPreCheck(t) + testAccPreCheckHasDefaultVpcOrEc2Classic(t) + testAccPreCheckOffersEc2InstanceType(t, instanceType) + }, Providers: testAccProviders, CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigBasic(rName), + Config: testAccInstanceConfigBasic(instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), testAccCheckInstanceDisappears(&v), @@ -2382,6 +2557,7 @@ func TestAccAWSInstance_UserData_EmptyStringToUnspecified(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2389,7 +2565,7 @@ func TestAccAWSInstance_UserData_EmptyStringToUnspecified(t *testing.T) { CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_UserData_EmptyString(rName), + Config: testAccInstanceConfig_UserData_EmptyString(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), ), @@ -2402,7 +2578,7 @@ func TestAccAWSInstance_UserData_EmptyStringToUnspecified(t *testing.T) { }, // Switching should show no difference { - Config: testAccInstanceConfig_UserData_Unspecified(rName), + Config: testAccInstanceConfig_UserData_Unspecified(rName, instanceType), ExpectNonEmptyPlan: false, PlanOnly: true, }, @@ -2414,6 +2590,7 @@ func TestAccAWSInstance_UserData_UnspecifiedToEmptyString(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -2421,7 +2598,7 @@ func TestAccAWSInstance_UserData_UnspecifiedToEmptyString(t *testing.T) { CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfig_UserData_Unspecified(rName), + Config: testAccInstanceConfig_UserData_Unspecified(rName, instanceType), Check: resource.ComposeTestCheckFunc( testAccCheckInstanceExists(resourceName, &v), ), @@ -2433,7 +2610,7 @@ func TestAccAWSInstance_UserData_UnspecifiedToEmptyString(t *testing.T) { }, // Switching should show no difference { - Config: testAccInstanceConfig_UserData_EmptyString(rName), + Config: testAccInstanceConfig_UserData_EmptyString(rName, instanceType), ExpectNonEmptyPlan: false, PlanOnly: true, }, @@ -2676,11 +2853,12 @@ func driftTags(instance *ec2.Instance) resource.TestCheckFunc { } } -func testAccInstanceConfigInDefaultVpcBySgName(rName string) string { +func testAccInstanceConfigInDefaultVpcBySgName(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` data "aws_availability_zones" "current" { # Exclude usw2-az4 (us-west-2d) as it has limited instance types. blacklisted_zone_ids = ["usw2-az4"] + state = "available" } data "aws_vpc" "default" { @@ -2699,7 +2877,7 @@ resource "aws_security_group" "test" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q security_groups = ["${aws_security_group.test.name}"] availability_zone = "${data.aws_availability_zones.current.names[0]}" @@ -2707,14 +2885,15 @@ resource "aws_instance" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigInDefaultVpcBySgId(rName string) string { +func testAccInstanceConfigInDefaultVpcBySgId(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` data "aws_availability_zones" "current" { # Exclude usw2-az4 (us-west-2d) as it has limited instance types. blacklisted_zone_ids = ["usw2-az4"] + state = "available" } data "aws_vpc" "default" { @@ -2733,7 +2912,7 @@ resource "aws_security_group" "test" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q vpc_security_group_ids = ["${aws_security_group.test.id}"] availability_zone = "${data.aws_availability_zones.current.names[0]}" @@ -2741,11 +2920,11 @@ resource "aws_instance" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigInEc2Classic(rName string) string { - return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` +func testAccInstanceConfigInEc2Classic(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_security_group" "test" { name = %[1]q description = %[1]q @@ -2756,20 +2935,32 @@ resource "aws_security_group" "test" { } resource "aws_instance" "test" { - ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" - instance_type = "m3.medium" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q security_groups = ["${aws_security_group.test.name}"] tags = { Name = %[1]q } } -`, rName) +`, rName, instanceType) +} + +func testAccInstanceConfigBasic(instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` +resource "aws_instance" "test" { + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[1]q + # Explicitly no tags so as to test creation without tags. +} +`, instanceType) } -func testAccInstanceConfigBasic(rName string) string { - return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` -data "aws_availability_zones" "current" {} +func testAccInstanceConfigAtLeastOneOtherEbsVolume(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` +data "aws_availability_zones" "current" { + state = "available" +} # Ensure that there is at least 1 EBS volume in the current region. # See https://github.com/hashicorp/terraform/issues/1249. @@ -2782,211 +2973,261 @@ resource "aws_ebs_volume" "test" { } } -resource "aws_security_group" "test" { - name = %[1]q - description = %[1]q - - ingress { - protocol = "icmp" - from_port = -1 - to_port = -1 - cidr_blocks = ["0.0.0.0/0"] - } +resource "aws_instance" "test" { + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q + availability_zone = "${data.aws_availability_zones.current.names[0]}" + user_data = "foo:-with-character's" tags = { Name = %[1]q } -} -resource "aws_instance" "test" { - ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - availability_zone = "${data.aws_availability_zones.current.names[0]}" - instance_type = "m1.small" - security_groups = ["${aws_security_group.test.name}"] - user_data = "foo:-with-character's" - # Explicitly no tags to test creation without tags. + depends_on = ["aws_ebs_volume.test"] } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigWithUserDataBase64(rInt int) string { - return fmt.Sprintf(` -resource "aws_security_group" "tf_test_test" { - name = "tf_test_%d" - description = "test" +func testAccInstanceConfigWithUserDataBase64(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` +resource "aws_instance" "test" { + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q + user_data_base64 = "${base64encode("hello world")}" - ingress { - protocol = "icmp" - from_port = -1 - to_port = -1 - cidr_blocks = ["0.0.0.0/0"] + tags = { + Name = %[1]q } } +`, rName, instanceType) +} +func testAccInstanceConfigWithSmallInstanceType(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - # us-west-2 - ami = "ami-4fccb37f" - availability_zone = "us-west-2a" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q - instance_type = "m1.small" - security_groups = ["${aws_security_group.tf_test_test.name}"] - user_data_base64 = "${base64encode("hello world")}" + tags = { + Name = %[1]q + } } -`, rInt) +`, rName, instanceType) } -const testAccInstanceConfigWithSmallInstanceType = ` +func testAccInstanceConfigUpdateInstanceType(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - # us-west-2 - ami = "ami-55a7ea65" - availability_zone = "us-west-2a" - - instance_type = "m3.medium" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q - tags = { - Name = "tf-acctest" - } + tags = { + Name = %[1]q + } +} +`, rName, instanceType) } -` -const testAccInstanceConfigUpdateInstanceType = ` +func testAccInstanceGP2IopsDevice(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - # us-west-2 - ami = "ami-55a7ea65" - availability_zone = "us-west-2a" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q - instance_type = "m3.large" + root_block_device { + volume_type = "gp2" + volume_size = 11 + } - tags = { - Name = "tf-acctest" - } + tags = { + Name = %[1]q + } +} +`, rName, instanceType) } -` -const testAccInstanceGP2IopsDevice = ` +func testAccInstanceGP2WithIopsValue(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - # us-west-2 - ami = "ami-55a7ea65" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q - # In order to attach an encrypted volume to an instance you need to have an - # m3.medium or larger. See "Supported Instance Types" in: - # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html - instance_type = "m3.medium" + root_block_device { + volume_type = "gp2" + volume_size = 11 + # configured explicitly + iops = 10 + } - root_block_device { - volume_type = "gp2" - volume_size = 11 - } + tags = { + Name = %[1]q + } } -` - -const testAccInstanceGP2WithIopsValue = ` -resource "aws_instance" "test" { - # us-west-2 - ami = "ami-55a7ea65" - - # In order to attach an encrypted volume to an instance you need to have an - # m3.medium or larger. See "Supported Instance Types" in: - # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html - instance_type = "m3.medium" - - root_block_device { - volume_type = "gp2" - volume_size = 11 - # configured explicitly - iops = 10 - } +`, rName, instanceType) } -` -const testAccInstanceConfigBlockDevices = ` +func testAccInstanceConfigBlockDevices(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - # us-west-2 - ami = "ami-55a7ea65" - - # In order to attach an encrypted volume to an instance you need to have an - # m3.medium or larger. See "Supported Instance Types" in: - # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html - instance_type = "m3.medium" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q - root_block_device { - volume_type = "gp2" - volume_size = 11 - } + root_block_device { + volume_type = "gp2" + volume_size = 11 + } - ebs_block_device { - device_name = "/dev/sdb" - volume_size = 9 - } + ebs_block_device { + device_name = "/dev/sdb" + volume_size = 9 + } - ebs_block_device { - device_name = "/dev/sdc" - volume_size = 10 - volume_type = "io1" - iops = 100 - } + ebs_block_device { + device_name = "/dev/sdc" + volume_size = 10 + volume_type = "io1" + iops = 100 + } - # Encrypted ebs block device - ebs_block_device { - device_name = "/dev/sdd" - volume_size = 12 - encrypted = true - } + # Encrypted ebs block device + ebs_block_device { + device_name = "/dev/sdd" + volume_size = 12 + encrypted = true + } - ephemeral_block_device { - device_name = "/dev/sde" - virtual_name = "ephemeral0" - } -} -` + ephemeral_block_device { + device_name = "/dev/sde" + virtual_name = "ephemeral0" + } -func testAccInstanceConfigSourceDestEnable(rName string) string { - return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` -resource "aws_instance" "test" { - ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m1.small" - subnet_id = "${aws_subnet.test.id}" + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfigSourceDestDisable(rName string) string { - return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` +func testAccInstanceConfigRootInstanceStore(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmInstanceStoreAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m1.small" - subnet_id = "${aws_subnet.test.id}" - source_dest_check = false + ami = "${data.aws_ami.amzn-ami-minimal-hvm-instance-store.id}" + instance_type = %[2]q + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfigDisableAPITermination(rName string, val bool) string { - return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` +func testAccInstanceConfigNoAMIEphemeralDevices(rName, instanceType string) string { + return fmt.Sprintf(` +# This AMI has 2 ephemeral block devices. +data "aws_ami" "test" { + most_recent = true + owners = ["099720109477"] # Canonical + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-eoan-19.10-amd64-server-*"] + } + + filter { + name = "root-device-type" + values = ["ebs"] + } +} + +resource "aws_instance" "test" { + ami = "${data.aws_ami.test.id}" + instance_type = %[2]q + + root_block_device { + volume_type = "gp2" + volume_size = 11 + } + + ephemeral_block_device { + device_name = "/dev/sdb" + no_device = true + } + + ephemeral_block_device { + device_name = "/dev/sdc" + no_device = true + } + + tags = { + Name = %[1]q + } +} +`, rName, instanceType) +} + +func testAccInstanceConfigSourceDestEnable(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` +resource "aws_instance" "test" { + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q + subnet_id = "${aws_subnet.test.id}" + + tags = { + Name = %[1]q + } +} +`, rName, instanceType) +} + +func testAccInstanceConfigSourceDestDisable(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` +resource "aws_instance" "test" { + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q + subnet_id = "${aws_subnet.test.id}" + source_dest_check = false + + tags = { + Name = %[1]q + } +} +`, rName, instanceType) +} + +func testAccInstanceConfigDisableAPITermination(rName string, val bool, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m1.small" + instance_type = %[3]q subnet_id = "${aws_subnet.test.id}" - disable_api_termination = %[1]t + disable_api_termination = %[2]t + + tags = { + Name = %[1]q + } } -`, val) +`, rName, val, instanceType) } -func testAccInstanceConfigVPC(rName string) string { +func testAccInstanceConfigVPC(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m1.small" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = true tenancy = "dedicated" # pre-encoded base64 data user_data = "3dc39dda39be1205215e776bad998da361a5955d" + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfigPlacementGroup(rName string) string { +func testAccInstanceConfigPlacementGroup(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_placement_group" "test" { name = %[1]q @@ -2996,22 +3237,26 @@ resource "aws_placement_group" "test" { # Limitations: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/placement-groups.html#concepts-placement-groups resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "c3.large" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = true placement_group = "${aws_placement_group.test.name}" # pre-encoded base64 data user_data = "3dc39dda39be1205215e776bad998da361a5955d" + + tags = { + Name = %[1]q + } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigIpv6ErrorConfig(rName string) string { +func testAccInstanceConfigIpv6ErrorConfig(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcIpv6Config(rName) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" ipv6_addresses = ["2600:1f14:bb2:e501::10"] ipv6_address_count = 1 @@ -3020,14 +3265,14 @@ resource "aws_instance" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigIpv6Support(rName string) string { +func testAccInstanceConfigIpv6Support(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcIpv6Config(rName) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" ipv6_address_count = 1 @@ -3035,14 +3280,14 @@ resource "aws_instance" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigIpv6SupportWithIpv4(rName string) string { +func testAccInstanceConfigIpv6SupportWithIpv4(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcIpv6Config(rName) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = true ipv6_address_count = 1 @@ -3051,21 +3296,25 @@ resource "aws_instance" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -const testAccCheckInstanceConfigTags = ` +func testAccCheckInstanceConfigTags(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - ami = "ami-4fccb37f" - instance_type = "m1.small" - tags = { - test = "test2" - } + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q + + tags = { + Name = %[1]q + test = "test2" + } +} +`, rName, instanceType) } -` -func testAccInstanceConfigEbsBlockDeviceKmsKeyArn(rName string) string { - return testAccLatestAmazonLinuxPvEbsAmiConfig() + fmt.Sprintf(` +func testAccInstanceConfigEbsBlockDeviceKmsKeyArn(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_kms_key" "test" { deletion_window_in_days = 7 @@ -3075,12 +3324,8 @@ resource "aws_kms_key" "test" { } resource "aws_instance" "test" { - ami = "${data.aws_ami.amzn-ami-minimal-pv-ebs.id}" - - # In order to attach an encrypted volume to an instance you need to have an - # m3.medium or larger. See "Supported Instance Types" in: - # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html - instance_type = "m3.medium" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q root_block_device { volume_type = "gp2" @@ -3099,35 +3344,22 @@ resource "aws_instance" "test" { Name = %[1]q } } -`, rName) -} - -const testAccInstanceConfigRootBlockDeviceKmsKeyArn = ` -resource "aws_vpc" "test" { - cidr_block = "10.1.0.0/16" - - tags = { - Name = "terraform-testacc-instance-source-dest-enable" - } +`, rName, instanceType) } -resource "aws_subnet" "test" { - cidr_block = "10.1.1.0/24" - vpc_id = "${aws_vpc.test.id}" - availability_zone = "us-west-2a" +func testAccInstanceConfigRootBlockDeviceKmsKeyArn(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` +resource "aws_kms_key" "test" { + deletion_window_in_days = 7 tags = { - Name = "tf-acc-instance-source-dest-enable" + Name = %[1]q } } -resource "aws_kms_key" "test" { - deletion_window_in_days = 7 -} - resource "aws_instance" "test" { - ami = "ami-08692d171e3cf02d6" - instance_type = "t3.nano" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" root_block_device { @@ -3135,39 +3367,19 @@ resource "aws_instance" "test" { encrypted = true kms_key_id = "${aws_kms_key.test.arn}" } -} -` - -const testAccCheckInstanceConfigWithAttachedVolume = ` -data "aws_ami" "debian_jessie_latest" { - most_recent = true - - filter { - name = "name" - values = ["debian-jessie-*"] - } - - filter { - name = "virtualization-type" - values = ["hvm"] - } - filter { - name = "architecture" - values = ["x86_64"] - } - - filter { - name = "root-device-type" - values = ["ebs"] + tags = { + Name = %[1]q } - - owners = ["379101102735"] # Debian +} +`, rName, instanceType) } +func testAccCheckInstanceConfigWithAttachedVolume(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - ami = "${data.aws_ami.debian_jessie_latest.id}" - instance_type = "t2.medium" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q root_block_device { delete_on_termination = true @@ -3176,7 +3388,7 @@ resource "aws_instance" "test" { } tags = { - Name = "test-terraform" + Name = %[1]q } } @@ -3186,7 +3398,7 @@ resource "aws_ebs_volume" "test" { type = "gp2" tags = { - Name = "test-terraform" + Name = %[1]q } } @@ -3195,130 +3407,156 @@ resource "aws_volume_attachment" "test" { volume_id = "${aws_ebs_volume.test.id}" instance_id = "${aws_instance.test.id}" } -` +`, rName, instanceType) +} -const testAccCheckInstanceConfigNoVolumeTags = ` +func testAccCheckInstanceConfigNoVolumeTags(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - ami = "ami-55a7ea65" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q - instance_type = "m3.medium" + root_block_device { + volume_type = "gp2" + volume_size = 11 + } - root_block_device { - volume_type = "gp2" - volume_size = 11 - } - ebs_block_device { - device_name = "/dev/sdb" - volume_size = 9 - } - ebs_block_device { - device_name = "/dev/sdc" - volume_size = 10 - volume_type = "io1" - iops = 100 - } + ebs_block_device { + device_name = "/dev/sdb" + volume_size = 9 + } - ebs_block_device { - device_name = "/dev/sdd" - volume_size = 12 - encrypted = true - } + ebs_block_device { + device_name = "/dev/sdc" + volume_size = 10 + volume_type = "io1" + iops = 100 + } - ephemeral_block_device { - device_name = "/dev/sde" - virtual_name = "ephemeral0" - } + ebs_block_device { + device_name = "/dev/sdd" + volume_size = 12 + encrypted = true + } + + ephemeral_block_device { + device_name = "/dev/sde" + virtual_name = "ephemeral0" + } + + tags = { + Name = %[1]q + } +} +`, rName, instanceType) } -` -const testAccCheckInstanceConfigWithVolumeTags = ` +func testAccCheckInstanceConfigWithVolumeTags(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - ami = "ami-55a7ea65" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q - instance_type = "m3.medium" + root_block_device { + volume_type = "gp2" + volume_size = 11 + } - root_block_device { - volume_type = "gp2" - volume_size = 11 - } - ebs_block_device { - device_name = "/dev/sdb" - volume_size = 9 - } - ebs_block_device { - device_name = "/dev/sdc" - volume_size = 10 - volume_type = "io1" - iops = 100 - } + ebs_block_device { + device_name = "/dev/sdb" + volume_size = 9 + } - ebs_block_device { - device_name = "/dev/sdd" - volume_size = 12 - encrypted = true - } + ebs_block_device { + device_name = "/dev/sdc" + volume_size = 10 + volume_type = "io1" + iops = 100 + } - ephemeral_block_device { - device_name = "/dev/sde" - virtual_name = "ephemeral0" - } + ebs_block_device { + device_name = "/dev/sdd" + volume_size = 12 + encrypted = true + } - volume_tags = { - Name = "acceptance-test-volume-tag" - } + ephemeral_block_device { + device_name = "/dev/sde" + virtual_name = "ephemeral0" + } + + volume_tags = { + Name = %[1]q + } + + tags = { + Name = %[1]q + } +} +`, rName, instanceType) } -` -const testAccCheckInstanceConfigWithVolumeTagsUpdate = ` +func testAccCheckInstanceConfigWithVolumeTagsUpdate(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - ami = "ami-55a7ea65" + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q - instance_type = "m3.medium" + root_block_device { + volume_type = "gp2" + volume_size = 11 + } - root_block_device { - volume_type = "gp2" - volume_size = 11 - } - ebs_block_device { - device_name = "/dev/sdb" - volume_size = 9 - } - ebs_block_device { - device_name = "/dev/sdc" - volume_size = 10 - volume_type = "io1" - iops = 100 - } + ebs_block_device { + device_name = "/dev/sdb" + volume_size = 9 + } - ebs_block_device { - device_name = "/dev/sdd" - volume_size = 12 - encrypted = true - } + ebs_block_device { + device_name = "/dev/sdc" + volume_size = 10 + volume_type = "io1" + iops = 100 + } - ephemeral_block_device { - device_name = "/dev/sde" - virtual_name = "ephemeral0" - } + ebs_block_device { + device_name = "/dev/sdd" + volume_size = 12 + encrypted = true + } - volume_tags = { - Name = "acceptance-test-volume-tag" - Environment = "dev" - } + ephemeral_block_device { + device_name = "/dev/sde" + virtual_name = "ephemeral0" + } + + volume_tags = { + Name = %[1]q + Environment = "dev" + } + + tags = { + Name = %[1]q + } +} +`, rName, instanceType) } -` -const testAccCheckInstanceConfigTagsUpdate = ` +func testAccCheckInstanceConfigTagsUpdate(rName, instanceType string) string { + return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_instance" "test" { - ami = "ami-4fccb37f" - instance_type = "m1.small" - tags = { - test2 = "test3" - } + ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" + instance_type = %[2]q + + tags = { + Name = %[1]q + test2 = "test3" + } +} +`, rName, instanceType) } -` -func testAccInstanceConfigWithoutInstanceProfile(rName string) string { +func testAccInstanceConfigWithoutInstanceProfile(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_iam_role" "test" { name = %[1]q @@ -3327,16 +3565,16 @@ resource "aws_iam_role" "test" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m1.small" + instance_type = %[2]q tags = { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigWithInstanceProfile(rName string) string { +func testAccInstanceConfigWithInstanceProfile(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_iam_role" "test" { name = %[1]q @@ -3350,104 +3588,142 @@ resource "aws_iam_instance_profile" "test" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m1.small" + instance_type = %[2]q iam_instance_profile = "${aws_iam_instance_profile.test.name}" tags = { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigPrivateIP(rName string) string { +func testAccInstanceConfigPrivateIP(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" private_ip = "10.1.1.42" + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfigAssociatePublicIPAndPrivateIP(rName string) string { +func testAccInstanceConfigAssociatePublicIPAndPrivateIP(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = true private_ip = "10.1.1.42" + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceNetworkInstanceSecurityGroups(rName string) string { +func testAccInstanceNetworkInstanceSecurityGroups(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + testAccAwsInstanceVpcSecurityGroupConfig(rName) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t1.micro" + instance_type = %[2]q vpc_security_group_ids = ["${aws_security_group.test.id}"] subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = true - depends_on = ["aws_internet_gateway.test"] + + tags = { + Name = %[1]q + } + + depends_on = ["aws_internet_gateway.test"] } resource "aws_eip" "test" { - instance = "${aws_instance.test.id}" - vpc = true + instance = "${aws_instance.test.id}" + vpc = true + + tags = { + Name = %[1]q + } + depends_on = ["aws_internet_gateway.test"] } -`) +`, rName, instanceType) } -func testAccInstanceNetworkInstanceVPCSecurityGroupIDs(rName string) string { +func testAccInstanceNetworkInstanceVPCSecurityGroupIDs(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + testAccAwsInstanceVpcSecurityGroupConfig(rName) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t1.micro" + instance_type = %[2]q vpc_security_group_ids = ["${aws_security_group.test.id}"] subnet_id = "${aws_subnet.test.id}" - depends_on = ["aws_internet_gateway.test"] + + tags = { + Name = %[1]q + } + + depends_on = ["aws_internet_gateway.test"] } resource "aws_eip" "test" { - instance = "${aws_instance.test.id}" - vpc = true + instance = "${aws_instance.test.id}" + vpc = true + + tags = { + Name = %[1]q + } + depends_on = ["aws_internet_gateway.test"] } -`) +`, rName, instanceType) } -func testAccInstanceNetworkInstanceVPCRemoveSecurityGroupIDs(rName string) string { +func testAccInstanceNetworkInstanceVPCRemoveSecurityGroupIDs(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + testAccAwsInstanceVpcSecurityGroupConfig(rName) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t1.micro" + instance_type = %[2]q vpc_security_group_ids = [] subnet_id = "${aws_subnet.test.id}" - depends_on = ["aws_internet_gateway.test"] + + tags = { + Name = %[1]q + } + + depends_on = ["aws_internet_gateway.test"] } resource "aws_eip" "test" { - instance = "${aws_instance.test.id}" - vpc = true + instance = "${aws_instance.test.id}" + vpc = true + + tags = { + Name = %[1]q + } + depends_on = ["aws_internet_gateway.test"] } -`) +`, rName, instanceType) } -func testAccInstanceConfigKeyPair(rName string) string { +func testAccInstanceConfigKeyPair(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + fmt.Sprintf(` resource "aws_key_pair" "test" { key_name = %[1]q @@ -3456,52 +3732,64 @@ resource "aws_key_pair" "test" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t1.micro" + instance_type = %[2]q key_name = "${aws_key_pair.test.key_name}" tags = { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigRootBlockDeviceMismatch(rName string) string { +func testAccInstanceConfigRootBlockDeviceMismatch(rName, instanceType string) string { return testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { - // This is an AMI with RootDeviceName: "/dev/sda1"; actual root: "/dev/sda" + // This is an AMI in us-west-2 with RootDeviceName: "/dev/sda1"; actual root: "/dev/sda" ami = "ami-ef5b69df" - instance_type = "t1.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" root_block_device { volume_size = 13 } + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfigForceNewAndTagsDrift(rName string) string { +func testAccInstanceConfigForceNewAndTagsDrift(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.nano" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfigForceNewAndTagsDrift_Update(rName string) string { +func testAccInstanceConfigForceNewAndTagsDrift_Update(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfigPrimaryNetworkInterface(rName string) string { +func testAccInstanceConfigPrimaryNetworkInterface(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_network_interface" "test" { subnet_id = "${aws_subnet.test.id}" @@ -3514,17 +3802,21 @@ resource "aws_network_interface" "test" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q network_interface { network_interface_id = "${aws_network_interface.test.id}" device_index = 0 } + + tags = { + Name = %[1]q + } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigPrimaryNetworkInterfaceSourceDestCheck(rName string) string { +func testAccInstanceConfigPrimaryNetworkInterfaceSourceDestCheck(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_network_interface" "test" { subnet_id = "${aws_subnet.test.id}" @@ -3538,17 +3830,21 @@ resource "aws_network_interface" "test" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q network_interface { network_interface_id = "${aws_network_interface.test.id}" device_index = 0 } + + tags = { + Name = %[1]q + } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigAddSecondaryNetworkInterfaceBefore(rName string) string { +func testAccInstanceConfigAddSecondaryNetworkInterfaceBefore(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_network_interface" "primary" { subnet_id = "${aws_subnet.test.id}" @@ -3570,17 +3866,21 @@ resource "aws_network_interface" "secondary" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q network_interface { network_interface_id = "${aws_network_interface.primary.id}" device_index = 0 } + + tags = { + Name = %[1]q + } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigAddSecondaryNetworkInterfaceAfter(rName string) string { +func testAccInstanceConfigAddSecondaryNetworkInterfaceAfter(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_network_interface" "primary" { subnet_id = "${aws_subnet.test.id}" @@ -3608,17 +3908,21 @@ resource "aws_network_interface" "secondary" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q network_interface { network_interface_id = "${aws_network_interface.primary.id}" device_index = 0 } + + tags = { + Name = %[1]q + } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigAddSecurityGroupBefore(rName string) string { +func testAccInstanceConfigAddSecurityGroupBefore(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_subnet" "test2" { cidr_block = "10.1.2.0/24" @@ -3644,7 +3948,7 @@ resource "aws_security_group" "test2" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = false @@ -3672,10 +3976,10 @@ resource "aws_network_interface" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfigAddSecurityGroupAfter(rName string) string { +func testAccInstanceConfigAddSecurityGroupAfter(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_subnet" "test2" { cidr_block = "10.1.2.0/24" @@ -3701,7 +4005,7 @@ resource "aws_security_group" "test2" { resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = false @@ -3730,42 +4034,42 @@ resource "aws_network_interface" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfig_associatePublic_defaultPrivate(rName string) string { +func testAccInstanceConfig_associatePublic_defaultPrivate(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" tags = { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfig_associatePublic_defaultPublic(rName string) string { +func testAccInstanceConfig_associatePublic_defaultPublic(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, true) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" tags = { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfig_associatePublic_explicitPublic(rName string) string { +func testAccInstanceConfig_associatePublic_explicitPublic(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, true) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = true @@ -3773,14 +4077,14 @@ resource "aws_instance" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfig_associatePublic_explicitPrivate(rName string) string { +func testAccInstanceConfig_associatePublic_explicitPrivate(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, true) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = false @@ -3788,14 +4092,14 @@ resource "aws_instance" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfig_associatePublic_overridePublic(rName string) string { +func testAccInstanceConfig_associatePublic_overridePublic(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = true @@ -3803,14 +4107,14 @@ resource "aws_instance" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfig_associatePublic_overridePrivate(rName string) string { +func testAccInstanceConfig_associatePublic_overridePrivate(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, true) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" associate_public_ip_address = false @@ -3818,10 +4122,10 @@ resource "aws_instance" "test" { Name = %[1]q } } -`, rName) +`, rName, instanceType) } -func testAccInstanceConfig_getPasswordData(rName string, val bool) string { +func testAccInstanceConfig_getPasswordData(rName string, val bool, instanceType string) string { return testAccLatestWindowsServer2016CoreAmiConfig() + fmt.Sprintf(` resource "aws_key_pair" "test" { key_name = %[1]q @@ -3830,158 +4134,209 @@ resource "aws_key_pair" "test" { resource "aws_instance" "test" { ami = "${data.aws_ami.win2016core-ami.id}" - instance_type = "t2.medium" + instance_type = %[3]q key_name = "${aws_key_pair.test.key_name}" get_password_data = %[2]t + + tags = { + Name = %[1]q + } } -`, rName, val) +`, rName, val, instanceType) } -func testAccInstanceConfig_CreditSpecification_Empty_NonBurstable(rName string) string { +func testAccInstanceConfig_CreditSpecification_Empty_NonBurstable(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m5.large" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" credit_specification {} + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfig_CreditSpecification_Unspecified_NonBurstable(rName string) string { +func testAccInstanceConfig_CreditSpecification_Unspecified_NonBurstable(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m5.large" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfig_creditSpecification_unspecified(rName string) string { +func testAccInstanceConfig_creditSpecification_unspecified(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfig_creditSpecification_unspecified_t3(rName string) string { +func testAccInstanceConfig_creditSpecification_unspecified_t3(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t3.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfig_creditSpecification_standardCpuCredits(rName string) string { +func testAccInstanceConfig_creditSpecification_standardCpuCredits(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" credit_specification { cpu_credits = "standard" } + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfig_creditSpecification_standardCpuCredits_t3(rName string) string { +func testAccInstanceConfig_creditSpecification_standardCpuCredits_t3(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t3.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" credit_specification { cpu_credits = "standard" } + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfig_creditSpecification_unlimitedCpuCredits(rName string) string { +func testAccInstanceConfig_creditSpecification_unlimitedCpuCredits(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" credit_specification { cpu_credits = "unlimited" } + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfig_creditSpecification_unlimitedCpuCredits_t3(rName string) string { +func testAccInstanceConfig_creditSpecification_unlimitedCpuCredits_t3(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t3.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" credit_specification { cpu_credits = "unlimited" } + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfig_creditSpecification_isNotAppliedToNonBurstable(rName string) string { +func testAccInstanceConfig_creditSpecification_isNotAppliedToNonBurstable(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "m1.small" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" credit_specification { cpu_credits = "standard" } + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } func testAccInstanceConfig_creditSpecification_unknownCpuCredits(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = %[1]q + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" credit_specification {} + + tags = { + Name = %[1]q + } } -`, instanceType) +`, rName, instanceType) } -func testAccInstanceConfig_UserData_Unspecified(rName string) string { +func testAccInstanceConfig_UserData_Unspecified(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } -func testAccInstanceConfig_UserData_EmptyString(rName string) string { +func testAccInstanceConfig_UserData_EmptyString(rName, instanceType string) string { return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(` resource "aws_instance" "test" { ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}" - instance_type = "t2.micro" + instance_type = %[2]q subnet_id = "${aws_subnet.test.id}" user_data = "" + + tags = { + Name = %[1]q + } } -`) +`, rName, instanceType) } // testAccLatestAmazonLinuxHvmEbsAmiConfig returns the configuration for a data source that @@ -4028,28 +4383,6 @@ data "aws_ami" "amzn-ami-minimal-hvm-instance-store" { `) } -// testAccLatestAmazonLinuxPvEbsAmiConfig returns the configuration for a data source that -// describes the latest Amazon Linux AMI using PV virtualization and an EBS root device. -// The data source is named 'amzn-ami-minimal-pv-ebs'. -func testAccLatestAmazonLinuxPvEbsAmiConfig() string { - return fmt.Sprintf(` -data "aws_ami" "amzn-ami-minimal-pv-ebs" { - most_recent = true - owners = ["amazon"] - - filter { - name = "name" - values = ["amzn-ami-minimal-pv-*"] - } - - filter { - name = "root-device-type" - values = ["ebs"] - } -} -`) -} - // testAccLatestWindowsServer2016CoreAmiConfig returns the configuration for a data source that // describes the latest Microsoft Windows Server 2016 Core AMI. // The data source is named 'win2016core-ami'. @@ -4076,6 +4409,7 @@ func testAccAwsInstanceVpcConfig(rName string, mapPublicIpOnLaunch bool) string data "aws_availability_zones" "current" { # Exclude usw2-az4 (us-west-2d) as it has limited instance types. blacklisted_zone_ids = ["usw2-az4"] + state = "available" } resource "aws_vpc" "test" { @@ -4141,6 +4475,7 @@ func testAccAwsInstanceVpcIpv6Config(rName string) string { data "aws_availability_zones" "current" { # Exclude usw2-az4 (us-west-2d) as it has limited instance types. blacklisted_zone_ids = ["usw2-az4"] + state = "available" } resource "aws_vpc" "test" { From b41e6de295dd02751208ae6406f5db958810ad70 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 23 Dec 2019 14:00:51 -0500 Subject: [PATCH 16/17] Move some testAccPreCheck functions to 'provider_test.go'. --- aws/provider_test.go | 39 +++++++++++++++++++++++++++++++ aws/resource_aws_instance_test.go | 37 ----------------------------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/aws/provider_test.go b/aws/provider_test.go index c544b4089e8..404c43b32d6 100644 --- a/aws/provider_test.go +++ b/aws/provider_test.go @@ -9,8 +9,10 @@ import ( "strings" "testing" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/aws/endpoints" + "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/organizations" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -428,6 +430,43 @@ func testAccOrganizationsAccountPreCheck(t *testing.T) { t.Skip("skipping tests; this AWS account must not be an existing member of an AWS Organization") } +// testAccPreCheckHasDefaultVpc checks that the test region has a default VPC. +func testAccPreCheckHasDefaultVpc(t *testing.T) { + if !testAccHasDefaultVpc(t) { + region := testAccProvider.Meta().(*AWSClient).region + t.Skipf("skipping tests; %s does not have a default VPC", region) + } +} + +// testAccPreCheckHasDefaultVpcOrEc2Classic checks that the test region has a default VPC or has the EC2-Classic platform. +// This check is useful to ensure that an instance can be launched without specifying a subnet. +func testAccPreCheckHasDefaultVpcOrEc2Classic(t *testing.T) { + client := testAccProvider.Meta().(*AWSClient) + + if !testAccHasDefaultVpc(t) && !hasEc2Classic(client.supportedplatforms) { + t.Skipf("skipping tests; %s does not have a default VPC or EC2-Classic", client.region) + } +} + +func testAccHasDefaultVpc(t *testing.T) bool { + conn := testAccProvider.Meta().(*AWSClient).ec2conn + + resp, err := conn.DescribeAccountAttributes(&ec2.DescribeAccountAttributesInput{ + AttributeNames: aws.StringSlice([]string{ec2.AccountAttributeNameDefaultVpc}), + }) + if testAccPreCheckSkipError(err) || + len(resp.AccountAttributes) == 0 || + len(resp.AccountAttributes[0].AttributeValues) == 0 || + aws.StringValue(resp.AccountAttributes[0].AttributeValues[0].AttributeValue) == "none" { + return false + } + if err != nil { + t.Fatalf("error describing EC2 account attributes: %s", err) + } + + return true +} + func testAccAlternateAccountProviderConfig() string { return fmt.Sprintf(` provider "aws" { diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index d992c3d4a7c..072a0b5eda8 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -4567,40 +4567,3 @@ func testAccPreCheckOffersEc2InstanceType(t *testing.T, instanceType string) { t.Fatalf("error describing EC2 instance type offerings: %s", err) } } - -// testAccPreCheckHasDefaultVpc checks that the test region has a default VPC. -func testAccPreCheckHasDefaultVpc(t *testing.T) { - if !testAccHasDefaultVpc(t) { - region := testAccProvider.Meta().(*AWSClient).region - t.Skipf("skipping tests; %s does not have a default VPC", region) - } -} - -// testAccPreCheckHasDefaultVpcOrEc2Classic checks that the test region has a default VPC or has the EC2-Classic platform. -// This check is useful to ensure that an instance can be launched without specifying a subnet. -func testAccPreCheckHasDefaultVpcOrEc2Classic(t *testing.T) { - client := testAccProvider.Meta().(*AWSClient) - - if !testAccHasDefaultVpc(t) && !hasEc2Classic(client.supportedplatforms) { - t.Skipf("skipping tests; %s does not have a default VPC or EC2-Classic", client.region) - } -} - -func testAccHasDefaultVpc(t *testing.T) bool { - conn := testAccProvider.Meta().(*AWSClient).ec2conn - - resp, err := conn.DescribeAccountAttributes(&ec2.DescribeAccountAttributesInput{ - AttributeNames: aws.StringSlice([]string{ec2.AccountAttributeNameDefaultVpc}), - }) - if testAccPreCheckSkipError(err) || - len(resp.AccountAttributes) == 0 || - len(resp.AccountAttributes[0].AttributeValues) == 0 || - aws.StringValue(resp.AccountAttributes[0].AttributeValues[0].AttributeValue) == "none" { - return false - } - if err != nil { - t.Fatalf("error describing EC2 account attributes: %s", err) - } - - return true -} From 1f4dd7994b4dbddeae4ed3b418daf8ffe4321a7d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 6 Mar 2020 17:13:15 -0500 Subject: [PATCH 17/17] Use 'acctest.RandomWithPrefix(tf-acc-test)' for resource names. --- aws/data_source_aws_instance_test.go | 38 ++++---- aws/data_source_aws_instances_test.go | 6 +- aws/resource_aws_instance_test.go | 126 +++++++++++++------------- 3 files changed, 85 insertions(+), 85 deletions(-) diff --git a/aws/data_source_aws_instance_test.go b/aws/data_source_aws_instance_test.go index 2046982aeeb..c04aa6274ef 100644 --- a/aws/data_source_aws_instance_test.go +++ b/aws/data_source_aws_instance_test.go @@ -12,7 +12,7 @@ import ( func TestAccAWSInstanceDataSource_basic(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ @@ -41,7 +41,7 @@ func TestAccAWSInstanceDataSource_basic(t *testing.T) { func TestAccAWSInstanceDataSource_tags(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") rInt := acctest.RandInt() instanceType := "m1.small" @@ -69,7 +69,7 @@ func TestAccAWSInstanceDataSource_tags(t *testing.T) { func TestAccAWSInstanceDataSource_AzUserData(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ @@ -98,7 +98,7 @@ func TestAccAWSInstanceDataSource_AzUserData(t *testing.T) { func TestAccAWSInstanceDataSource_gp2IopsDevice(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ @@ -128,7 +128,7 @@ func TestAccAWSInstanceDataSource_gp2IopsDevice(t *testing.T) { func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ @@ -159,7 +159,7 @@ func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) { // Test to verify that ebs_block_device kms_key_id does not elicit a panic func TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId(t *testing.T) { - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. instanceType := "m3.medium" @@ -181,7 +181,7 @@ func TestAccAWSInstanceDataSource_EbsBlockDevice_KmsKeyId(t *testing.T) { // Test to verify that root_block_device kms_key_id does not elicit a panic func TestAccAWSInstanceDataSource_RootBlockDevice_KmsKeyId(t *testing.T) { - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. instanceType := "m3.medium" @@ -204,7 +204,7 @@ func TestAccAWSInstanceDataSource_RootBlockDevice_KmsKeyId(t *testing.T) { func TestAccAWSInstanceDataSource_rootInstanceStore(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ @@ -233,7 +233,7 @@ func TestAccAWSInstanceDataSource_rootInstanceStore(t *testing.T) { func TestAccAWSInstanceDataSource_privateIP(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -255,7 +255,7 @@ func TestAccAWSInstanceDataSource_privateIP(t *testing.T) { func TestAccAWSInstanceDataSource_keyPair(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ @@ -283,7 +283,7 @@ func TestAccAWSInstanceDataSource_keyPair(t *testing.T) { func TestAccAWSInstanceDataSource_VPC(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ @@ -307,7 +307,7 @@ func TestAccAWSInstanceDataSource_VPC(t *testing.T) { func TestAccAWSInstanceDataSource_PlacementGroup(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "c3.large" resource.ParallelTest(t, resource.TestCase{ @@ -327,7 +327,7 @@ func TestAccAWSInstanceDataSource_PlacementGroup(t *testing.T) { func TestAccAWSInstanceDataSource_Ec2ClassicSecurityGroups(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ @@ -355,7 +355,7 @@ func TestAccAWSInstanceDataSource_Ec2ClassicSecurityGroups(t *testing.T) { func TestAccAWSInstanceDataSource_VPCSecurityGroups(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ @@ -377,7 +377,7 @@ func TestAccAWSInstanceDataSource_VPCSecurityGroups(t *testing.T) { func TestAccAWSInstanceDataSource_getPasswordData_trueToFalse(t *testing.T) { datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.medium" resource.ParallelTest(t, resource.TestCase{ @@ -411,7 +411,7 @@ func TestAccAWSInstanceDataSource_getPasswordData_trueToFalse(t *testing.T) { func TestAccAWSInstanceDataSource_getPasswordData_falseToTrue(t *testing.T) { datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.medium" resource.ParallelTest(t, resource.TestCase{ @@ -445,7 +445,7 @@ func TestAccAWSInstanceDataSource_getPasswordData_falseToTrue(t *testing.T) { func TestAccAWSInstanceDataSource_GetUserData(t *testing.T) { datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -480,7 +480,7 @@ func TestAccAWSInstanceDataSource_GetUserData(t *testing.T) { func TestAccAWSInstanceDataSource_GetUserData_NoUserData(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -518,7 +518,7 @@ func TestAccAWSInstanceDataSource_GetUserData_NoUserData(t *testing.T) { func TestAccAWSInstanceDataSource_creditSpecification(t *testing.T) { resourceName := "aws_instance.test" datasourceName := "data.aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ diff --git a/aws/data_source_aws_instances_test.go b/aws/data_source_aws_instances_test.go index c86e8d74396..016a9525801 100644 --- a/aws/data_source_aws_instances_test.go +++ b/aws/data_source_aws_instances_test.go @@ -10,7 +10,7 @@ import ( func TestAccAWSInstancesDataSource_basic(t *testing.T) { datasourceName := "data.aws_instances.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -39,7 +39,7 @@ func TestAccAWSInstancesDataSource_basic(t *testing.T) { func TestAccAWSInstancesDataSource_tags(t *testing.T) { datasourceName := "data.aws_instances.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -63,7 +63,7 @@ func TestAccAWSInstancesDataSource_tags(t *testing.T) { func TestAccAWSInstancesDataSource_instance_state_names(t *testing.T) { datasourceName := "data.aws_instances.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 072a0b5eda8..114197e4d60 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -148,7 +148,7 @@ func TestFetchRootDevice(t *testing.T) { func TestAccAWSInstance_inDefaultVpcBySgName(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -179,7 +179,7 @@ func TestAccAWSInstance_inDefaultVpcBySgName(t *testing.T) { func TestAccAWSInstance_inDefaultVpcBySgId(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -210,7 +210,7 @@ func TestAccAWSInstance_inDefaultVpcBySgId(t *testing.T) { func TestAccAWSInstance_inEc2Classic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ @@ -283,7 +283,7 @@ func TestAccAWSInstance_basic(t *testing.T) { func TestAccAWSInstance_atLeastOneOtherEbsVolume(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ @@ -330,7 +330,7 @@ func TestAccAWSInstance_EbsBlockDevice_KmsKeyArn(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" kmsKeyResourceName := "aws_kms_key.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. instanceType := "m3.medium" @@ -361,7 +361,7 @@ func TestAccAWSInstance_RootBlockDevice_KmsKeyArn(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" kmsKeyResourceName := "aws_kms_key.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. instanceType := "t3.nano" @@ -391,7 +391,7 @@ func TestAccAWSInstance_RootBlockDevice_KmsKeyArn(t *testing.T) { func TestAccAWSInstance_userDataBase64(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ @@ -429,7 +429,7 @@ func TestAccAWSInstance_userDataBase64(t *testing.T) { func TestAccAWSInstance_GP2IopsDevice(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m3.medium" testCheck := func() resource.TestCheckFunc { @@ -491,7 +491,7 @@ func TestAccAWSInstance_GP2IopsDevice(t *testing.T) { func TestAccAWSInstance_GP2WithIopsValue(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m3.medium" resource.ParallelTest(t, resource.TestCase{ @@ -527,7 +527,7 @@ func TestAccAWSInstance_GP2WithIopsValue(t *testing.T) { func TestAccAWSInstance_blockDevices(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. instanceType := "m3.medium" @@ -639,7 +639,7 @@ func TestAccAWSInstance_blockDevices(t *testing.T) { func TestAccAWSInstance_rootInstanceStore(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html#instance-store-volumes. instanceType := "m3.medium" @@ -681,7 +681,7 @@ func TestAccAWSInstance_rootInstanceStore(t *testing.T) { func TestAccAWSInstance_noAMIEphemeralDevices(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "c3.large" testCheck := func() resource.TestCheckFunc { @@ -766,7 +766,7 @@ func TestAccAWSInstance_noAMIEphemeralDevices(t *testing.T) { func TestAccAWSInstance_sourceDestCheck(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" testCheck := func(enabled bool) resource.TestCheckFunc { @@ -821,7 +821,7 @@ func TestAccAWSInstance_sourceDestCheck(t *testing.T) { func TestAccAWSInstance_disableApiTermination(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" checkDisableApiTermination := func(expected bool) resource.TestCheckFunc { @@ -874,7 +874,7 @@ func TestAccAWSInstance_disableApiTermination(t *testing.T) { func TestAccAWSInstance_vpc(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ @@ -908,7 +908,7 @@ func TestAccAWSInstance_vpc(t *testing.T) { func TestAccAWSInstance_placementGroup(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "c3.large" resource.ParallelTest(t, resource.TestCase{ @@ -941,7 +941,7 @@ func TestAccAWSInstance_placementGroup(t *testing.T) { func TestAccAWSInstance_ipv6_supportAddressCount(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -970,7 +970,7 @@ func TestAccAWSInstance_ipv6_supportAddressCount(t *testing.T) { } func TestAccAWSInstance_ipv6AddressCountAndSingleAddressCausesError(t *testing.T) { - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -989,7 +989,7 @@ func TestAccAWSInstance_ipv6AddressCountAndSingleAddressCausesError(t *testing.T func TestAccAWSInstance_ipv6_supportAddressCountWithIpv4(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1020,7 +1020,7 @@ func TestAccAWSInstance_ipv6_supportAddressCountWithIpv4(t *testing.T) { func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1049,7 +1049,7 @@ func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { func TestAccAWSInstance_NetworkInstanceRemovingAllSecurityGroups(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1093,7 +1093,7 @@ func TestAccAWSInstance_NetworkInstanceRemovingAllSecurityGroups(t *testing.T) { func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1125,7 +1125,7 @@ func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) { func TestAccAWSInstance_tags(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ @@ -1167,7 +1167,7 @@ func TestAccAWSInstance_tags(t *testing.T) { func TestAccAWSInstance_volumeTags(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") // See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances. instanceType := "m3.medium" @@ -1232,7 +1232,7 @@ func TestAccAWSInstance_volumeTags(t *testing.T) { func TestAccAWSInstance_volumeTagsComputed(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.medium" resource.ParallelTest(t, resource.TestCase{ @@ -1265,8 +1265,8 @@ func TestAccAWSInstance_volumeTagsComputed(t *testing.T) { func TestAccAWSInstance_instanceProfileChange(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) - rName2 := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") + rName2 := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" testCheckInstanceProfile := func() resource.TestCheckFunc { @@ -1328,7 +1328,7 @@ func TestAccAWSInstance_instanceProfileChange(t *testing.T) { func TestAccAWSInstance_withIamInstanceProfile(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" testCheckInstanceProfile := func() resource.TestCheckFunc { @@ -1371,7 +1371,7 @@ func TestAccAWSInstance_withIamInstanceProfile(t *testing.T) { func TestAccAWSInstance_privateIP(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" testCheckPrivateIP := func() resource.TestCheckFunc { @@ -1409,7 +1409,7 @@ func TestAccAWSInstance_privateIP(t *testing.T) { func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" testCheckPrivateIP := func() resource.TestCheckFunc { @@ -1450,7 +1450,7 @@ func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { func TestAccAWSInstance_keyPairCheck(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t1.micro" testCheckKeyPair := func(keyName string) resource.TestCheckFunc { @@ -1492,7 +1492,7 @@ func TestAccAWSInstance_keyPairCheck(t *testing.T) { func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t1.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1534,7 +1534,7 @@ func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) { func TestAccAWSInstance_forceNewAndTagsDrift(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.nano" resource.ParallelTest(t, resource.TestCase{ @@ -1570,7 +1570,7 @@ func TestAccAWSInstance_changeInstanceType(t *testing.T) { var before ec2.Instance var after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceTypeBefore := "m3.medium" instanceTypeAfter := "m3.large" @@ -1613,7 +1613,7 @@ func TestAccAWSInstance_primaryNetworkInterface(t *testing.T) { var eni ec2.NetworkInterface resourceName := "aws_instance.test" eniResourceName := "aws_network_interface.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1644,7 +1644,7 @@ func TestAccAWSInstance_primaryNetworkInterfaceSourceDestCheck(t *testing.T) { var eni ec2.NetworkInterface resourceName := "aws_instance.test" eniResourceName := "aws_network_interface.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1678,7 +1678,7 @@ func TestAccAWSInstance_addSecondaryInterface(t *testing.T) { resourceName := "aws_instance.test" eniPrimaryResourceName := "aws_network_interface.primary" eniSecondaryResourceName := "aws_network_interface.secondary" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1717,7 +1717,7 @@ func TestAccAWSInstance_addSecurityGroupNetworkInterface(t *testing.T) { var before ec2.Instance var after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1752,7 +1752,7 @@ func TestAccAWSInstance_addSecurityGroupNetworkInterface(t *testing.T) { func TestAccAWSInstance_associatePublic_defaultPrivate(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1781,7 +1781,7 @@ func TestAccAWSInstance_associatePublic_defaultPrivate(t *testing.T) { func TestAccAWSInstance_associatePublic_defaultPublic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1810,7 +1810,7 @@ func TestAccAWSInstance_associatePublic_defaultPublic(t *testing.T) { func TestAccAWSInstance_associatePublic_explicitPublic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1839,7 +1839,7 @@ func TestAccAWSInstance_associatePublic_explicitPublic(t *testing.T) { func TestAccAWSInstance_associatePublic_explicitPrivate(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1868,7 +1868,7 @@ func TestAccAWSInstance_associatePublic_explicitPrivate(t *testing.T) { func TestAccAWSInstance_associatePublic_overridePublic(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1897,7 +1897,7 @@ func TestAccAWSInstance_associatePublic_overridePublic(t *testing.T) { func TestAccAWSInstance_associatePublic_overridePrivate(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -1925,7 +1925,7 @@ func TestAccAWSInstance_associatePublic_overridePrivate(t *testing.T) { func TestAccAWSInstance_getPasswordData_falseToTrue(t *testing.T) { var before, after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.medium" resource.ParallelTest(t, resource.TestCase{ @@ -1969,7 +1969,7 @@ func TestAccAWSInstance_getPasswordData_falseToTrue(t *testing.T) { func TestAccAWSInstance_getPasswordData_trueToFalse(t *testing.T) { var before, after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.medium" resource.ParallelTest(t, resource.TestCase{ @@ -2014,7 +2014,7 @@ func TestAccAWSInstance_getPasswordData_trueToFalse(t *testing.T) { func TestAccAWSInstance_CreditSpecification_Empty_NonBurstable(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m5.large" resource.ParallelTest(t, resource.TestCase{ @@ -2042,7 +2042,7 @@ func TestAccAWSInstance_CreditSpecification_Empty_NonBurstable(t *testing.T) { func TestAccAWSInstance_CreditSpecification_UnspecifiedToEmpty_NonBurstable(t *testing.T) { var instance ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m5.large" resource.ParallelTest(t, resource.TestCase{ @@ -2074,7 +2074,7 @@ func TestAccAWSInstance_CreditSpecification_UnspecifiedToEmpty_NonBurstable(t *t func TestAccAWSInstance_creditSpecification_unspecifiedDefaultsToStandard(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2102,7 +2102,7 @@ func TestAccAWSInstance_creditSpecification_unspecifiedDefaultsToStandard(t *tes func TestAccAWSInstance_creditSpecification_standardCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2138,7 +2138,7 @@ func TestAccAWSInstance_creditSpecification_standardCpuCredits(t *testing.T) { func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2174,7 +2174,7 @@ func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits(t *testing.T) { func TestAccAWSInstance_creditSpecification_unknownCpuCredits_t2(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2202,7 +2202,7 @@ func TestAccAWSInstance_creditSpecification_unknownCpuCredits_t2(t *testing.T) { func TestAccAWSInstance_creditSpecification_unknownCpuCredits_t3(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t3.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2230,7 +2230,7 @@ func TestAccAWSInstance_creditSpecification_unknownCpuCredits_t3(t *testing.T) { func TestAccAWSInstance_creditSpecification_updateCpuCredits(t *testing.T) { var first, second, third ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2274,7 +2274,7 @@ func TestAccAWSInstance_creditSpecification_updateCpuCredits(t *testing.T) { func TestAccAWSInstance_creditSpecification_isNotAppliedToNonBurstable(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "m1.small" resource.ParallelTest(t, resource.TestCase{ @@ -2301,7 +2301,7 @@ func TestAccAWSInstance_creditSpecification_isNotAppliedToNonBurstable(t *testin func TestAccAWSInstance_creditSpecificationT3_unspecifiedDefaultsToUnlimited(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t3.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2329,7 +2329,7 @@ func TestAccAWSInstance_creditSpecificationT3_unspecifiedDefaultsToUnlimited(t * func TestAccAWSInstance_creditSpecificationT3_standardCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t3.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2365,7 +2365,7 @@ func TestAccAWSInstance_creditSpecificationT3_standardCpuCredits(t *testing.T) { func TestAccAWSInstance_creditSpecificationT3_unlimitedCpuCredits(t *testing.T) { var first, second ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t3.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2401,7 +2401,7 @@ func TestAccAWSInstance_creditSpecificationT3_unlimitedCpuCredits(t *testing.T) func TestAccAWSInstance_creditSpecificationT3_updateCpuCredits(t *testing.T) { var first, second, third ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t3.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2445,7 +2445,7 @@ func TestAccAWSInstance_creditSpecificationT3_updateCpuCredits(t *testing.T) { func TestAccAWSInstance_creditSpecification_standardCpuCredits_t2Tot3Taint(t *testing.T) { var before, after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceTypeT2 := "t2.micro" instanceTypeT3 := "t3.micro" @@ -2487,7 +2487,7 @@ func TestAccAWSInstance_creditSpecification_standardCpuCredits_t2Tot3Taint(t *te func TestAccAWSInstance_creditSpecification_unlimitedCpuCredits_t2Tot3Taint(t *testing.T) { var before, after ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceTypeT2 := "t2.micro" instanceTypeT3 := "t3.micro" @@ -2556,7 +2556,7 @@ func TestAccAWSInstance_disappears(t *testing.T) { func TestAccAWSInstance_UserData_EmptyStringToUnspecified(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{ @@ -2589,7 +2589,7 @@ func TestAccAWSInstance_UserData_EmptyStringToUnspecified(t *testing.T) { func TestAccAWSInstance_UserData_UnspecifiedToEmptyString(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlphaNum)) + rName := acctest.RandomWithPrefix("tf-acc-test") instanceType := "t2.micro" resource.ParallelTest(t, resource.TestCase{