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