github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/aws/resource_aws_instance_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccAWSInstance_basic(t *testing.T) {
    17  	var v ec2.Instance
    18  	var vol *ec2.Volume
    19  
    20  	testCheck := func(*terraform.State) error {
    21  		if *v.Placement.AvailabilityZone != "us-west-2a" {
    22  			return fmt.Errorf("bad availability zone: %#v", *v.Placement.AvailabilityZone)
    23  		}
    24  
    25  		if len(v.SecurityGroups) == 0 {
    26  			return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
    27  		}
    28  		if *v.SecurityGroups[0].GroupName != "tf_test_foo" {
    29  			return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
    30  		}
    31  
    32  		return nil
    33  	}
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckInstanceDestroy,
    39  		Steps: []resource.TestStep{
    40  			// Create a volume to cover #1249
    41  			resource.TestStep{
    42  				// Need a resource in this config so the provisioner will be available
    43  				Config: testAccInstanceConfig_pre,
    44  				Check: func(*terraform.State) error {
    45  					conn := testAccProvider.Meta().(*AWSClient).ec2conn
    46  					var err error
    47  					vol, err = conn.CreateVolume(&ec2.CreateVolumeInput{
    48  						AvailabilityZone: aws.String("us-west-2a"),
    49  						Size:             aws.Int64(int64(5)),
    50  					})
    51  					return err
    52  				},
    53  			},
    54  
    55  			resource.TestStep{
    56  				Config: testAccInstanceConfig,
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckInstanceExists(
    59  						"aws_instance.foo", &v),
    60  					testCheck,
    61  					resource.TestCheckResourceAttr(
    62  						"aws_instance.foo",
    63  						"user_data",
    64  						"3dc39dda39be1205215e776bad998da361a5955d"),
    65  					resource.TestCheckResourceAttr(
    66  						"aws_instance.foo", "ebs_block_device.#", "0"),
    67  				),
    68  			},
    69  
    70  			// We repeat the exact same test so that we can be sure
    71  			// that the user data hash stuff is working without generating
    72  			// an incorrect diff.
    73  			resource.TestStep{
    74  				Config: testAccInstanceConfig,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckInstanceExists(
    77  						"aws_instance.foo", &v),
    78  					testCheck,
    79  					resource.TestCheckResourceAttr(
    80  						"aws_instance.foo",
    81  						"user_data",
    82  						"3dc39dda39be1205215e776bad998da361a5955d"),
    83  					resource.TestCheckResourceAttr(
    84  						"aws_instance.foo", "ebs_block_device.#", "0"),
    85  				),
    86  			},
    87  
    88  			// Clean up volume created above
    89  			resource.TestStep{
    90  				Config: testAccInstanceConfig,
    91  				Check: func(*terraform.State) error {
    92  					conn := testAccProvider.Meta().(*AWSClient).ec2conn
    93  					_, err := conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeId: vol.VolumeId})
    94  					return err
    95  				},
    96  			},
    97  		},
    98  	})
    99  }
   100  
   101  func TestAccAWSInstance_blockDevices(t *testing.T) {
   102  	var v ec2.Instance
   103  
   104  	testCheck := func() resource.TestCheckFunc {
   105  		return func(*terraform.State) error {
   106  
   107  			// Map out the block devices by name, which should be unique.
   108  			blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping)
   109  			for _, blockDevice := range v.BlockDeviceMappings {
   110  				blockDevices[*blockDevice.DeviceName] = blockDevice
   111  			}
   112  
   113  			// Check if the root block device exists.
   114  			if _, ok := blockDevices["/dev/sda1"]; !ok {
   115  				return fmt.Errorf("block device doesn't exist: /dev/sda1")
   116  			}
   117  
   118  			// Check if the secondary block device exists.
   119  			if _, ok := blockDevices["/dev/sdb"]; !ok {
   120  				return fmt.Errorf("block device doesn't exist: /dev/sdb")
   121  			}
   122  
   123  			// Check if the third block device exists.
   124  			if _, ok := blockDevices["/dev/sdc"]; !ok {
   125  				return fmt.Errorf("block device doesn't exist: /dev/sdc")
   126  			}
   127  
   128  			// Check if the encrypted block device exists
   129  			if _, ok := blockDevices["/dev/sdd"]; !ok {
   130  				return fmt.Errorf("block device doesn't exist: /dev/sdd")
   131  			}
   132  
   133  			return nil
   134  		}
   135  	}
   136  
   137  	resource.Test(t, resource.TestCase{
   138  		PreCheck:     func() { testAccPreCheck(t) },
   139  		Providers:    testAccProviders,
   140  		CheckDestroy: testAccCheckInstanceDestroy,
   141  		Steps: []resource.TestStep{
   142  			resource.TestStep{
   143  				Config: testAccInstanceConfigBlockDevices,
   144  				Check: resource.ComposeTestCheckFunc(
   145  					testAccCheckInstanceExists(
   146  						"aws_instance.foo", &v),
   147  					resource.TestCheckResourceAttr(
   148  						"aws_instance.foo", "root_block_device.#", "1"),
   149  					resource.TestCheckResourceAttr(
   150  						"aws_instance.foo", "root_block_device.0.volume_size", "11"),
   151  					resource.TestCheckResourceAttr(
   152  						"aws_instance.foo", "root_block_device.0.volume_type", "gp2"),
   153  					resource.TestCheckResourceAttr(
   154  						"aws_instance.foo", "ebs_block_device.#", "3"),
   155  					resource.TestCheckResourceAttr(
   156  						"aws_instance.foo", "ebs_block_device.2576023345.device_name", "/dev/sdb"),
   157  					resource.TestCheckResourceAttr(
   158  						"aws_instance.foo", "ebs_block_device.2576023345.volume_size", "9"),
   159  					resource.TestCheckResourceAttr(
   160  						"aws_instance.foo", "ebs_block_device.2576023345.volume_type", "standard"),
   161  					resource.TestCheckResourceAttr(
   162  						"aws_instance.foo", "ebs_block_device.2554893574.device_name", "/dev/sdc"),
   163  					resource.TestCheckResourceAttr(
   164  						"aws_instance.foo", "ebs_block_device.2554893574.volume_size", "10"),
   165  					resource.TestCheckResourceAttr(
   166  						"aws_instance.foo", "ebs_block_device.2554893574.volume_type", "io1"),
   167  					resource.TestCheckResourceAttr(
   168  						"aws_instance.foo", "ebs_block_device.2554893574.iops", "100"),
   169  					resource.TestCheckResourceAttr(
   170  						"aws_instance.foo", "ebs_block_device.2634515331.device_name", "/dev/sdd"),
   171  					resource.TestCheckResourceAttr(
   172  						"aws_instance.foo", "ebs_block_device.2634515331.encrypted", "true"),
   173  					resource.TestCheckResourceAttr(
   174  						"aws_instance.foo", "ebs_block_device.2634515331.volume_size", "12"),
   175  					resource.TestCheckResourceAttr(
   176  						"aws_instance.foo", "ephemeral_block_device.#", "1"),
   177  					resource.TestCheckResourceAttr(
   178  						"aws_instance.foo", "ephemeral_block_device.1692014856.device_name", "/dev/sde"),
   179  					resource.TestCheckResourceAttr(
   180  						"aws_instance.foo", "ephemeral_block_device.1692014856.virtual_name", "ephemeral0"),
   181  					testCheck(),
   182  				),
   183  			},
   184  		},
   185  	})
   186  }
   187  
   188  func TestAccAWSInstance_sourceDestCheck(t *testing.T) {
   189  	var v ec2.Instance
   190  
   191  	testCheck := func(enabled bool) resource.TestCheckFunc {
   192  		return func(*terraform.State) error {
   193  			if v.SourceDestCheck == nil {
   194  				return fmt.Errorf("bad source_dest_check: got nil")
   195  			}
   196  			if *v.SourceDestCheck != enabled {
   197  				return fmt.Errorf("bad source_dest_check: %#v", *v.SourceDestCheck)
   198  			}
   199  
   200  			return nil
   201  		}
   202  	}
   203  
   204  	resource.Test(t, resource.TestCase{
   205  		PreCheck:     func() { testAccPreCheck(t) },
   206  		Providers:    testAccProviders,
   207  		CheckDestroy: testAccCheckInstanceDestroy,
   208  		Steps: []resource.TestStep{
   209  			resource.TestStep{
   210  				Config: testAccInstanceConfigSourceDestDisable,
   211  				Check: resource.ComposeTestCheckFunc(
   212  					testAccCheckInstanceExists("aws_instance.foo", &v),
   213  					testCheck(false),
   214  				),
   215  			},
   216  
   217  			resource.TestStep{
   218  				Config: testAccInstanceConfigSourceDestEnable,
   219  				Check: resource.ComposeTestCheckFunc(
   220  					testAccCheckInstanceExists("aws_instance.foo", &v),
   221  					testCheck(true),
   222  				),
   223  			},
   224  
   225  			resource.TestStep{
   226  				Config: testAccInstanceConfigSourceDestDisable,
   227  				Check: resource.ComposeTestCheckFunc(
   228  					testAccCheckInstanceExists("aws_instance.foo", &v),
   229  					testCheck(false),
   230  				),
   231  			},
   232  		},
   233  	})
   234  }
   235  
   236  func TestAccAWSInstance_disableApiTermination(t *testing.T) {
   237  	var v ec2.Instance
   238  
   239  	checkDisableApiTermination := func(expected bool) resource.TestCheckFunc {
   240  		return func(*terraform.State) error {
   241  			conn := testAccProvider.Meta().(*AWSClient).ec2conn
   242  			r, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{
   243  				InstanceId: v.InstanceId,
   244  				Attribute:  aws.String("disableApiTermination"),
   245  			})
   246  			if err != nil {
   247  				return err
   248  			}
   249  			got := *r.DisableApiTermination.Value
   250  			if got != expected {
   251  				return fmt.Errorf("expected: %t, got: %t", expected, got)
   252  			}
   253  			return nil
   254  		}
   255  	}
   256  
   257  	resource.Test(t, resource.TestCase{
   258  		PreCheck:     func() { testAccPreCheck(t) },
   259  		Providers:    testAccProviders,
   260  		CheckDestroy: testAccCheckInstanceDestroy,
   261  		Steps: []resource.TestStep{
   262  			resource.TestStep{
   263  				Config: testAccInstanceConfigDisableAPITermination(true),
   264  				Check: resource.ComposeTestCheckFunc(
   265  					testAccCheckInstanceExists("aws_instance.foo", &v),
   266  					checkDisableApiTermination(true),
   267  				),
   268  			},
   269  
   270  			resource.TestStep{
   271  				Config: testAccInstanceConfigDisableAPITermination(false),
   272  				Check: resource.ComposeTestCheckFunc(
   273  					testAccCheckInstanceExists("aws_instance.foo", &v),
   274  					checkDisableApiTermination(false),
   275  				),
   276  			},
   277  		},
   278  	})
   279  }
   280  
   281  func TestAccAWSInstance_vpc(t *testing.T) {
   282  	var v ec2.Instance
   283  
   284  	resource.Test(t, resource.TestCase{
   285  		PreCheck:     func() { testAccPreCheck(t) },
   286  		Providers:    testAccProviders,
   287  		CheckDestroy: testAccCheckInstanceDestroy,
   288  		Steps: []resource.TestStep{
   289  			resource.TestStep{
   290  				Config: testAccInstanceConfigVPC,
   291  				Check: resource.ComposeTestCheckFunc(
   292  					testAccCheckInstanceExists(
   293  						"aws_instance.foo", &v),
   294  				),
   295  			},
   296  		},
   297  	})
   298  }
   299  
   300  func TestAccAWSInstance_multipleRegions(t *testing.T) {
   301  	var v ec2.Instance
   302  
   303  	// record the initialized providers so that we can use them to
   304  	// check for the instances in each region
   305  	var providers []*schema.Provider
   306  	providerFactories := map[string]terraform.ResourceProviderFactory{
   307  		"aws": func() (terraform.ResourceProvider, error) {
   308  			p := Provider()
   309  			providers = append(providers, p.(*schema.Provider))
   310  			return p, nil
   311  		},
   312  	}
   313  
   314  	resource.Test(t, resource.TestCase{
   315  		PreCheck:          func() { testAccPreCheck(t) },
   316  		ProviderFactories: providerFactories,
   317  		CheckDestroy:      testAccCheckInstanceDestroyWithProviders(&providers),
   318  		Steps: []resource.TestStep{
   319  			resource.TestStep{
   320  				Config: testAccInstanceConfigMultipleRegions,
   321  				Check: resource.ComposeTestCheckFunc(
   322  					testAccCheckInstanceExistsWithProviders(
   323  						"aws_instance.foo", &v, &providers),
   324  					testAccCheckInstanceExistsWithProviders(
   325  						"aws_instance.bar", &v, &providers),
   326  				),
   327  			},
   328  		},
   329  	})
   330  }
   331  
   332  func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) {
   333  	var v ec2.Instance
   334  
   335  	resource.Test(t, resource.TestCase{
   336  		PreCheck:     func() { testAccPreCheck(t) },
   337  		Providers:    testAccProviders,
   338  		CheckDestroy: testAccCheckInstanceDestroy,
   339  		Steps: []resource.TestStep{
   340  			resource.TestStep{
   341  				Config: testAccInstanceNetworkInstanceSecurityGroups,
   342  				Check: resource.ComposeTestCheckFunc(
   343  					testAccCheckInstanceExists(
   344  						"aws_instance.foo_instance", &v),
   345  				),
   346  			},
   347  		},
   348  	})
   349  }
   350  
   351  func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) {
   352  	var v ec2.Instance
   353  
   354  	resource.Test(t, resource.TestCase{
   355  		PreCheck:     func() { testAccPreCheck(t) },
   356  		Providers:    testAccProviders,
   357  		CheckDestroy: testAccCheckInstanceDestroy,
   358  		Steps: []resource.TestStep{
   359  			resource.TestStep{
   360  				Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs,
   361  				Check: resource.ComposeTestCheckFunc(
   362  					testAccCheckInstanceExists(
   363  						"aws_instance.foo_instance", &v),
   364  					resource.TestCheckResourceAttr(
   365  						"aws_instance.foo_instance", "security_groups.#", "0"),
   366  					resource.TestCheckResourceAttr(
   367  						"aws_instance.foo_instance", "vpc_security_group_ids.#", "1"),
   368  				),
   369  			},
   370  		},
   371  	})
   372  }
   373  
   374  func TestAccAWSInstance_tags(t *testing.T) {
   375  	var v ec2.Instance
   376  
   377  	resource.Test(t, resource.TestCase{
   378  		PreCheck:     func() { testAccPreCheck(t) },
   379  		Providers:    testAccProviders,
   380  		CheckDestroy: testAccCheckInstanceDestroy,
   381  		Steps: []resource.TestStep{
   382  			resource.TestStep{
   383  				Config: testAccCheckInstanceConfigTags,
   384  				Check: resource.ComposeTestCheckFunc(
   385  					testAccCheckInstanceExists("aws_instance.foo", &v),
   386  					testAccCheckTags(&v.Tags, "foo", "bar"),
   387  					// Guard against regression of https://github.com/hashicorp/terraform/issues/914
   388  					testAccCheckTags(&v.Tags, "#", ""),
   389  				),
   390  			},
   391  
   392  			resource.TestStep{
   393  				Config: testAccCheckInstanceConfigTagsUpdate,
   394  				Check: resource.ComposeTestCheckFunc(
   395  					testAccCheckInstanceExists("aws_instance.foo", &v),
   396  					testAccCheckTags(&v.Tags, "foo", ""),
   397  					testAccCheckTags(&v.Tags, "bar", "baz"),
   398  				),
   399  			},
   400  		},
   401  	})
   402  }
   403  
   404  func TestAccAWSInstance_privateIP(t *testing.T) {
   405  	var v ec2.Instance
   406  
   407  	testCheckPrivateIP := func() resource.TestCheckFunc {
   408  		return func(*terraform.State) error {
   409  			if *v.PrivateIpAddress != "10.1.1.42" {
   410  				return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress)
   411  			}
   412  
   413  			return nil
   414  		}
   415  	}
   416  
   417  	resource.Test(t, resource.TestCase{
   418  		PreCheck:     func() { testAccPreCheck(t) },
   419  		Providers:    testAccProviders,
   420  		CheckDestroy: testAccCheckInstanceDestroy,
   421  		Steps: []resource.TestStep{
   422  			resource.TestStep{
   423  				Config: testAccInstanceConfigPrivateIP,
   424  				Check: resource.ComposeTestCheckFunc(
   425  					testAccCheckInstanceExists("aws_instance.foo", &v),
   426  					testCheckPrivateIP(),
   427  				),
   428  			},
   429  		},
   430  	})
   431  }
   432  
   433  func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) {
   434  	var v ec2.Instance
   435  
   436  	testCheckPrivateIP := func() resource.TestCheckFunc {
   437  		return func(*terraform.State) error {
   438  			if *v.PrivateIpAddress != "10.1.1.42" {
   439  				return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress)
   440  			}
   441  
   442  			return nil
   443  		}
   444  	}
   445  
   446  	resource.Test(t, resource.TestCase{
   447  		PreCheck:     func() { testAccPreCheck(t) },
   448  		Providers:    testAccProviders,
   449  		CheckDestroy: testAccCheckInstanceDestroy,
   450  		Steps: []resource.TestStep{
   451  			resource.TestStep{
   452  				Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP,
   453  				Check: resource.ComposeTestCheckFunc(
   454  					testAccCheckInstanceExists("aws_instance.foo", &v),
   455  					testCheckPrivateIP(),
   456  				),
   457  			},
   458  		},
   459  	})
   460  }
   461  
   462  // Guard against regression with KeyPairs
   463  // https://github.com/hashicorp/terraform/issues/2302
   464  func TestAccAWSInstance_keyPairCheck(t *testing.T) {
   465  	var v ec2.Instance
   466  
   467  	testCheckKeyPair := func(keyName string) resource.TestCheckFunc {
   468  		return func(*terraform.State) error {
   469  			if v.KeyName == nil {
   470  				return fmt.Errorf("No Key Pair found, expected(%s)", keyName)
   471  			}
   472  			if v.KeyName != nil && *v.KeyName != keyName {
   473  				return fmt.Errorf("Bad key name, expected (%s), got (%s)", keyName, *v.KeyName)
   474  			}
   475  
   476  			return nil
   477  		}
   478  	}
   479  
   480  	resource.Test(t, resource.TestCase{
   481  		PreCheck:     func() { testAccPreCheck(t) },
   482  		Providers:    testAccProviders,
   483  		CheckDestroy: testAccCheckInstanceDestroy,
   484  		Steps: []resource.TestStep{
   485  			resource.TestStep{
   486  				Config: testAccInstanceConfigKeyPair,
   487  				Check: resource.ComposeTestCheckFunc(
   488  					testAccCheckInstanceExists("aws_instance.foo", &v),
   489  					testCheckKeyPair("tmp-key"),
   490  				),
   491  			},
   492  		},
   493  	})
   494  }
   495  
   496  func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) {
   497  	var v ec2.Instance
   498  
   499  	resource.Test(t, resource.TestCase{
   500  		PreCheck:     func() { testAccPreCheck(t) },
   501  		Providers:    testAccProviders,
   502  		CheckDestroy: testAccCheckInstanceDestroy,
   503  		Steps: []resource.TestStep{
   504  			resource.TestStep{
   505  				Config: testAccInstanceConfigRootBlockDeviceMismatch,
   506  				Check: resource.ComposeTestCheckFunc(
   507  					testAccCheckInstanceExists("aws_instance.foo", &v),
   508  					resource.TestCheckResourceAttr(
   509  						"aws_instance.foo", "root_block_device.0.volume_size", "13"),
   510  				),
   511  			},
   512  		},
   513  	})
   514  }
   515  
   516  func testAccCheckInstanceDestroy(s *terraform.State) error {
   517  	return testAccCheckInstanceDestroyWithProvider(s, testAccProvider)
   518  }
   519  
   520  func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
   521  	return func(s *terraform.State) error {
   522  		for _, provider := range *providers {
   523  			if provider.Meta() == nil {
   524  				continue
   525  			}
   526  			if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil {
   527  				return err
   528  			}
   529  		}
   530  		return nil
   531  	}
   532  }
   533  
   534  func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
   535  	conn := provider.Meta().(*AWSClient).ec2conn
   536  
   537  	for _, rs := range s.RootModule().Resources {
   538  		if rs.Type != "aws_instance" {
   539  			continue
   540  		}
   541  
   542  		// Try to find the resource
   543  		resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
   544  			InstanceIds: []*string{aws.String(rs.Primary.ID)},
   545  		})
   546  		if err == nil {
   547  			for _, r := range resp.Reservations {
   548  				for _, i := range r.Instances {
   549  					if i.State != nil && *i.State.Name != "terminated" {
   550  						return fmt.Errorf("Found unterminated instance: %s", i)
   551  					}
   552  				}
   553  			}
   554  		}
   555  
   556  		// Verify the error is what we want
   557  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidInstanceID.NotFound" {
   558  			continue
   559  		}
   560  
   561  		return err
   562  	}
   563  
   564  	return nil
   565  }
   566  
   567  func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc {
   568  	providers := []*schema.Provider{testAccProvider}
   569  	return testAccCheckInstanceExistsWithProviders(n, i, &providers)
   570  }
   571  
   572  func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc {
   573  	return func(s *terraform.State) error {
   574  		rs, ok := s.RootModule().Resources[n]
   575  		if !ok {
   576  			return fmt.Errorf("Not found: %s", n)
   577  		}
   578  
   579  		if rs.Primary.ID == "" {
   580  			return fmt.Errorf("No ID is set")
   581  		}
   582  		for _, provider := range *providers {
   583  			// Ignore if Meta is empty, this can happen for validation providers
   584  			if provider.Meta() == nil {
   585  				continue
   586  			}
   587  
   588  			conn := provider.Meta().(*AWSClient).ec2conn
   589  			resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
   590  				InstanceIds: []*string{aws.String(rs.Primary.ID)},
   591  			})
   592  			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" {
   593  				continue
   594  			}
   595  			if err != nil {
   596  				return err
   597  			}
   598  
   599  			if len(resp.Reservations) > 0 {
   600  				*i = *resp.Reservations[0].Instances[0]
   601  				return nil
   602  			}
   603  		}
   604  
   605  		return fmt.Errorf("Instance not found")
   606  	}
   607  }
   608  
   609  func TestInstanceTenancySchema(t *testing.T) {
   610  	actualSchema := resourceAwsInstance().Schema["tenancy"]
   611  	expectedSchema := &schema.Schema{
   612  		Type:     schema.TypeString,
   613  		Optional: true,
   614  		Computed: true,
   615  		ForceNew: true,
   616  	}
   617  	if !reflect.DeepEqual(actualSchema, expectedSchema) {
   618  		t.Fatalf(
   619  			"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   620  			actualSchema,
   621  			expectedSchema)
   622  	}
   623  }
   624  
   625  const testAccInstanceConfig_pre = `
   626  resource "aws_security_group" "tf_test_foo" {
   627  	name = "tf_test_foo"
   628  	description = "foo"
   629  
   630  	ingress {
   631  		protocol = "icmp"
   632  		from_port = -1
   633  		to_port = -1
   634  		cidr_blocks = ["0.0.0.0/0"]
   635  	}
   636  }
   637  `
   638  
   639  const testAccInstanceConfig = `
   640  resource "aws_security_group" "tf_test_foo" {
   641  	name = "tf_test_foo"
   642  	description = "foo"
   643  
   644  	ingress {
   645  		protocol = "icmp"
   646  		from_port = -1
   647  		to_port = -1
   648  		cidr_blocks = ["0.0.0.0/0"]
   649  	}
   650  }
   651  
   652  resource "aws_instance" "foo" {
   653  	# us-west-2
   654  	ami = "ami-4fccb37f"
   655  	availability_zone = "us-west-2a"
   656  
   657  	instance_type = "m1.small"
   658  	security_groups = ["${aws_security_group.tf_test_foo.name}"]
   659  	user_data = "foo:-with-character's"
   660  }
   661  `
   662  
   663  const testAccInstanceConfigBlockDevices = `
   664  resource "aws_instance" "foo" {
   665  	# us-west-2
   666  	ami = "ami-55a7ea65"
   667  
   668  	# In order to attach an encrypted volume to an instance you need to have an
   669  	# m3.medium or larger. See "Supported Instance Types" in:
   670  	# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html
   671  	instance_type = "m3.medium"
   672  
   673  	root_block_device {
   674  		volume_type = "gp2"
   675  		volume_size = 11
   676  	}
   677  	ebs_block_device {
   678  		device_name = "/dev/sdb"
   679  		volume_size = 9
   680  	}
   681  	ebs_block_device {
   682  		device_name = "/dev/sdc"
   683  		volume_size = 10
   684  		volume_type = "io1"
   685  		iops = 100
   686  	}
   687  
   688  	# Encrypted ebs block device
   689  	ebs_block_device {
   690  		device_name = "/dev/sdd"
   691  		volume_size = 12
   692  		encrypted = true
   693  	}
   694  
   695  	ephemeral_block_device {
   696  		device_name = "/dev/sde"
   697  		virtual_name = "ephemeral0"
   698  	}
   699  }
   700  `
   701  
   702  const testAccInstanceConfigSourceDestEnable = `
   703  resource "aws_vpc" "foo" {
   704  	cidr_block = "10.1.0.0/16"
   705  }
   706  
   707  resource "aws_subnet" "foo" {
   708  	cidr_block = "10.1.1.0/24"
   709  	vpc_id = "${aws_vpc.foo.id}"
   710  }
   711  
   712  resource "aws_instance" "foo" {
   713  	# us-west-2
   714  	ami = "ami-4fccb37f"
   715  	instance_type = "m1.small"
   716  	subnet_id = "${aws_subnet.foo.id}"
   717  }
   718  `
   719  
   720  const testAccInstanceConfigSourceDestDisable = `
   721  resource "aws_vpc" "foo" {
   722  	cidr_block = "10.1.0.0/16"
   723  }
   724  
   725  resource "aws_subnet" "foo" {
   726  	cidr_block = "10.1.1.0/24"
   727  	vpc_id = "${aws_vpc.foo.id}"
   728  }
   729  
   730  resource "aws_instance" "foo" {
   731  	# us-west-2
   732  	ami = "ami-4fccb37f"
   733  	instance_type = "m1.small"
   734  	subnet_id = "${aws_subnet.foo.id}"
   735  	source_dest_check = false
   736  }
   737  `
   738  
   739  func testAccInstanceConfigDisableAPITermination(val bool) string {
   740  	return fmt.Sprintf(`
   741  	resource "aws_vpc" "foo" {
   742  		cidr_block = "10.1.0.0/16"
   743  	}
   744  
   745  	resource "aws_subnet" "foo" {
   746  		cidr_block = "10.1.1.0/24"
   747  		vpc_id = "${aws_vpc.foo.id}"
   748  	}
   749  
   750  	resource "aws_instance" "foo" {
   751  		# us-west-2
   752  		ami = "ami-4fccb37f"
   753  		instance_type = "m1.small"
   754  		subnet_id = "${aws_subnet.foo.id}"
   755  		disable_api_termination = %t
   756  	}
   757  	`, val)
   758  }
   759  
   760  const testAccInstanceConfigVPC = `
   761  resource "aws_vpc" "foo" {
   762  	cidr_block = "10.1.0.0/16"
   763  }
   764  
   765  resource "aws_subnet" "foo" {
   766  	cidr_block = "10.1.1.0/24"
   767  	vpc_id = "${aws_vpc.foo.id}"
   768  }
   769  
   770  resource "aws_instance" "foo" {
   771  	# us-west-2
   772  	ami = "ami-4fccb37f"
   773  	instance_type = "m1.small"
   774  	subnet_id = "${aws_subnet.foo.id}"
   775  	associate_public_ip_address = true
   776  	tenancy = "dedicated"
   777  }
   778  `
   779  
   780  const testAccInstanceConfigMultipleRegions = `
   781  provider "aws" {
   782  	alias = "west"
   783  	region = "us-west-2"
   784  }
   785  
   786  provider "aws" {
   787  	alias = "east"
   788  	region = "us-east-1"
   789  }
   790  
   791  resource "aws_instance" "foo" {
   792  	# us-west-2
   793  	provider = "aws.west"
   794  	ami = "ami-4fccb37f"
   795  	instance_type = "m1.small"
   796  }
   797  
   798  resource "aws_instance" "bar" {
   799  	# us-east-1
   800  	provider = "aws.east"
   801  	ami = "ami-8c6ea9e4"
   802  	instance_type = "m1.small"
   803  }
   804  `
   805  
   806  const testAccCheckInstanceConfigTags = `
   807  resource "aws_instance" "foo" {
   808  	ami = "ami-4fccb37f"
   809  	instance_type = "m1.small"
   810  	tags {
   811  		foo = "bar"
   812  	}
   813  }
   814  `
   815  
   816  const testAccCheckInstanceConfigTagsUpdate = `
   817  resource "aws_instance" "foo" {
   818  	ami = "ami-4fccb37f"
   819  	instance_type = "m1.small"
   820  	tags {
   821  		bar = "baz"
   822  	}
   823  }
   824  `
   825  
   826  const testAccInstanceConfigPrivateIP = `
   827  resource "aws_vpc" "foo" {
   828  	cidr_block = "10.1.0.0/16"
   829  }
   830  
   831  resource "aws_subnet" "foo" {
   832  	cidr_block = "10.1.1.0/24"
   833  	vpc_id = "${aws_vpc.foo.id}"
   834  }
   835  
   836  resource "aws_instance" "foo" {
   837  	ami = "ami-c5eabbf5"
   838  	instance_type = "t2.micro"
   839  	subnet_id = "${aws_subnet.foo.id}"
   840  	private_ip = "10.1.1.42"
   841  }
   842  `
   843  
   844  const testAccInstanceConfigAssociatePublicIPAndPrivateIP = `
   845  resource "aws_vpc" "foo" {
   846  	cidr_block = "10.1.0.0/16"
   847  }
   848  
   849  resource "aws_subnet" "foo" {
   850  	cidr_block = "10.1.1.0/24"
   851  	vpc_id = "${aws_vpc.foo.id}"
   852  }
   853  
   854  resource "aws_instance" "foo" {
   855  	ami = "ami-c5eabbf5"
   856  	instance_type = "t2.micro"
   857  	subnet_id = "${aws_subnet.foo.id}"
   858  	associate_public_ip_address = true
   859  	private_ip = "10.1.1.42"
   860  }
   861  `
   862  
   863  const testAccInstanceNetworkInstanceSecurityGroups = `
   864  resource "aws_internet_gateway" "gw" {
   865    vpc_id = "${aws_vpc.foo.id}"
   866  }
   867  
   868  resource "aws_vpc" "foo" {
   869    cidr_block = "10.1.0.0/16"
   870  	tags {
   871  		Name = "tf-network-test"
   872  	}
   873  }
   874  
   875  resource "aws_security_group" "tf_test_foo" {
   876    name = "tf_test_foo"
   877    description = "foo"
   878    vpc_id="${aws_vpc.foo.id}"
   879  
   880    ingress {
   881      protocol = "icmp"
   882      from_port = -1
   883      to_port = -1
   884      cidr_blocks = ["0.0.0.0/0"]
   885    }
   886  }
   887  
   888  resource "aws_subnet" "foo" {
   889    cidr_block = "10.1.1.0/24"
   890    vpc_id = "${aws_vpc.foo.id}"
   891  }
   892  
   893  resource "aws_instance" "foo_instance" {
   894    ami = "ami-21f78e11"
   895    instance_type = "t1.micro"
   896    security_groups = ["${aws_security_group.tf_test_foo.id}"]
   897    subnet_id = "${aws_subnet.foo.id}"
   898    associate_public_ip_address = true
   899  	depends_on = ["aws_internet_gateway.gw"]
   900  }
   901  
   902  resource "aws_eip" "foo_eip" {
   903    instance = "${aws_instance.foo_instance.id}"
   904    vpc = true
   905  	depends_on = ["aws_internet_gateway.gw"]
   906  }
   907  `
   908  
   909  const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = `
   910  resource "aws_internet_gateway" "gw" {
   911    vpc_id = "${aws_vpc.foo.id}"
   912  }
   913  
   914  resource "aws_vpc" "foo" {
   915    cidr_block = "10.1.0.0/16"
   916  	tags {
   917  		Name = "tf-network-test"
   918  	}
   919  }
   920  
   921  resource "aws_security_group" "tf_test_foo" {
   922    name = "tf_test_foo"
   923    description = "foo"
   924    vpc_id="${aws_vpc.foo.id}"
   925  
   926    ingress {
   927      protocol = "icmp"
   928      from_port = -1
   929      to_port = -1
   930      cidr_blocks = ["0.0.0.0/0"]
   931    }
   932  }
   933  
   934  resource "aws_subnet" "foo" {
   935    cidr_block = "10.1.1.0/24"
   936    vpc_id = "${aws_vpc.foo.id}"
   937  }
   938  
   939  resource "aws_instance" "foo_instance" {
   940    ami = "ami-21f78e11"
   941    instance_type = "t1.micro"
   942    vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"]
   943    subnet_id = "${aws_subnet.foo.id}"
   944  	depends_on = ["aws_internet_gateway.gw"]
   945  }
   946  
   947  resource "aws_eip" "foo_eip" {
   948    instance = "${aws_instance.foo_instance.id}"
   949    vpc = true
   950  	depends_on = ["aws_internet_gateway.gw"]
   951  }
   952  `
   953  
   954  const testAccInstanceConfigKeyPair = `
   955  provider "aws" {
   956  	region = "us-east-1"
   957  }
   958  
   959  resource "aws_key_pair" "debugging" {
   960  	key_name = "tmp-key"
   961  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   962  }
   963  
   964  resource "aws_instance" "foo" {
   965    ami = "ami-408c7f28"
   966    instance_type = "t1.micro"
   967    key_name = "${aws_key_pair.debugging.key_name}"
   968  }
   969  `
   970  
   971  const testAccInstanceConfigRootBlockDeviceMismatch = `
   972  resource "aws_vpc" "foo" {
   973  	cidr_block = "10.1.0.0/16"
   974  }
   975  
   976  resource "aws_subnet" "foo" {
   977  	cidr_block = "10.1.1.0/24"
   978  	vpc_id = "${aws_vpc.foo.id}"
   979  }
   980  
   981  resource "aws_instance" "foo" {
   982  	// This is an AMI with RootDeviceName: "/dev/sda1"; actual root: "/dev/sda"
   983  	ami = "ami-ef5b69df"
   984  	instance_type = "t1.micro"
   985  	subnet_id = "${aws_subnet.foo.id}"
   986  	root_block_device {
   987  		volume_size = 13
   988  	}
   989  }
   990  `