github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/aws/resource_aws_instance_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"regexp"
     7  	"testing"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/ec2"
    12  	"github.com/hashicorp/terraform/helper/acctest"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  	"github.com/hashicorp/terraform/helper/schema"
    15  	"github.com/hashicorp/terraform/terraform"
    16  )
    17  
    18  func TestAccAWSInstance_basic(t *testing.T) {
    19  	var v ec2.Instance
    20  	var vol *ec2.Volume
    21  
    22  	testCheck := func(*terraform.State) error {
    23  		if *v.Placement.AvailabilityZone != "us-west-2a" {
    24  			return fmt.Errorf("bad availability zone: %#v", *v.Placement.AvailabilityZone)
    25  		}
    26  
    27  		if len(v.SecurityGroups) == 0 {
    28  			return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
    29  		}
    30  		if *v.SecurityGroups[0].GroupName != "tf_test_foo" {
    31  			return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
    32  		}
    33  
    34  		return nil
    35  	}
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck: func() { testAccPreCheck(t) },
    39  
    40  		// We ignore security groups because even with EC2 classic
    41  		// we'll import as VPC security groups, which is fine. We verify
    42  		// VPC security group import in other tests
    43  		IDRefreshName:   "aws_instance.foo",
    44  		IDRefreshIgnore: []string{"security_groups", "vpc_security_group_ids"},
    45  
    46  		Providers:    testAccProviders,
    47  		CheckDestroy: testAccCheckInstanceDestroy,
    48  		Steps: []resource.TestStep{
    49  			// Create a volume to cover #1249
    50  			{
    51  				// Need a resource in this config so the provisioner will be available
    52  				Config: testAccInstanceConfig_pre,
    53  				Check: func(*terraform.State) error {
    54  					conn := testAccProvider.Meta().(*AWSClient).ec2conn
    55  					var err error
    56  					vol, err = conn.CreateVolume(&ec2.CreateVolumeInput{
    57  						AvailabilityZone: aws.String("us-west-2a"),
    58  						Size:             aws.Int64(int64(5)),
    59  					})
    60  					return err
    61  				},
    62  			},
    63  
    64  			{
    65  				Config: testAccInstanceConfig,
    66  				Check: resource.ComposeTestCheckFunc(
    67  					testAccCheckInstanceExists(
    68  						"aws_instance.foo", &v),
    69  					testCheck,
    70  					resource.TestCheckResourceAttr(
    71  						"aws_instance.foo",
    72  						"user_data",
    73  						"3dc39dda39be1205215e776bad998da361a5955d"),
    74  					resource.TestCheckResourceAttr(
    75  						"aws_instance.foo", "ebs_block_device.#", "0"),
    76  				),
    77  			},
    78  
    79  			// We repeat the exact same test so that we can be sure
    80  			// that the user data hash stuff is working without generating
    81  			// an incorrect diff.
    82  			{
    83  				Config: testAccInstanceConfig,
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testAccCheckInstanceExists(
    86  						"aws_instance.foo", &v),
    87  					testCheck,
    88  					resource.TestCheckResourceAttr(
    89  						"aws_instance.foo",
    90  						"user_data",
    91  						"3dc39dda39be1205215e776bad998da361a5955d"),
    92  					resource.TestCheckResourceAttr(
    93  						"aws_instance.foo", "ebs_block_device.#", "0"),
    94  				),
    95  			},
    96  
    97  			// Clean up volume created above
    98  			{
    99  				Config: testAccInstanceConfig,
   100  				Check: func(*terraform.State) error {
   101  					conn := testAccProvider.Meta().(*AWSClient).ec2conn
   102  					_, err := conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeId: vol.VolumeId})
   103  					return err
   104  				},
   105  			},
   106  		},
   107  	})
   108  }
   109  
   110  func TestAccAWSInstance_GP2IopsDevice(t *testing.T) {
   111  	var v ec2.Instance
   112  
   113  	testCheck := func() resource.TestCheckFunc {
   114  		return func(*terraform.State) error {
   115  
   116  			// Map out the block devices by name, which should be unique.
   117  			blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping)
   118  			for _, blockDevice := range v.BlockDeviceMappings {
   119  				blockDevices[*blockDevice.DeviceName] = blockDevice
   120  			}
   121  
   122  			// Check if the root block device exists.
   123  			if _, ok := blockDevices["/dev/sda1"]; !ok {
   124  				return fmt.Errorf("block device doesn't exist: /dev/sda1")
   125  			}
   126  
   127  			return nil
   128  		}
   129  	}
   130  
   131  	resource.Test(t, resource.TestCase{
   132  		PreCheck:      func() { testAccPreCheck(t) },
   133  		IDRefreshName: "aws_instance.foo",
   134  		IDRefreshIgnore: []string{
   135  			"ephemeral_block_device", "user_data", "security_groups", "vpc_security_groups"},
   136  		Providers:    testAccProviders,
   137  		CheckDestroy: testAccCheckInstanceDestroy,
   138  		Steps: []resource.TestStep{
   139  			{
   140  				Config: testAccInstanceGP2IopsDevice,
   141  				//Config: testAccInstanceConfigBlockDevices,
   142  				Check: resource.ComposeTestCheckFunc(
   143  					testAccCheckInstanceExists(
   144  						"aws_instance.foo", &v),
   145  					resource.TestCheckResourceAttr(
   146  						"aws_instance.foo", "root_block_device.#", "1"),
   147  					resource.TestCheckResourceAttr(
   148  						"aws_instance.foo", "root_block_device.0.volume_size", "11"),
   149  					resource.TestCheckResourceAttr(
   150  						"aws_instance.foo", "root_block_device.0.volume_type", "gp2"),
   151  					resource.TestCheckResourceAttr(
   152  						"aws_instance.foo", "root_block_device.0.iops", "100"),
   153  					testCheck(),
   154  				),
   155  			},
   156  		},
   157  	})
   158  }
   159  
   160  func TestAccAWSInstance_blockDevices(t *testing.T) {
   161  	var v ec2.Instance
   162  
   163  	testCheck := func() resource.TestCheckFunc {
   164  		return func(*terraform.State) error {
   165  
   166  			// Map out the block devices by name, which should be unique.
   167  			blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping)
   168  			for _, blockDevice := range v.BlockDeviceMappings {
   169  				blockDevices[*blockDevice.DeviceName] = blockDevice
   170  			}
   171  
   172  			// Check if the root block device exists.
   173  			if _, ok := blockDevices["/dev/sda1"]; !ok {
   174  				return fmt.Errorf("block device doesn't exist: /dev/sda1")
   175  			}
   176  
   177  			// Check if the secondary block device exists.
   178  			if _, ok := blockDevices["/dev/sdb"]; !ok {
   179  				return fmt.Errorf("block device doesn't exist: /dev/sdb")
   180  			}
   181  
   182  			// Check if the third block device exists.
   183  			if _, ok := blockDevices["/dev/sdc"]; !ok {
   184  				return fmt.Errorf("block device doesn't exist: /dev/sdc")
   185  			}
   186  
   187  			// Check if the encrypted block device exists
   188  			if _, ok := blockDevices["/dev/sdd"]; !ok {
   189  				return fmt.Errorf("block device doesn't exist: /dev/sdd")
   190  			}
   191  
   192  			return nil
   193  		}
   194  	}
   195  
   196  	resource.Test(t, resource.TestCase{
   197  		PreCheck:      func() { testAccPreCheck(t) },
   198  		IDRefreshName: "aws_instance.foo",
   199  		IDRefreshIgnore: []string{
   200  			"ephemeral_block_device", "security_groups", "vpc_security_groups"},
   201  		Providers:    testAccProviders,
   202  		CheckDestroy: testAccCheckInstanceDestroy,
   203  		Steps: []resource.TestStep{
   204  			{
   205  				Config: testAccInstanceConfigBlockDevices,
   206  				Check: resource.ComposeTestCheckFunc(
   207  					testAccCheckInstanceExists(
   208  						"aws_instance.foo", &v),
   209  					resource.TestCheckResourceAttr(
   210  						"aws_instance.foo", "root_block_device.#", "1"),
   211  					resource.TestCheckResourceAttr(
   212  						"aws_instance.foo", "root_block_device.0.volume_size", "11"),
   213  					resource.TestCheckResourceAttr(
   214  						"aws_instance.foo", "root_block_device.0.volume_type", "gp2"),
   215  					resource.TestCheckResourceAttr(
   216  						"aws_instance.foo", "ebs_block_device.#", "3"),
   217  					resource.TestCheckResourceAttr(
   218  						"aws_instance.foo", "ebs_block_device.2576023345.device_name", "/dev/sdb"),
   219  					resource.TestCheckResourceAttr(
   220  						"aws_instance.foo", "ebs_block_device.2576023345.volume_size", "9"),
   221  					resource.TestCheckResourceAttr(
   222  						"aws_instance.foo", "ebs_block_device.2576023345.volume_type", "standard"),
   223  					resource.TestCheckResourceAttr(
   224  						"aws_instance.foo", "ebs_block_device.2554893574.device_name", "/dev/sdc"),
   225  					resource.TestCheckResourceAttr(
   226  						"aws_instance.foo", "ebs_block_device.2554893574.volume_size", "10"),
   227  					resource.TestCheckResourceAttr(
   228  						"aws_instance.foo", "ebs_block_device.2554893574.volume_type", "io1"),
   229  					resource.TestCheckResourceAttr(
   230  						"aws_instance.foo", "ebs_block_device.2554893574.iops", "100"),
   231  					resource.TestCheckResourceAttr(
   232  						"aws_instance.foo", "ebs_block_device.2634515331.device_name", "/dev/sdd"),
   233  					resource.TestCheckResourceAttr(
   234  						"aws_instance.foo", "ebs_block_device.2634515331.encrypted", "true"),
   235  					resource.TestCheckResourceAttr(
   236  						"aws_instance.foo", "ebs_block_device.2634515331.volume_size", "12"),
   237  					resource.TestCheckResourceAttr(
   238  						"aws_instance.foo", "ephemeral_block_device.#", "1"),
   239  					resource.TestCheckResourceAttr(
   240  						"aws_instance.foo", "ephemeral_block_device.1692014856.device_name", "/dev/sde"),
   241  					resource.TestCheckResourceAttr(
   242  						"aws_instance.foo", "ephemeral_block_device.1692014856.virtual_name", "ephemeral0"),
   243  					testCheck(),
   244  				),
   245  			},
   246  		},
   247  	})
   248  }
   249  
   250  func TestAccAWSInstance_rootInstanceStore(t *testing.T) {
   251  	var v ec2.Instance
   252  
   253  	resource.Test(t, resource.TestCase{
   254  		PreCheck:      func() { testAccPreCheck(t) },
   255  		IDRefreshName: "aws_instance.foo",
   256  		Providers:     testAccProviders,
   257  		CheckDestroy:  testAccCheckInstanceDestroy,
   258  		Steps: []resource.TestStep{
   259  			{
   260  				Config: `
   261  					resource "aws_instance" "foo" {
   262  						# us-west-2
   263  						# Amazon Linux HVM Instance Store 64-bit (2016.09.0)
   264  						# https://aws.amazon.com/amazon-linux-ami
   265  						ami = "ami-44c36524"
   266  
   267  						# Only certain instance types support ephemeral root instance stores.
   268  						# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html
   269  						instance_type = "m3.medium"
   270  					}`,
   271  				Check: resource.ComposeTestCheckFunc(
   272  					testAccCheckInstanceExists(
   273  						"aws_instance.foo", &v),
   274  					resource.TestCheckResourceAttr(
   275  						"aws_instance.foo", "ami", "ami-44c36524"),
   276  					resource.TestCheckResourceAttr(
   277  						"aws_instance.foo", "ebs_block_device.#", "0"),
   278  					resource.TestCheckResourceAttr(
   279  						"aws_instance.foo", "ebs_optimized", "false"),
   280  					resource.TestCheckResourceAttr(
   281  						"aws_instance.foo", "instance_type", "m3.medium"),
   282  					resource.TestCheckResourceAttr(
   283  						"aws_instance.foo", "root_block_device.#", "0"),
   284  				),
   285  			},
   286  		},
   287  	})
   288  }
   289  
   290  func TestAcctABSInstance_noAMIEphemeralDevices(t *testing.T) {
   291  	var v ec2.Instance
   292  
   293  	testCheck := func() resource.TestCheckFunc {
   294  		return func(*terraform.State) error {
   295  
   296  			// Map out the block devices by name, which should be unique.
   297  			blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping)
   298  			for _, blockDevice := range v.BlockDeviceMappings {
   299  				blockDevices[*blockDevice.DeviceName] = blockDevice
   300  			}
   301  
   302  			// Check if the root block device exists.
   303  			if _, ok := blockDevices["/dev/sda1"]; !ok {
   304  				return fmt.Errorf("block device doesn't exist: /dev/sda1")
   305  			}
   306  
   307  			// Check if the secondary block not exists.
   308  			if _, ok := blockDevices["/dev/sdb"]; ok {
   309  				return fmt.Errorf("block device exist: /dev/sdb")
   310  			}
   311  
   312  			// Check if the third block device not exists.
   313  			if _, ok := blockDevices["/dev/sdc"]; ok {
   314  				return fmt.Errorf("block device exist: /dev/sdc")
   315  			}
   316  			return nil
   317  		}
   318  	}
   319  
   320  	resource.Test(t, resource.TestCase{
   321  		PreCheck:      func() { testAccPreCheck(t) },
   322  		IDRefreshName: "aws_instance.foo",
   323  		IDRefreshIgnore: []string{
   324  			"ephemeral_block_device", "security_groups", "vpc_security_groups"},
   325  		Providers:    testAccProviders,
   326  		CheckDestroy: testAccCheckInstanceDestroy,
   327  		Steps: []resource.TestStep{
   328  			{
   329  				Config: `
   330  					resource "aws_instance" "foo" {
   331  						# us-west-2
   332  						ami = "ami-01f05461"  // This AMI (Ubuntu) contains two ephemerals
   333  
   334  						instance_type = "c3.large"
   335  
   336  						root_block_device {
   337  							volume_type = "gp2"
   338  							volume_size = 11
   339  						}
   340  						ephemeral_block_device {
   341  							device_name = "/dev/sdb"
   342  							no_device = true
   343  						}
   344  						ephemeral_block_device {
   345  							device_name = "/dev/sdc"
   346  							no_device = true
   347  						}
   348  					}`,
   349  				Check: resource.ComposeTestCheckFunc(
   350  					testAccCheckInstanceExists(
   351  						"aws_instance.foo", &v),
   352  					resource.TestCheckResourceAttr(
   353  						"aws_instance.foo", "ami", "ami-01f05461"),
   354  					resource.TestCheckResourceAttr(
   355  						"aws_instance.foo", "ebs_optimized", "false"),
   356  					resource.TestCheckResourceAttr(
   357  						"aws_instance.foo", "instance_type", "c3.large"),
   358  					resource.TestCheckResourceAttr(
   359  						"aws_instance.foo", "root_block_device.#", "1"),
   360  					resource.TestCheckResourceAttr(
   361  						"aws_instance.foo", "root_block_device.0.volume_size", "11"),
   362  					resource.TestCheckResourceAttr(
   363  						"aws_instance.foo", "root_block_device.0.volume_type", "gp2"),
   364  					resource.TestCheckResourceAttr(
   365  						"aws_instance.foo", "ebs_block_device.#", "0"),
   366  					resource.TestCheckResourceAttr(
   367  						"aws_instance.foo", "ephemeral_block_device.#", "2"),
   368  					resource.TestCheckResourceAttr(
   369  						"aws_instance.foo", "ephemeral_block_device.172787947.device_name", "/dev/sdb"),
   370  					resource.TestCheckResourceAttr(
   371  						"aws_instance.foo", "ephemeral_block_device.172787947.no_device", "true"),
   372  					resource.TestCheckResourceAttr(
   373  						"aws_instance.foo", "ephemeral_block_device.3336996981.device_name", "/dev/sdc"),
   374  					resource.TestCheckResourceAttr(
   375  						"aws_instance.foo", "ephemeral_block_device.3336996981.no_device", "true"),
   376  					testCheck(),
   377  				),
   378  			},
   379  		},
   380  	})
   381  }
   382  
   383  func TestAccAWSInstance_sourceDestCheck(t *testing.T) {
   384  	var v ec2.Instance
   385  
   386  	testCheck := func(enabled bool) resource.TestCheckFunc {
   387  		return func(*terraform.State) error {
   388  			if v.SourceDestCheck == nil {
   389  				return fmt.Errorf("bad source_dest_check: got nil")
   390  			}
   391  			if *v.SourceDestCheck != enabled {
   392  				return fmt.Errorf("bad source_dest_check: %#v", *v.SourceDestCheck)
   393  			}
   394  
   395  			return nil
   396  		}
   397  	}
   398  
   399  	resource.Test(t, resource.TestCase{
   400  		PreCheck:      func() { testAccPreCheck(t) },
   401  		IDRefreshName: "aws_instance.foo",
   402  		Providers:     testAccProviders,
   403  		CheckDestroy:  testAccCheckInstanceDestroy,
   404  		Steps: []resource.TestStep{
   405  			{
   406  				Config: testAccInstanceConfigSourceDestDisable,
   407  				Check: resource.ComposeTestCheckFunc(
   408  					testAccCheckInstanceExists("aws_instance.foo", &v),
   409  					testCheck(false),
   410  				),
   411  			},
   412  
   413  			{
   414  				Config: testAccInstanceConfigSourceDestEnable,
   415  				Check: resource.ComposeTestCheckFunc(
   416  					testAccCheckInstanceExists("aws_instance.foo", &v),
   417  					testCheck(true),
   418  				),
   419  			},
   420  
   421  			{
   422  				Config: testAccInstanceConfigSourceDestDisable,
   423  				Check: resource.ComposeTestCheckFunc(
   424  					testAccCheckInstanceExists("aws_instance.foo", &v),
   425  					testCheck(false),
   426  				),
   427  			},
   428  		},
   429  	})
   430  }
   431  
   432  func TestAccAWSInstance_disableApiTermination(t *testing.T) {
   433  	var v ec2.Instance
   434  
   435  	checkDisableApiTermination := func(expected bool) resource.TestCheckFunc {
   436  		return func(*terraform.State) error {
   437  			conn := testAccProvider.Meta().(*AWSClient).ec2conn
   438  			r, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{
   439  				InstanceId: v.InstanceId,
   440  				Attribute:  aws.String("disableApiTermination"),
   441  			})
   442  			if err != nil {
   443  				return err
   444  			}
   445  			got := *r.DisableApiTermination.Value
   446  			if got != expected {
   447  				return fmt.Errorf("expected: %t, got: %t", expected, got)
   448  			}
   449  			return nil
   450  		}
   451  	}
   452  
   453  	resource.Test(t, resource.TestCase{
   454  		PreCheck:      func() { testAccPreCheck(t) },
   455  		IDRefreshName: "aws_instance.foo",
   456  		Providers:     testAccProviders,
   457  		CheckDestroy:  testAccCheckInstanceDestroy,
   458  		Steps: []resource.TestStep{
   459  			{
   460  				Config: testAccInstanceConfigDisableAPITermination(true),
   461  				Check: resource.ComposeTestCheckFunc(
   462  					testAccCheckInstanceExists("aws_instance.foo", &v),
   463  					checkDisableApiTermination(true),
   464  				),
   465  			},
   466  
   467  			{
   468  				Config: testAccInstanceConfigDisableAPITermination(false),
   469  				Check: resource.ComposeTestCheckFunc(
   470  					testAccCheckInstanceExists("aws_instance.foo", &v),
   471  					checkDisableApiTermination(false),
   472  				),
   473  			},
   474  		},
   475  	})
   476  }
   477  
   478  func TestAccAWSInstance_vpc(t *testing.T) {
   479  	var v ec2.Instance
   480  
   481  	resource.Test(t, resource.TestCase{
   482  		PreCheck:        func() { testAccPreCheck(t) },
   483  		IDRefreshName:   "aws_instance.foo",
   484  		IDRefreshIgnore: []string{"associate_public_ip_address"},
   485  		Providers:       testAccProviders,
   486  		CheckDestroy:    testAccCheckInstanceDestroy,
   487  		Steps: []resource.TestStep{
   488  			{
   489  				Config: testAccInstanceConfigVPC,
   490  				Check: resource.ComposeTestCheckFunc(
   491  					testAccCheckInstanceExists(
   492  						"aws_instance.foo", &v),
   493  					resource.TestCheckResourceAttr(
   494  						"aws_instance.foo",
   495  						"user_data",
   496  						"562a3e32810edf6ff09994f050f12e799452379d"),
   497  				),
   498  			},
   499  		},
   500  	})
   501  }
   502  
   503  func TestAccAWSInstance_ipv6_supportAddressCount(t *testing.T) {
   504  	var v ec2.Instance
   505  
   506  	resource.Test(t, resource.TestCase{
   507  		PreCheck:     func() { testAccPreCheck(t) },
   508  		Providers:    testAccProviders,
   509  		CheckDestroy: testAccCheckInstanceDestroy,
   510  		Steps: []resource.TestStep{
   511  			{
   512  				Config: testAccInstanceConfigIpv6Support,
   513  				Check: resource.ComposeTestCheckFunc(
   514  					testAccCheckInstanceExists(
   515  						"aws_instance.foo", &v),
   516  					resource.TestCheckResourceAttr(
   517  						"aws_instance.foo",
   518  						"ipv6_address_count",
   519  						"1"),
   520  				),
   521  			},
   522  		},
   523  	})
   524  }
   525  
   526  func TestAccAWSInstance_ipv6AddressCountAndSingleAddressCausesError(t *testing.T) {
   527  
   528  	resource.Test(t, resource.TestCase{
   529  		PreCheck:     func() { testAccPreCheck(t) },
   530  		Providers:    testAccProviders,
   531  		CheckDestroy: testAccCheckInstanceDestroy,
   532  		Steps: []resource.TestStep{
   533  			{
   534  				Config:      testAccInstanceConfigIpv6ErrorConfig,
   535  				ExpectError: regexp.MustCompile("Only 1 of `ipv6_address_count` or `ipv6_addresses` can be specified"),
   536  			},
   537  		},
   538  	})
   539  }
   540  
   541  func TestAccAWSInstance_ipv6_supportAddressCountWithIpv4(t *testing.T) {
   542  	var v ec2.Instance
   543  
   544  	resource.Test(t, resource.TestCase{
   545  		PreCheck:     func() { testAccPreCheck(t) },
   546  		Providers:    testAccProviders,
   547  		CheckDestroy: testAccCheckInstanceDestroy,
   548  		Steps: []resource.TestStep{
   549  			{
   550  				Config: testAccInstanceConfigIpv6SupportWithIpv4,
   551  				Check: resource.ComposeTestCheckFunc(
   552  					testAccCheckInstanceExists(
   553  						"aws_instance.foo", &v),
   554  					resource.TestCheckResourceAttr(
   555  						"aws_instance.foo",
   556  						"ipv6_address_count",
   557  						"1"),
   558  				),
   559  			},
   560  		},
   561  	})
   562  }
   563  
   564  func TestAccAWSInstance_multipleRegions(t *testing.T) {
   565  	var v ec2.Instance
   566  
   567  	// record the initialized providers so that we can use them to
   568  	// check for the instances in each region
   569  	var providers []*schema.Provider
   570  	providerFactories := map[string]terraform.ResourceProviderFactory{
   571  		"aws": func() (terraform.ResourceProvider, error) {
   572  			p := Provider()
   573  			providers = append(providers, p.(*schema.Provider))
   574  			return p, nil
   575  		},
   576  	}
   577  
   578  	resource.Test(t, resource.TestCase{
   579  		PreCheck:          func() { testAccPreCheck(t) },
   580  		ProviderFactories: providerFactories,
   581  		CheckDestroy:      testAccCheckInstanceDestroyWithProviders(&providers),
   582  		Steps: []resource.TestStep{
   583  			{
   584  				Config: testAccInstanceConfigMultipleRegions,
   585  				Check: resource.ComposeTestCheckFunc(
   586  					testAccCheckInstanceExistsWithProviders(
   587  						"aws_instance.foo", &v, &providers),
   588  					testAccCheckInstanceExistsWithProviders(
   589  						"aws_instance.bar", &v, &providers),
   590  				),
   591  			},
   592  		},
   593  	})
   594  }
   595  
   596  func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) {
   597  	var v ec2.Instance
   598  
   599  	resource.Test(t, resource.TestCase{
   600  		PreCheck:        func() { testAccPreCheck(t) },
   601  		IDRefreshName:   "aws_instance.foo_instance",
   602  		IDRefreshIgnore: []string{"associate_public_ip_address"},
   603  		Providers:       testAccProviders,
   604  		CheckDestroy:    testAccCheckInstanceDestroy,
   605  		Steps: []resource.TestStep{
   606  			{
   607  				Config: testAccInstanceNetworkInstanceSecurityGroups,
   608  				Check: resource.ComposeTestCheckFunc(
   609  					testAccCheckInstanceExists(
   610  						"aws_instance.foo_instance", &v),
   611  				),
   612  			},
   613  		},
   614  	})
   615  }
   616  
   617  func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) {
   618  	var v ec2.Instance
   619  
   620  	resource.Test(t, resource.TestCase{
   621  		PreCheck:      func() { testAccPreCheck(t) },
   622  		IDRefreshName: "aws_instance.foo_instance",
   623  		Providers:     testAccProviders,
   624  		CheckDestroy:  testAccCheckInstanceDestroy,
   625  		Steps: []resource.TestStep{
   626  			{
   627  				Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs,
   628  				Check: resource.ComposeTestCheckFunc(
   629  					testAccCheckInstanceExists(
   630  						"aws_instance.foo_instance", &v),
   631  					resource.TestCheckResourceAttr(
   632  						"aws_instance.foo_instance", "security_groups.#", "0"),
   633  					resource.TestCheckResourceAttr(
   634  						"aws_instance.foo_instance", "vpc_security_group_ids.#", "1"),
   635  				),
   636  			},
   637  		},
   638  	})
   639  }
   640  
   641  func TestAccAWSInstance_tags(t *testing.T) {
   642  	var v ec2.Instance
   643  
   644  	resource.Test(t, resource.TestCase{
   645  		PreCheck:     func() { testAccPreCheck(t) },
   646  		Providers:    testAccProviders,
   647  		CheckDestroy: testAccCheckInstanceDestroy,
   648  		Steps: []resource.TestStep{
   649  			{
   650  				Config: testAccCheckInstanceConfigTags,
   651  				Check: resource.ComposeTestCheckFunc(
   652  					testAccCheckInstanceExists("aws_instance.foo", &v),
   653  					testAccCheckTags(&v.Tags, "foo", "bar"),
   654  					// Guard against regression of https://github.com/hashicorp/terraform/issues/914
   655  					testAccCheckTags(&v.Tags, "#", ""),
   656  				),
   657  			},
   658  			{
   659  				Config: testAccCheckInstanceConfigTagsUpdate,
   660  				Check: resource.ComposeTestCheckFunc(
   661  					testAccCheckInstanceExists("aws_instance.foo", &v),
   662  					testAccCheckTags(&v.Tags, "foo", ""),
   663  					testAccCheckTags(&v.Tags, "bar", "baz"),
   664  				),
   665  			},
   666  		},
   667  	})
   668  }
   669  
   670  func TestAccAWSInstance_volumeTags(t *testing.T) {
   671  	var v ec2.Instance
   672  
   673  	resource.Test(t, resource.TestCase{
   674  		PreCheck:     func() { testAccPreCheck(t) },
   675  		Providers:    testAccProviders,
   676  		CheckDestroy: testAccCheckInstanceDestroy,
   677  		Steps: []resource.TestStep{
   678  			{
   679  				Config: testAccCheckInstanceConfigNoVolumeTags,
   680  				Check: resource.ComposeTestCheckFunc(
   681  					testAccCheckInstanceExists("aws_instance.foo", &v),
   682  					resource.TestCheckNoResourceAttr(
   683  						"aws_instance.foo", "volume_tags"),
   684  				),
   685  			},
   686  			{
   687  				Config: testAccCheckInstanceConfigWithVolumeTags,
   688  				Check: resource.ComposeTestCheckFunc(
   689  					testAccCheckInstanceExists("aws_instance.foo", &v),
   690  					resource.TestCheckResourceAttr(
   691  						"aws_instance.foo", "volume_tags.%", "1"),
   692  					resource.TestCheckResourceAttr(
   693  						"aws_instance.foo", "volume_tags.Name", "acceptance-test-volume-tag"),
   694  				),
   695  			},
   696  			{
   697  				Config: testAccCheckInstanceConfigWithVolumeTagsUpdate,
   698  				Check: resource.ComposeTestCheckFunc(
   699  					testAccCheckInstanceExists("aws_instance.foo", &v),
   700  					resource.TestCheckResourceAttr(
   701  						"aws_instance.foo", "volume_tags.%", "2"),
   702  					resource.TestCheckResourceAttr(
   703  						"aws_instance.foo", "volume_tags.Name", "acceptance-test-volume-tag"),
   704  					resource.TestCheckResourceAttr(
   705  						"aws_instance.foo", "volume_tags.Environment", "dev"),
   706  				),
   707  			},
   708  			{
   709  				Config: testAccCheckInstanceConfigNoVolumeTags,
   710  				Check: resource.ComposeTestCheckFunc(
   711  					testAccCheckInstanceExists("aws_instance.foo", &v),
   712  					resource.TestCheckNoResourceAttr(
   713  						"aws_instance.foo", "volume_tags"),
   714  				),
   715  			},
   716  		},
   717  	})
   718  }
   719  
   720  func TestAccAWSInstance_volumeTagsComputed(t *testing.T) {
   721  	var v ec2.Instance
   722  
   723  	resource.Test(t, resource.TestCase{
   724  		PreCheck:     func() { testAccPreCheck(t) },
   725  		Providers:    testAccProviders,
   726  		CheckDestroy: testAccCheckInstanceDestroy,
   727  		Steps: []resource.TestStep{
   728  			{
   729  				Config: testAccCheckInstanceConfigWithAttachedVolume,
   730  				Check: resource.ComposeTestCheckFunc(
   731  					testAccCheckInstanceExists("aws_instance.foo", &v),
   732  				),
   733  				ExpectNonEmptyPlan: false,
   734  			},
   735  		},
   736  	})
   737  }
   738  
   739  func TestAccAWSInstance_instanceProfileChange(t *testing.T) {
   740  	var v ec2.Instance
   741  	rName := acctest.RandString(5)
   742  
   743  	testCheckInstanceProfile := func() resource.TestCheckFunc {
   744  		return func(*terraform.State) error {
   745  			if v.IamInstanceProfile == nil {
   746  				return fmt.Errorf("Instance Profile is nil - we expected an InstanceProfile associated with the Instance")
   747  			}
   748  
   749  			return nil
   750  		}
   751  	}
   752  
   753  	resource.Test(t, resource.TestCase{
   754  		PreCheck:      func() { testAccPreCheck(t) },
   755  		IDRefreshName: "aws_instance.foo",
   756  		Providers:     testAccProviders,
   757  		CheckDestroy:  testAccCheckInstanceDestroy,
   758  		Steps: []resource.TestStep{
   759  			{
   760  				Config: testAccInstanceConfigWithoutInstanceProfile(rName),
   761  				Check: resource.ComposeTestCheckFunc(
   762  					testAccCheckInstanceExists("aws_instance.foo", &v),
   763  				),
   764  			},
   765  			{
   766  				Config: testAccInstanceConfigWithInstanceProfile(rName),
   767  				Check: resource.ComposeTestCheckFunc(
   768  					testAccCheckInstanceExists("aws_instance.foo", &v),
   769  					testCheckInstanceProfile(),
   770  				),
   771  			},
   772  		},
   773  	})
   774  }
   775  
   776  func TestAccAWSInstance_withIamInstanceProfile(t *testing.T) {
   777  	var v ec2.Instance
   778  	rName := acctest.RandString(5)
   779  
   780  	testCheckInstanceProfile := func() resource.TestCheckFunc {
   781  		return func(*terraform.State) error {
   782  			if v.IamInstanceProfile == nil {
   783  				return fmt.Errorf("Instance Profile is nil - we expected an InstanceProfile associated with the Instance")
   784  			}
   785  
   786  			return nil
   787  		}
   788  	}
   789  
   790  	resource.Test(t, resource.TestCase{
   791  		PreCheck:      func() { testAccPreCheck(t) },
   792  		IDRefreshName: "aws_instance.foo",
   793  		Providers:     testAccProviders,
   794  		CheckDestroy:  testAccCheckInstanceDestroy,
   795  		Steps: []resource.TestStep{
   796  			{
   797  				Config: testAccInstanceConfigWithInstanceProfile(rName),
   798  				Check: resource.ComposeTestCheckFunc(
   799  					testAccCheckInstanceExists("aws_instance.foo", &v),
   800  					testCheckInstanceProfile(),
   801  				),
   802  			},
   803  		},
   804  	})
   805  }
   806  
   807  func TestAccAWSInstance_privateIP(t *testing.T) {
   808  	var v ec2.Instance
   809  
   810  	testCheckPrivateIP := func() resource.TestCheckFunc {
   811  		return func(*terraform.State) error {
   812  			if *v.PrivateIpAddress != "10.1.1.42" {
   813  				return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress)
   814  			}
   815  
   816  			return nil
   817  		}
   818  	}
   819  
   820  	resource.Test(t, resource.TestCase{
   821  		PreCheck:      func() { testAccPreCheck(t) },
   822  		IDRefreshName: "aws_instance.foo",
   823  		Providers:     testAccProviders,
   824  		CheckDestroy:  testAccCheckInstanceDestroy,
   825  		Steps: []resource.TestStep{
   826  			{
   827  				Config: testAccInstanceConfigPrivateIP,
   828  				Check: resource.ComposeTestCheckFunc(
   829  					testAccCheckInstanceExists("aws_instance.foo", &v),
   830  					testCheckPrivateIP(),
   831  				),
   832  			},
   833  		},
   834  	})
   835  }
   836  
   837  func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) {
   838  	var v ec2.Instance
   839  
   840  	testCheckPrivateIP := func() resource.TestCheckFunc {
   841  		return func(*terraform.State) error {
   842  			if *v.PrivateIpAddress != "10.1.1.42" {
   843  				return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress)
   844  			}
   845  
   846  			return nil
   847  		}
   848  	}
   849  
   850  	resource.Test(t, resource.TestCase{
   851  		PreCheck:        func() { testAccPreCheck(t) },
   852  		IDRefreshName:   "aws_instance.foo",
   853  		IDRefreshIgnore: []string{"associate_public_ip_address"},
   854  		Providers:       testAccProviders,
   855  		CheckDestroy:    testAccCheckInstanceDestroy,
   856  		Steps: []resource.TestStep{
   857  			{
   858  				Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP,
   859  				Check: resource.ComposeTestCheckFunc(
   860  					testAccCheckInstanceExists("aws_instance.foo", &v),
   861  					testCheckPrivateIP(),
   862  				),
   863  			},
   864  		},
   865  	})
   866  }
   867  
   868  // Guard against regression with KeyPairs
   869  // https://github.com/hashicorp/terraform/issues/2302
   870  func TestAccAWSInstance_keyPairCheck(t *testing.T) {
   871  	var v ec2.Instance
   872  
   873  	testCheckKeyPair := func(keyName string) resource.TestCheckFunc {
   874  		return func(*terraform.State) error {
   875  			if v.KeyName == nil {
   876  				return fmt.Errorf("No Key Pair found, expected(%s)", keyName)
   877  			}
   878  			if v.KeyName != nil && *v.KeyName != keyName {
   879  				return fmt.Errorf("Bad key name, expected (%s), got (%s)", keyName, *v.KeyName)
   880  			}
   881  
   882  			return nil
   883  		}
   884  	}
   885  
   886  	keyPairName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
   887  
   888  	resource.Test(t, resource.TestCase{
   889  		PreCheck:        func() { testAccPreCheck(t) },
   890  		IDRefreshName:   "aws_instance.foo",
   891  		IDRefreshIgnore: []string{"source_dest_check"},
   892  		Providers:       testAccProviders,
   893  		CheckDestroy:    testAccCheckInstanceDestroy,
   894  		Steps: []resource.TestStep{
   895  			{
   896  				Config: testAccInstanceConfigKeyPair(keyPairName),
   897  				Check: resource.ComposeTestCheckFunc(
   898  					testAccCheckInstanceExists("aws_instance.foo", &v),
   899  					testCheckKeyPair(keyPairName),
   900  				),
   901  			},
   902  		},
   903  	})
   904  }
   905  
   906  func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) {
   907  	var v ec2.Instance
   908  
   909  	resource.Test(t, resource.TestCase{
   910  		PreCheck:     func() { testAccPreCheck(t) },
   911  		Providers:    testAccProviders,
   912  		CheckDestroy: testAccCheckInstanceDestroy,
   913  		Steps: []resource.TestStep{
   914  			{
   915  				Config: testAccInstanceConfigRootBlockDeviceMismatch,
   916  				Check: resource.ComposeTestCheckFunc(
   917  					testAccCheckInstanceExists("aws_instance.foo", &v),
   918  					resource.TestCheckResourceAttr(
   919  						"aws_instance.foo", "root_block_device.0.volume_size", "13"),
   920  				),
   921  			},
   922  		},
   923  	})
   924  }
   925  
   926  // This test reproduces the bug here:
   927  //   https://github.com/hashicorp/terraform/issues/1752
   928  //
   929  // I wish there were a way to exercise resources built with helper.Schema in a
   930  // unit context, in which case this test could be moved there, but for now this
   931  // will cover the bugfix.
   932  //
   933  // The following triggers "diffs didn't match during apply" without the fix in to
   934  // set NewRemoved on the .# field when it changes to 0.
   935  func TestAccAWSInstance_forceNewAndTagsDrift(t *testing.T) {
   936  	var v ec2.Instance
   937  
   938  	resource.Test(t, resource.TestCase{
   939  		PreCheck:      func() { testAccPreCheck(t) },
   940  		IDRefreshName: "aws_instance.foo",
   941  		Providers:     testAccProviders,
   942  		CheckDestroy:  testAccCheckInstanceDestroy,
   943  		Steps: []resource.TestStep{
   944  			{
   945  				Config: testAccInstanceConfigForceNewAndTagsDrift,
   946  				Check: resource.ComposeTestCheckFunc(
   947  					testAccCheckInstanceExists("aws_instance.foo", &v),
   948  					driftTags(&v),
   949  				),
   950  				ExpectNonEmptyPlan: true,
   951  			},
   952  			{
   953  				Config: testAccInstanceConfigForceNewAndTagsDrift_Update,
   954  				Check: resource.ComposeTestCheckFunc(
   955  					testAccCheckInstanceExists("aws_instance.foo", &v),
   956  				),
   957  			},
   958  		},
   959  	})
   960  }
   961  
   962  func TestAccAWSInstance_changeInstanceType(t *testing.T) {
   963  	var before ec2.Instance
   964  	var after ec2.Instance
   965  
   966  	resource.Test(t, resource.TestCase{
   967  		PreCheck:     func() { testAccPreCheck(t) },
   968  		Providers:    testAccProviders,
   969  		CheckDestroy: testAccCheckInstanceDestroy,
   970  		Steps: []resource.TestStep{
   971  			{
   972  				Config: testAccInstanceConfigWithSmallInstanceType,
   973  				Check: resource.ComposeTestCheckFunc(
   974  					testAccCheckInstanceExists("aws_instance.foo", &before),
   975  				),
   976  			},
   977  			{
   978  				Config: testAccInstanceConfigUpdateInstanceType,
   979  				Check: resource.ComposeTestCheckFunc(
   980  					testAccCheckInstanceExists("aws_instance.foo", &after),
   981  					testAccCheckInstanceNotRecreated(
   982  						t, &before, &after),
   983  				),
   984  			},
   985  		},
   986  	})
   987  }
   988  
   989  func TestAccAWSInstance_primaryNetworkInterface(t *testing.T) {
   990  	var instance ec2.Instance
   991  	var ini ec2.NetworkInterface
   992  
   993  	resource.Test(t, resource.TestCase{
   994  		PreCheck:     func() { testAccPreCheck(t) },
   995  		Providers:    testAccProviders,
   996  		CheckDestroy: testAccCheckInstanceDestroy,
   997  		Steps: []resource.TestStep{
   998  			{
   999  				Config: testAccInstanceConfigPrimaryNetworkInterface,
  1000  				Check: resource.ComposeTestCheckFunc(
  1001  					testAccCheckInstanceExists("aws_instance.foo", &instance),
  1002  					testAccCheckAWSENIExists("aws_network_interface.bar", &ini),
  1003  					resource.TestCheckResourceAttr("aws_instance.foo", "network_interface.#", "1"),
  1004  				),
  1005  			},
  1006  		},
  1007  	})
  1008  }
  1009  
  1010  func TestAccAWSInstance_primaryNetworkInterfaceSourceDestCheck(t *testing.T) {
  1011  	var instance ec2.Instance
  1012  	var ini ec2.NetworkInterface
  1013  
  1014  	resource.Test(t, resource.TestCase{
  1015  		PreCheck:     func() { testAccPreCheck(t) },
  1016  		Providers:    testAccProviders,
  1017  		CheckDestroy: testAccCheckInstanceDestroy,
  1018  		Steps: []resource.TestStep{
  1019  			{
  1020  				Config: testAccInstanceConfigPrimaryNetworkInterfaceSourceDestCheck,
  1021  				Check: resource.ComposeTestCheckFunc(
  1022  					testAccCheckInstanceExists("aws_instance.foo", &instance),
  1023  					testAccCheckAWSENIExists("aws_network_interface.bar", &ini),
  1024  					resource.TestCheckResourceAttr("aws_instance.foo", "source_dest_check", "false"),
  1025  				),
  1026  			},
  1027  		},
  1028  	})
  1029  }
  1030  
  1031  func TestAccAWSInstance_addSecondaryInterface(t *testing.T) {
  1032  	var before ec2.Instance
  1033  	var after ec2.Instance
  1034  	var iniPrimary ec2.NetworkInterface
  1035  	var iniSecondary ec2.NetworkInterface
  1036  
  1037  	resource.Test(t, resource.TestCase{
  1038  		PreCheck:     func() { testAccPreCheck(t) },
  1039  		Providers:    testAccProviders,
  1040  		CheckDestroy: testAccCheckInstanceDestroy,
  1041  		Steps: []resource.TestStep{
  1042  			{
  1043  				Config: testAccInstanceConfigAddSecondaryNetworkInterfaceBefore,
  1044  				Check: resource.ComposeTestCheckFunc(
  1045  					testAccCheckInstanceExists("aws_instance.foo", &before),
  1046  					testAccCheckAWSENIExists("aws_network_interface.primary", &iniPrimary),
  1047  					resource.TestCheckResourceAttr("aws_instance.foo", "network_interface.#", "1"),
  1048  				),
  1049  			},
  1050  			{
  1051  				Config: testAccInstanceConfigAddSecondaryNetworkInterfaceAfter,
  1052  				Check: resource.ComposeTestCheckFunc(
  1053  					testAccCheckInstanceExists("aws_instance.foo", &after),
  1054  					testAccCheckAWSENIExists("aws_network_interface.secondary", &iniSecondary),
  1055  					resource.TestCheckResourceAttr("aws_instance.foo", "network_interface.#", "1"),
  1056  				),
  1057  			},
  1058  		},
  1059  	})
  1060  }
  1061  
  1062  // https://github.com/hashicorp/terraform/issues/3205
  1063  func TestAccAWSInstance_addSecurityGroupNetworkInterface(t *testing.T) {
  1064  	var before ec2.Instance
  1065  	var after ec2.Instance
  1066  
  1067  	resource.Test(t, resource.TestCase{
  1068  		PreCheck:     func() { testAccPreCheck(t) },
  1069  		Providers:    testAccProviders,
  1070  		CheckDestroy: testAccCheckInstanceDestroy,
  1071  		Steps: []resource.TestStep{
  1072  			{
  1073  				Config: testAccInstanceConfigAddSecurityGroupBefore,
  1074  				Check: resource.ComposeTestCheckFunc(
  1075  					testAccCheckInstanceExists("aws_instance.foo", &before),
  1076  					resource.TestCheckResourceAttr("aws_instance.foo", "vpc_security_group_ids.#", "1"),
  1077  				),
  1078  			},
  1079  			{
  1080  				Config: testAccInstanceConfigAddSecurityGroupAfter,
  1081  				Check: resource.ComposeTestCheckFunc(
  1082  					testAccCheckInstanceExists("aws_instance.foo", &after),
  1083  					resource.TestCheckResourceAttr("aws_instance.foo", "vpc_security_group_ids.#", "2"),
  1084  				),
  1085  			},
  1086  		},
  1087  	})
  1088  }
  1089  
  1090  func testAccCheckInstanceNotRecreated(t *testing.T,
  1091  	before, after *ec2.Instance) resource.TestCheckFunc {
  1092  	return func(s *terraform.State) error {
  1093  		if *before.InstanceId != *after.InstanceId {
  1094  			t.Fatalf("AWS Instance IDs have changed. Before %s. After %s", *before.InstanceId, *after.InstanceId)
  1095  		}
  1096  		return nil
  1097  	}
  1098  }
  1099  
  1100  func testAccCheckInstanceDestroy(s *terraform.State) error {
  1101  	return testAccCheckInstanceDestroyWithProvider(s, testAccProvider)
  1102  }
  1103  
  1104  func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
  1105  	return func(s *terraform.State) error {
  1106  		for _, provider := range *providers {
  1107  			if provider.Meta() == nil {
  1108  				continue
  1109  			}
  1110  			if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil {
  1111  				return err
  1112  			}
  1113  		}
  1114  		return nil
  1115  	}
  1116  }
  1117  
  1118  func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
  1119  	conn := provider.Meta().(*AWSClient).ec2conn
  1120  
  1121  	for _, rs := range s.RootModule().Resources {
  1122  		if rs.Type != "aws_instance" {
  1123  			continue
  1124  		}
  1125  
  1126  		// Try to find the resource
  1127  		resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
  1128  			InstanceIds: []*string{aws.String(rs.Primary.ID)},
  1129  		})
  1130  		if err == nil {
  1131  			for _, r := range resp.Reservations {
  1132  				for _, i := range r.Instances {
  1133  					if i.State != nil && *i.State.Name != "terminated" {
  1134  						return fmt.Errorf("Found unterminated instance: %s", i)
  1135  					}
  1136  				}
  1137  			}
  1138  		}
  1139  
  1140  		// Verify the error is what we want
  1141  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidInstanceID.NotFound" {
  1142  			continue
  1143  		}
  1144  
  1145  		return err
  1146  	}
  1147  
  1148  	return nil
  1149  }
  1150  
  1151  func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc {
  1152  	providers := []*schema.Provider{testAccProvider}
  1153  	return testAccCheckInstanceExistsWithProviders(n, i, &providers)
  1154  }
  1155  
  1156  func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc {
  1157  	return func(s *terraform.State) error {
  1158  		rs, ok := s.RootModule().Resources[n]
  1159  		if !ok {
  1160  			return fmt.Errorf("Not found: %s", n)
  1161  		}
  1162  
  1163  		if rs.Primary.ID == "" {
  1164  			return fmt.Errorf("No ID is set")
  1165  		}
  1166  		for _, provider := range *providers {
  1167  			// Ignore if Meta is empty, this can happen for validation providers
  1168  			if provider.Meta() == nil {
  1169  				continue
  1170  			}
  1171  
  1172  			conn := provider.Meta().(*AWSClient).ec2conn
  1173  			resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
  1174  				InstanceIds: []*string{aws.String(rs.Primary.ID)},
  1175  			})
  1176  			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" {
  1177  				continue
  1178  			}
  1179  			if err != nil {
  1180  				return err
  1181  			}
  1182  
  1183  			if len(resp.Reservations) > 0 {
  1184  				*i = *resp.Reservations[0].Instances[0]
  1185  				return nil
  1186  			}
  1187  		}
  1188  
  1189  		return fmt.Errorf("Instance not found")
  1190  	}
  1191  }
  1192  
  1193  func TestInstanceTenancySchema(t *testing.T) {
  1194  	actualSchema := resourceAwsInstance().Schema["tenancy"]
  1195  	expectedSchema := &schema.Schema{
  1196  		Type:     schema.TypeString,
  1197  		Optional: true,
  1198  		Computed: true,
  1199  		ForceNew: true,
  1200  	}
  1201  	if !reflect.DeepEqual(actualSchema, expectedSchema) {
  1202  		t.Fatalf(
  1203  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
  1204  			actualSchema,
  1205  			expectedSchema)
  1206  	}
  1207  }
  1208  
  1209  func driftTags(instance *ec2.Instance) resource.TestCheckFunc {
  1210  	return func(s *terraform.State) error {
  1211  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
  1212  		_, err := conn.CreateTags(&ec2.CreateTagsInput{
  1213  			Resources: []*string{instance.InstanceId},
  1214  			Tags: []*ec2.Tag{
  1215  				{
  1216  					Key:   aws.String("Drift"),
  1217  					Value: aws.String("Happens"),
  1218  				},
  1219  			},
  1220  		})
  1221  		return err
  1222  	}
  1223  }
  1224  
  1225  const testAccInstanceConfig_pre = `
  1226  resource "aws_security_group" "tf_test_foo" {
  1227  	name = "tf_test_foo"
  1228  	description = "foo"
  1229  
  1230  	ingress {
  1231  		protocol = "icmp"
  1232  		from_port = -1
  1233  		to_port = -1
  1234  		cidr_blocks = ["0.0.0.0/0"]
  1235  	}
  1236  }
  1237  `
  1238  
  1239  const testAccInstanceConfig = `
  1240  resource "aws_security_group" "tf_test_foo" {
  1241  	name = "tf_test_foo"
  1242  	description = "foo"
  1243  
  1244  	ingress {
  1245  		protocol = "icmp"
  1246  		from_port = -1
  1247  		to_port = -1
  1248  		cidr_blocks = ["0.0.0.0/0"]
  1249  	}
  1250  }
  1251  
  1252  resource "aws_instance" "foo" {
  1253  	# us-west-2
  1254  	ami = "ami-4fccb37f"
  1255  	availability_zone = "us-west-2a"
  1256  
  1257  	instance_type = "m1.small"
  1258  	security_groups = ["${aws_security_group.tf_test_foo.name}"]
  1259  	user_data = "foo:-with-character's"
  1260  }
  1261  `
  1262  
  1263  const testAccInstanceConfigWithSmallInstanceType = `
  1264  resource "aws_instance" "foo" {
  1265  	# us-west-2
  1266  	ami = "ami-55a7ea65"
  1267  	availability_zone = "us-west-2a"
  1268  
  1269  	instance_type = "m3.medium"
  1270  
  1271  	tags {
  1272  	    Name = "tf-acctest"
  1273  	}
  1274  }
  1275  `
  1276  
  1277  const testAccInstanceConfigUpdateInstanceType = `
  1278  resource "aws_instance" "foo" {
  1279  	# us-west-2
  1280  	ami = "ami-55a7ea65"
  1281  	availability_zone = "us-west-2a"
  1282  
  1283  	instance_type = "m3.large"
  1284  
  1285  	tags {
  1286  	    Name = "tf-acctest"
  1287  	}
  1288  }
  1289  `
  1290  
  1291  const testAccInstanceGP2IopsDevice = `
  1292  resource "aws_instance" "foo" {
  1293  	# us-west-2
  1294  	ami = "ami-55a7ea65"
  1295  
  1296  	# In order to attach an encrypted volume to an instance you need to have an
  1297  	# m3.medium or larger. See "Supported Instance Types" in:
  1298  	# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html
  1299  	instance_type = "m3.medium"
  1300  
  1301  	root_block_device {
  1302  		volume_type = "gp2"
  1303  		volume_size = 11
  1304  	}
  1305  }
  1306  `
  1307  
  1308  const testAccInstanceConfigBlockDevices = `
  1309  resource "aws_instance" "foo" {
  1310  	# us-west-2
  1311  	ami = "ami-55a7ea65"
  1312  
  1313  	# In order to attach an encrypted volume to an instance you need to have an
  1314  	# m3.medium or larger. See "Supported Instance Types" in:
  1315  	# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html
  1316  	instance_type = "m3.medium"
  1317  
  1318  	root_block_device {
  1319  		volume_type = "gp2"
  1320  		volume_size = 11
  1321  	}
  1322  	ebs_block_device {
  1323  		device_name = "/dev/sdb"
  1324  		volume_size = 9
  1325  	}
  1326  	ebs_block_device {
  1327  		device_name = "/dev/sdc"
  1328  		volume_size = 10
  1329  		volume_type = "io1"
  1330  		iops = 100
  1331  	}
  1332  
  1333  	# Encrypted ebs block device
  1334  	ebs_block_device {
  1335  		device_name = "/dev/sdd"
  1336  		volume_size = 12
  1337  		encrypted = true
  1338  	}
  1339  
  1340  	ephemeral_block_device {
  1341  		device_name = "/dev/sde"
  1342  		virtual_name = "ephemeral0"
  1343  	}
  1344  }
  1345  `
  1346  
  1347  const testAccInstanceConfigSourceDestEnable = `
  1348  resource "aws_vpc" "foo" {
  1349  	cidr_block = "10.1.0.0/16"
  1350  	tags {
  1351  		Name = "testAccInstanceConfigSourceDestEnable"
  1352  	}
  1353  }
  1354  
  1355  resource "aws_subnet" "foo" {
  1356  	cidr_block = "10.1.1.0/24"
  1357  	vpc_id = "${aws_vpc.foo.id}"
  1358  }
  1359  
  1360  resource "aws_instance" "foo" {
  1361  	# us-west-2
  1362  	ami = "ami-4fccb37f"
  1363  	instance_type = "m1.small"
  1364  	subnet_id = "${aws_subnet.foo.id}"
  1365  }
  1366  `
  1367  
  1368  const testAccInstanceConfigSourceDestDisable = `
  1369  resource "aws_vpc" "foo" {
  1370  	cidr_block = "10.1.0.0/16"
  1371  	tags {
  1372  		Name = "testAccInstanceConfigSourceDestDisable"
  1373  	}
  1374  }
  1375  
  1376  resource "aws_subnet" "foo" {
  1377  	cidr_block = "10.1.1.0/24"
  1378  	vpc_id = "${aws_vpc.foo.id}"
  1379  }
  1380  
  1381  resource "aws_instance" "foo" {
  1382  	# us-west-2
  1383  	ami = "ami-4fccb37f"
  1384  	instance_type = "m1.small"
  1385  	subnet_id = "${aws_subnet.foo.id}"
  1386  	source_dest_check = false
  1387  }
  1388  `
  1389  
  1390  func testAccInstanceConfigDisableAPITermination(val bool) string {
  1391  	return fmt.Sprintf(`
  1392  	resource "aws_vpc" "foo" {
  1393  		cidr_block = "10.1.0.0/16"
  1394  		tags {
  1395  			Name = "testAccInstanceConfigDisableAPITermination"
  1396  		}
  1397  	}
  1398  
  1399  	resource "aws_subnet" "foo" {
  1400  		cidr_block = "10.1.1.0/24"
  1401  		vpc_id = "${aws_vpc.foo.id}"
  1402  	}
  1403  
  1404  	resource "aws_instance" "foo" {
  1405  		# us-west-2
  1406  		ami = "ami-4fccb37f"
  1407  		instance_type = "m1.small"
  1408  		subnet_id = "${aws_subnet.foo.id}"
  1409  		disable_api_termination = %t
  1410  	}
  1411  	`, val)
  1412  }
  1413  
  1414  const testAccInstanceConfigVPC = `
  1415  resource "aws_vpc" "foo" {
  1416  	cidr_block = "10.1.0.0/16"
  1417  	tags {
  1418  		Name = "testAccInstanceConfigVPC"
  1419  	}
  1420  }
  1421  
  1422  resource "aws_subnet" "foo" {
  1423  	cidr_block = "10.1.1.0/24"
  1424  	vpc_id = "${aws_vpc.foo.id}"
  1425  }
  1426  
  1427  resource "aws_instance" "foo" {
  1428  	# us-west-2
  1429  	ami = "ami-4fccb37f"
  1430  	instance_type = "m1.small"
  1431  	subnet_id = "${aws_subnet.foo.id}"
  1432  	associate_public_ip_address = true
  1433  	tenancy = "dedicated"
  1434  	# pre-encoded base64 data
  1435  	user_data = "3dc39dda39be1205215e776bad998da361a5955d"
  1436  }
  1437  `
  1438  
  1439  const testAccInstanceConfigIpv6ErrorConfig = `
  1440  resource "aws_vpc" "foo" {
  1441  	cidr_block = "10.1.0.0/16"
  1442  	assign_generated_ipv6_cidr_block = true
  1443  	tags {
  1444  		Name = "tf-ipv6-instance-acc-test"
  1445  	}
  1446  }
  1447  
  1448  resource "aws_subnet" "foo" {
  1449  	cidr_block = "10.1.1.0/24"
  1450  	vpc_id = "${aws_vpc.foo.id}"
  1451  	ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}"
  1452  	tags {
  1453  		Name = "tf-ipv6-instance-acc-test"
  1454  	}
  1455  }
  1456  
  1457  resource "aws_instance" "foo" {
  1458  	# us-west-2
  1459  	ami = "ami-c5eabbf5"
  1460  	instance_type = "t2.micro"
  1461  	subnet_id = "${aws_subnet.foo.id}"
  1462  	ipv6_addresses = ["2600:1f14:bb2:e501::10"]
  1463  	ipv6_address_count = 1
  1464  	tags {
  1465  		Name = "tf-ipv6-instance-acc-test"
  1466  	}
  1467  }
  1468  `
  1469  
  1470  const testAccInstanceConfigIpv6Support = `
  1471  resource "aws_vpc" "foo" {
  1472  	cidr_block = "10.1.0.0/16"
  1473  	assign_generated_ipv6_cidr_block = true
  1474  	tags {
  1475  		Name = "tf-ipv6-instance-acc-test"
  1476  	}
  1477  }
  1478  
  1479  resource "aws_subnet" "foo" {
  1480  	cidr_block = "10.1.1.0/24"
  1481  	vpc_id = "${aws_vpc.foo.id}"
  1482  	ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}"
  1483  	tags {
  1484  		Name = "tf-ipv6-instance-acc-test"
  1485  	}
  1486  }
  1487  
  1488  resource "aws_instance" "foo" {
  1489  	# us-west-2
  1490  	ami = "ami-c5eabbf5"
  1491  	instance_type = "t2.micro"
  1492  	subnet_id = "${aws_subnet.foo.id}"
  1493  
  1494  	ipv6_address_count = 1
  1495  	tags {
  1496  		Name = "tf-ipv6-instance-acc-test"
  1497  	}
  1498  }
  1499  `
  1500  
  1501  const testAccInstanceConfigIpv6SupportWithIpv4 = `
  1502  resource "aws_vpc" "foo" {
  1503  	cidr_block = "10.1.0.0/16"
  1504  	assign_generated_ipv6_cidr_block = true
  1505  	tags {
  1506  		Name = "tf-ipv6-instance-acc-test"
  1507  	}
  1508  }
  1509  
  1510  resource "aws_subnet" "foo" {
  1511  	cidr_block = "10.1.1.0/24"
  1512  	vpc_id = "${aws_vpc.foo.id}"
  1513  	ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}"
  1514  	tags {
  1515  		Name = "tf-ipv6-instance-acc-test"
  1516  	}
  1517  }
  1518  
  1519  resource "aws_instance" "foo" {
  1520  	# us-west-2
  1521  	ami = "ami-c5eabbf5"
  1522  	instance_type = "t2.micro"
  1523  	subnet_id = "${aws_subnet.foo.id}"
  1524  
  1525  	associate_public_ip_address = true
  1526  	ipv6_address_count = 1
  1527  	tags {
  1528  		Name = "tf-ipv6-instance-acc-test"
  1529  	}
  1530  }
  1531  `
  1532  
  1533  const testAccInstanceConfigMultipleRegions = `
  1534  provider "aws" {
  1535  	alias = "west"
  1536  	region = "us-west-2"
  1537  }
  1538  
  1539  provider "aws" {
  1540  	alias = "east"
  1541  	region = "us-east-1"
  1542  }
  1543  
  1544  resource "aws_instance" "foo" {
  1545  	# us-west-2
  1546  	provider = "aws.west"
  1547  	ami = "ami-4fccb37f"
  1548  	instance_type = "m1.small"
  1549  }
  1550  
  1551  resource "aws_instance" "bar" {
  1552  	# us-east-1
  1553  	provider = "aws.east"
  1554  	ami = "ami-8c6ea9e4"
  1555  	instance_type = "m1.small"
  1556  }
  1557  `
  1558  
  1559  const testAccCheckInstanceConfigTags = `
  1560  resource "aws_instance" "foo" {
  1561  	ami = "ami-4fccb37f"
  1562  	instance_type = "m1.small"
  1563  	tags {
  1564  		foo = "bar"
  1565  	}
  1566  }
  1567  `
  1568  
  1569  const testAccCheckInstanceConfigWithAttachedVolume = `
  1570  data "aws_ami" "debian_jessie_latest" {
  1571    most_recent = true
  1572  
  1573    filter {
  1574      name   = "name"
  1575      values = ["debian-jessie-*"]
  1576    }
  1577  
  1578    filter {
  1579      name   = "virtualization-type"
  1580      values = ["hvm"]
  1581    }
  1582  
  1583    filter {
  1584      name   = "architecture"
  1585      values = ["x86_64"]
  1586    }
  1587  
  1588    filter {
  1589      name   = "root-device-type"
  1590      values = ["ebs"]
  1591    }
  1592  
  1593    owners = ["379101102735"] # Debian
  1594  }
  1595  
  1596  resource "aws_instance" "foo" {
  1597    ami                         = "${data.aws_ami.debian_jessie_latest.id}"
  1598    associate_public_ip_address = true
  1599    count                       = 1
  1600    instance_type               = "t2.medium"
  1601  
  1602    root_block_device {
  1603      volume_size           = "10"
  1604      volume_type           = "standard"
  1605      delete_on_termination = true
  1606    }
  1607  
  1608    tags {
  1609      Name    = "test-terraform"
  1610    }
  1611  }
  1612  
  1613  resource "aws_ebs_volume" "test" {
  1614    depends_on        = ["aws_instance.foo"]
  1615    availability_zone = "${aws_instance.foo.availability_zone}"
  1616    type       = "gp2"
  1617    size              = "10"
  1618  
  1619    tags {
  1620      Name = "test-terraform"
  1621    }
  1622  }
  1623  
  1624  resource "aws_volume_attachment" "test" {
  1625    depends_on  = ["aws_ebs_volume.test"]
  1626    device_name = "/dev/xvdg"
  1627    volume_id   = "${aws_ebs_volume.test.id}"
  1628    instance_id = "${aws_instance.foo.id}"
  1629  }
  1630  `
  1631  
  1632  const testAccCheckInstanceConfigNoVolumeTags = `
  1633  resource "aws_instance" "foo" {
  1634  	ami = "ami-55a7ea65"
  1635  
  1636  	instance_type = "m3.medium"
  1637  
  1638  	root_block_device {
  1639  		volume_type = "gp2"
  1640  		volume_size = 11
  1641  	}
  1642  	ebs_block_device {
  1643  		device_name = "/dev/sdb"
  1644  		volume_size = 9
  1645  	}
  1646  	ebs_block_device {
  1647  		device_name = "/dev/sdc"
  1648  		volume_size = 10
  1649  		volume_type = "io1"
  1650  		iops = 100
  1651  	}
  1652  
  1653  	ebs_block_device {
  1654  		device_name = "/dev/sdd"
  1655  		volume_size = 12
  1656  		encrypted = true
  1657  	}
  1658  
  1659  	ephemeral_block_device {
  1660  		device_name = "/dev/sde"
  1661  		virtual_name = "ephemeral0"
  1662  	}
  1663  }
  1664  `
  1665  
  1666  const testAccCheckInstanceConfigWithVolumeTags = `
  1667  resource "aws_instance" "foo" {
  1668  	ami = "ami-55a7ea65"
  1669  
  1670  	instance_type = "m3.medium"
  1671  
  1672  	root_block_device {
  1673  		volume_type = "gp2"
  1674  		volume_size = 11
  1675  	}
  1676  	ebs_block_device {
  1677  		device_name = "/dev/sdb"
  1678  		volume_size = 9
  1679  	}
  1680  	ebs_block_device {
  1681  		device_name = "/dev/sdc"
  1682  		volume_size = 10
  1683  		volume_type = "io1"
  1684  		iops = 100
  1685  	}
  1686  
  1687  	ebs_block_device {
  1688  		device_name = "/dev/sdd"
  1689  		volume_size = 12
  1690  		encrypted = true
  1691  	}
  1692  
  1693  	ephemeral_block_device {
  1694  		device_name = "/dev/sde"
  1695  		virtual_name = "ephemeral0"
  1696  	}
  1697  
  1698  	volume_tags {
  1699  		Name = "acceptance-test-volume-tag"
  1700  	}
  1701  }
  1702  `
  1703  
  1704  const testAccCheckInstanceConfigWithVolumeTagsUpdate = `
  1705  resource "aws_instance" "foo" {
  1706  	ami = "ami-55a7ea65"
  1707  
  1708  	instance_type = "m3.medium"
  1709  
  1710  	root_block_device {
  1711  		volume_type = "gp2"
  1712  		volume_size = 11
  1713  	}
  1714  	ebs_block_device {
  1715  		device_name = "/dev/sdb"
  1716  		volume_size = 9
  1717  	}
  1718  	ebs_block_device {
  1719  		device_name = "/dev/sdc"
  1720  		volume_size = 10
  1721  		volume_type = "io1"
  1722  		iops = 100
  1723  	}
  1724  
  1725  	ebs_block_device {
  1726  		device_name = "/dev/sdd"
  1727  		volume_size = 12
  1728  		encrypted = true
  1729  	}
  1730  
  1731  	ephemeral_block_device {
  1732  		device_name = "/dev/sde"
  1733  		virtual_name = "ephemeral0"
  1734  	}
  1735  
  1736  	volume_tags {
  1737  		Name = "acceptance-test-volume-tag"
  1738  		Environment = "dev"
  1739  	}
  1740  }
  1741  `
  1742  
  1743  const testAccCheckInstanceConfigTagsUpdate = `
  1744  resource "aws_instance" "foo" {
  1745  	ami = "ami-4fccb37f"
  1746  	instance_type = "m1.small"
  1747  	tags {
  1748  		bar = "baz"
  1749  	}
  1750  }
  1751  `
  1752  
  1753  func testAccInstanceConfigWithoutInstanceProfile(rName string) string {
  1754  	return fmt.Sprintf(`
  1755  resource "aws_iam_role" "test" {
  1756  	name = "test-%s"
  1757  	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
  1758  }
  1759  
  1760  resource "aws_iam_instance_profile" "test" {
  1761  	name = "test-%s"
  1762  	roles = ["${aws_iam_role.test.name}"]
  1763  }
  1764  
  1765  resource "aws_instance" "foo" {
  1766  	ami = "ami-4fccb37f"
  1767  	instance_type = "m1.small"
  1768  	tags {
  1769  		bar = "baz"
  1770  	}
  1771  }`, rName, rName)
  1772  }
  1773  
  1774  func testAccInstanceConfigWithInstanceProfile(rName string) string {
  1775  	return fmt.Sprintf(`
  1776  resource "aws_iam_role" "test" {
  1777  	name = "test-%s"
  1778  	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
  1779  }
  1780  
  1781  resource "aws_iam_instance_profile" "test" {
  1782  	name = "test-%s"
  1783  	roles = ["${aws_iam_role.test.name}"]
  1784  }
  1785  
  1786  resource "aws_instance" "foo" {
  1787  	ami = "ami-4fccb37f"
  1788  	instance_type = "m1.small"
  1789  	iam_instance_profile = "${aws_iam_instance_profile.test.name}"
  1790  	tags {
  1791  		bar = "baz"
  1792  	}
  1793  }`, rName, rName)
  1794  }
  1795  
  1796  const testAccInstanceConfigPrivateIP = `
  1797  resource "aws_vpc" "foo" {
  1798  	cidr_block = "10.1.0.0/16"
  1799  	tags {
  1800  		Name = "testAccInstanceConfigPrivateIP"
  1801  	}
  1802  }
  1803  
  1804  resource "aws_subnet" "foo" {
  1805  	cidr_block = "10.1.1.0/24"
  1806  	vpc_id = "${aws_vpc.foo.id}"
  1807  }
  1808  
  1809  resource "aws_instance" "foo" {
  1810  	ami = "ami-c5eabbf5"
  1811  	instance_type = "t2.micro"
  1812  	subnet_id = "${aws_subnet.foo.id}"
  1813  	private_ip = "10.1.1.42"
  1814  }
  1815  `
  1816  
  1817  const testAccInstanceConfigAssociatePublicIPAndPrivateIP = `
  1818  resource "aws_vpc" "foo" {
  1819  	cidr_block = "10.1.0.0/16"
  1820  	tags {
  1821  		Name = "testAccInstanceConfigAssociatePublicIPAndPrivateIP"
  1822  	}
  1823  }
  1824  
  1825  resource "aws_subnet" "foo" {
  1826  	cidr_block = "10.1.1.0/24"
  1827  	vpc_id = "${aws_vpc.foo.id}"
  1828  }
  1829  
  1830  resource "aws_instance" "foo" {
  1831  	ami = "ami-c5eabbf5"
  1832  	instance_type = "t2.micro"
  1833  	subnet_id = "${aws_subnet.foo.id}"
  1834  	associate_public_ip_address = true
  1835  	private_ip = "10.1.1.42"
  1836  }
  1837  `
  1838  
  1839  const testAccInstanceNetworkInstanceSecurityGroups = `
  1840  resource "aws_internet_gateway" "gw" {
  1841    vpc_id = "${aws_vpc.foo.id}"
  1842  }
  1843  
  1844  resource "aws_vpc" "foo" {
  1845    cidr_block = "10.1.0.0/16"
  1846  	tags {
  1847  		Name = "tf-network-test"
  1848  	}
  1849  }
  1850  
  1851  resource "aws_security_group" "tf_test_foo" {
  1852    name = "tf_test_foo"
  1853    description = "foo"
  1854    vpc_id="${aws_vpc.foo.id}"
  1855  
  1856    ingress {
  1857      protocol = "icmp"
  1858      from_port = -1
  1859      to_port = -1
  1860      cidr_blocks = ["0.0.0.0/0"]
  1861    }
  1862  }
  1863  
  1864  resource "aws_subnet" "foo" {
  1865    cidr_block = "10.1.1.0/24"
  1866    vpc_id = "${aws_vpc.foo.id}"
  1867  }
  1868  
  1869  resource "aws_instance" "foo_instance" {
  1870    ami = "ami-21f78e11"
  1871    instance_type = "t1.micro"
  1872    vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"]
  1873    subnet_id = "${aws_subnet.foo.id}"
  1874    associate_public_ip_address = true
  1875  	depends_on = ["aws_internet_gateway.gw"]
  1876  }
  1877  
  1878  resource "aws_eip" "foo_eip" {
  1879    instance = "${aws_instance.foo_instance.id}"
  1880    vpc = true
  1881  	depends_on = ["aws_internet_gateway.gw"]
  1882  }
  1883  `
  1884  
  1885  const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = `
  1886  resource "aws_internet_gateway" "gw" {
  1887    vpc_id = "${aws_vpc.foo.id}"
  1888  }
  1889  
  1890  resource "aws_vpc" "foo" {
  1891    cidr_block = "10.1.0.0/16"
  1892  	tags {
  1893  		Name = "tf-network-test"
  1894  	}
  1895  }
  1896  
  1897  resource "aws_security_group" "tf_test_foo" {
  1898    name = "tf_test_foo"
  1899    description = "foo"
  1900    vpc_id="${aws_vpc.foo.id}"
  1901  
  1902    ingress {
  1903      protocol = "icmp"
  1904      from_port = -1
  1905      to_port = -1
  1906      cidr_blocks = ["0.0.0.0/0"]
  1907    }
  1908  }
  1909  
  1910  resource "aws_subnet" "foo" {
  1911    cidr_block = "10.1.1.0/24"
  1912    vpc_id = "${aws_vpc.foo.id}"
  1913  }
  1914  
  1915  resource "aws_instance" "foo_instance" {
  1916    ami = "ami-21f78e11"
  1917    instance_type = "t1.micro"
  1918    vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"]
  1919    subnet_id = "${aws_subnet.foo.id}"
  1920  	depends_on = ["aws_internet_gateway.gw"]
  1921  }
  1922  
  1923  resource "aws_eip" "foo_eip" {
  1924    instance = "${aws_instance.foo_instance.id}"
  1925    vpc = true
  1926  	depends_on = ["aws_internet_gateway.gw"]
  1927  }
  1928  `
  1929  
  1930  func testAccInstanceConfigKeyPair(keyPairName string) string {
  1931  	return fmt.Sprintf(`
  1932  provider "aws" {
  1933  	region = "us-east-1"
  1934  }
  1935  
  1936  resource "aws_key_pair" "debugging" {
  1937  	key_name = "%s"
  1938  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
  1939  }
  1940  
  1941  resource "aws_instance" "foo" {
  1942    ami = "ami-408c7f28"
  1943    instance_type = "t1.micro"
  1944    key_name = "${aws_key_pair.debugging.key_name}"
  1945  	tags {
  1946  		Name = "testAccInstanceConfigKeyPair_TestAMI"
  1947  	}
  1948  }
  1949  `, keyPairName)
  1950  }
  1951  
  1952  const testAccInstanceConfigRootBlockDeviceMismatch = `
  1953  resource "aws_vpc" "foo" {
  1954  	cidr_block = "10.1.0.0/16"
  1955  	tags {
  1956  		Name = "testAccInstanceConfigRootBlockDeviceMismatch"
  1957  	}
  1958  }
  1959  
  1960  resource "aws_subnet" "foo" {
  1961  	cidr_block = "10.1.1.0/24"
  1962  	vpc_id = "${aws_vpc.foo.id}"
  1963  }
  1964  
  1965  resource "aws_instance" "foo" {
  1966  	// This is an AMI with RootDeviceName: "/dev/sda1"; actual root: "/dev/sda"
  1967  	ami = "ami-ef5b69df"
  1968  	instance_type = "t1.micro"
  1969  	subnet_id = "${aws_subnet.foo.id}"
  1970  	root_block_device {
  1971  		volume_size = 13
  1972  	}
  1973  }
  1974  `
  1975  
  1976  const testAccInstanceConfigForceNewAndTagsDrift = `
  1977  resource "aws_vpc" "foo" {
  1978  	cidr_block = "10.1.0.0/16"
  1979  	tags {
  1980  		Name = "testAccInstanceConfigForceNewAndTagsDrift"
  1981  	}
  1982  }
  1983  
  1984  resource "aws_subnet" "foo" {
  1985  	cidr_block = "10.1.1.0/24"
  1986  	vpc_id = "${aws_vpc.foo.id}"
  1987  }
  1988  
  1989  resource "aws_instance" "foo" {
  1990  	ami = "ami-22b9a343"
  1991  	instance_type = "t2.nano"
  1992  	subnet_id = "${aws_subnet.foo.id}"
  1993  }
  1994  `
  1995  
  1996  const testAccInstanceConfigForceNewAndTagsDrift_Update = `
  1997  resource "aws_vpc" "foo" {
  1998  	cidr_block = "10.1.0.0/16"
  1999  	tags {
  2000  		Name = "testAccInstanceConfigForceNewAndTagsDrift_Update"
  2001  	}
  2002  }
  2003  
  2004  resource "aws_subnet" "foo" {
  2005  	cidr_block = "10.1.1.0/24"
  2006  	vpc_id = "${aws_vpc.foo.id}"
  2007  }
  2008  
  2009  resource "aws_instance" "foo" {
  2010  	ami = "ami-22b9a343"
  2011  	instance_type = "t2.micro"
  2012  	subnet_id = "${aws_subnet.foo.id}"
  2013  }
  2014  `
  2015  
  2016  const testAccInstanceConfigPrimaryNetworkInterface = `
  2017  resource "aws_vpc" "foo" {
  2018    cidr_block = "172.16.0.0/16"
  2019    tags {
  2020      Name = "tf-instance-test"
  2021    }
  2022  }
  2023  
  2024  resource "aws_subnet" "foo" {
  2025    vpc_id = "${aws_vpc.foo.id}"
  2026    cidr_block = "172.16.10.0/24"
  2027    availability_zone = "us-west-2a"
  2028    tags {
  2029      Name = "tf-instance-test"
  2030    }
  2031  }
  2032  
  2033  resource "aws_network_interface" "bar" {
  2034    subnet_id = "${aws_subnet.foo.id}"
  2035    private_ips = ["172.16.10.100"]
  2036    tags {
  2037      Name = "primary_network_interface"
  2038    }
  2039  }
  2040  
  2041  resource "aws_instance" "foo" {
  2042  	ami = "ami-22b9a343"
  2043  	instance_type = "t2.micro"
  2044  	network_interface {
  2045  	 network_interface_id = "${aws_network_interface.bar.id}"
  2046  	 device_index = 0
  2047    }
  2048  }
  2049  `
  2050  
  2051  const testAccInstanceConfigPrimaryNetworkInterfaceSourceDestCheck = `
  2052  resource "aws_vpc" "foo" {
  2053    cidr_block = "172.16.0.0/16"
  2054    tags {
  2055      Name = "tf-instance-test"
  2056    }
  2057  }
  2058  
  2059  resource "aws_subnet" "foo" {
  2060    vpc_id = "${aws_vpc.foo.id}"
  2061    cidr_block = "172.16.10.0/24"
  2062    availability_zone = "us-west-2a"
  2063    tags {
  2064      Name = "tf-instance-test"
  2065    }
  2066  }
  2067  
  2068  resource "aws_network_interface" "bar" {
  2069    subnet_id = "${aws_subnet.foo.id}"
  2070    private_ips = ["172.16.10.100"]
  2071    source_dest_check = false
  2072    tags {
  2073      Name = "primary_network_interface"
  2074    }
  2075  }
  2076  
  2077  resource "aws_instance" "foo" {
  2078  	ami = "ami-22b9a343"
  2079  	instance_type = "t2.micro"
  2080  	network_interface {
  2081  	 network_interface_id = "${aws_network_interface.bar.id}"
  2082  	 device_index = 0
  2083    }
  2084  }
  2085  `
  2086  
  2087  const testAccInstanceConfigAddSecondaryNetworkInterfaceBefore = `
  2088  resource "aws_vpc" "foo" {
  2089    cidr_block = "172.16.0.0/16"
  2090    tags {
  2091      Name = "tf-instance-test"
  2092    }
  2093  }
  2094  
  2095  resource "aws_subnet" "foo" {
  2096    vpc_id = "${aws_vpc.foo.id}"
  2097    cidr_block = "172.16.10.0/24"
  2098    availability_zone = "us-west-2a"
  2099    tags {
  2100      Name = "tf-instance-test"
  2101    }
  2102  }
  2103  
  2104  resource "aws_network_interface" "primary" {
  2105    subnet_id = "${aws_subnet.foo.id}"
  2106    private_ips = ["172.16.10.100"]
  2107    tags {
  2108      Name = "primary_network_interface"
  2109    }
  2110  }
  2111  
  2112  resource "aws_network_interface" "secondary" {
  2113    subnet_id = "${aws_subnet.foo.id}"
  2114    private_ips = ["172.16.10.101"]
  2115    tags {
  2116      Name = "secondary_network_interface"
  2117    }
  2118  }
  2119  
  2120  resource "aws_instance" "foo" {
  2121  	ami = "ami-22b9a343"
  2122  	instance_type = "t2.micro"
  2123  	network_interface {
  2124  	 network_interface_id = "${aws_network_interface.primary.id}"
  2125  	 device_index = 0
  2126    }
  2127  }
  2128  `
  2129  
  2130  const testAccInstanceConfigAddSecondaryNetworkInterfaceAfter = `
  2131  resource "aws_vpc" "foo" {
  2132    cidr_block = "172.16.0.0/16"
  2133    tags {
  2134      Name = "tf-instance-test"
  2135    }
  2136  }
  2137  
  2138  resource "aws_subnet" "foo" {
  2139    vpc_id = "${aws_vpc.foo.id}"
  2140    cidr_block = "172.16.10.0/24"
  2141    availability_zone = "us-west-2a"
  2142    tags {
  2143      Name = "tf-instance-test"
  2144    }
  2145  }
  2146  
  2147  resource "aws_network_interface" "primary" {
  2148    subnet_id = "${aws_subnet.foo.id}"
  2149    private_ips = ["172.16.10.100"]
  2150    tags {
  2151      Name = "primary_network_interface"
  2152    }
  2153  }
  2154  
  2155  // Attach previously created network interface, observe no state diff on instance resource
  2156  resource "aws_network_interface" "secondary" {
  2157    subnet_id = "${aws_subnet.foo.id}"
  2158    private_ips = ["172.16.10.101"]
  2159    tags {
  2160      Name = "secondary_network_interface"
  2161    }
  2162    attachment {
  2163      instance = "${aws_instance.foo.id}"
  2164      device_index = 1
  2165    }
  2166  }
  2167  
  2168  resource "aws_instance" "foo" {
  2169  	ami = "ami-22b9a343"
  2170  	instance_type = "t2.micro"
  2171  	network_interface {
  2172  	 network_interface_id = "${aws_network_interface.primary.id}"
  2173  	 device_index = 0
  2174    }
  2175  }
  2176  `
  2177  
  2178  const testAccInstanceConfigAddSecurityGroupBefore = `
  2179  resource "aws_vpc" "foo" {
  2180      cidr_block = "172.16.0.0/16"
  2181          tags {
  2182              Name = "tf-eni-test"
  2183          }
  2184  }
  2185  
  2186  resource "aws_subnet" "foo" {
  2187      vpc_id = "${aws_vpc.foo.id}"
  2188      cidr_block = "172.16.10.0/24"
  2189      availability_zone = "us-west-2a"
  2190          tags {
  2191              Name = "tf-foo-instance-add-sg-test"
  2192          }
  2193  }
  2194  
  2195  resource "aws_subnet" "bar" {
  2196      vpc_id = "${aws_vpc.foo.id}"
  2197      cidr_block = "172.16.11.0/24"
  2198      availability_zone = "us-west-2a"
  2199          tags {
  2200              Name = "tf-bar-instance-add-sg-test"
  2201          }
  2202  }
  2203  
  2204  resource "aws_security_group" "foo" {
  2205    vpc_id = "${aws_vpc.foo.id}"
  2206    description = "foo"
  2207    name = "foo"
  2208  }
  2209  
  2210  resource "aws_security_group" "bar" {
  2211    vpc_id = "${aws_vpc.foo.id}"
  2212    description = "bar"
  2213    name = "bar"
  2214  }
  2215  
  2216  resource "aws_instance" "foo" {
  2217      ami = "ami-c5eabbf5"
  2218      instance_type = "t2.micro"
  2219      subnet_id = "${aws_subnet.bar.id}"
  2220      associate_public_ip_address = false
  2221      vpc_security_group_ids = [
  2222        "${aws_security_group.foo.id}"
  2223      ]
  2224      tags {
  2225          Name = "foo-instance-sg-add-test"
  2226      }
  2227  }
  2228  
  2229  resource "aws_network_interface" "bar" {
  2230      subnet_id = "${aws_subnet.foo.id}"
  2231      private_ips = ["172.16.10.100"]
  2232      security_groups = ["${aws_security_group.foo.id}"]
  2233      attachment {
  2234          instance = "${aws_instance.foo.id}"
  2235          device_index = 1
  2236      }
  2237      tags {
  2238          Name = "bar_interface"
  2239      }
  2240  }
  2241  `
  2242  
  2243  const testAccInstanceConfigAddSecurityGroupAfter = `
  2244  resource "aws_vpc" "foo" {
  2245      cidr_block = "172.16.0.0/16"
  2246          tags {
  2247              Name = "tf-eni-test"
  2248          }
  2249  }
  2250  
  2251  resource "aws_subnet" "foo" {
  2252      vpc_id = "${aws_vpc.foo.id}"
  2253      cidr_block = "172.16.10.0/24"
  2254      availability_zone = "us-west-2a"
  2255          tags {
  2256              Name = "tf-foo-instance-add-sg-test"
  2257          }
  2258  }
  2259  
  2260  resource "aws_subnet" "bar" {
  2261      vpc_id = "${aws_vpc.foo.id}"
  2262      cidr_block = "172.16.11.0/24"
  2263      availability_zone = "us-west-2a"
  2264          tags {
  2265              Name = "tf-bar-instance-add-sg-test"
  2266          }
  2267  }
  2268  
  2269  resource "aws_security_group" "foo" {
  2270    vpc_id = "${aws_vpc.foo.id}"
  2271    description = "foo"
  2272    name = "foo"
  2273  }
  2274  
  2275  resource "aws_security_group" "bar" {
  2276    vpc_id = "${aws_vpc.foo.id}"
  2277    description = "bar"
  2278    name = "bar"
  2279  }
  2280  
  2281  resource "aws_instance" "foo" {
  2282      ami = "ami-c5eabbf5"
  2283      instance_type = "t2.micro"
  2284      subnet_id = "${aws_subnet.bar.id}"
  2285      associate_public_ip_address = false
  2286      vpc_security_group_ids = [
  2287        "${aws_security_group.foo.id}",
  2288        "${aws_security_group.bar.id}"
  2289      ]
  2290      tags {
  2291          Name = "foo-instance-sg-add-test"
  2292      }
  2293  }
  2294  
  2295  resource "aws_network_interface" "bar" {
  2296      subnet_id = "${aws_subnet.foo.id}"
  2297      private_ips = ["172.16.10.100"]
  2298      security_groups = ["${aws_security_group.foo.id}"]
  2299      attachment {
  2300          instance = "${aws_instance.foo.id}"
  2301          device_index = 1
  2302      }
  2303      tags {
  2304          Name = "bar_interface"
  2305      }
  2306  }
  2307  `