2014-07-14 22:28:00 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-12-03 11:28:51 +01:00
|
|
|
"reflect"
|
2014-12-20 20:47:50 +01:00
|
|
|
"testing"
|
2014-07-14 22:28:00 +02:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2014-07-14 22:28:00 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-12-03 11:28:51 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-14 22:28:00 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-06-08 01:04:38 +02:00
|
|
|
func TestAccAWSInstance_basic(t *testing.T) {
|
2014-07-14 22:28:00 +02:00
|
|
|
var v ec2.Instance
|
2015-03-19 19:14:31 +01:00
|
|
|
var vol *ec2.Volume
|
2014-07-14 22:28:00 +02:00
|
|
|
|
2014-07-15 06:56:37 +02:00
|
|
|
testCheck := func(*terraform.State) error {
|
2015-03-05 16:45:39 +01:00
|
|
|
if *v.Placement.AvailabilityZone != "us-west-2a" {
|
|
|
|
return fmt.Errorf("bad availability zone: %#v", *v.Placement.AvailabilityZone)
|
2014-08-01 02:35:31 +02:00
|
|
|
}
|
|
|
|
|
2014-07-15 06:56:37 +02:00
|
|
|
if len(v.SecurityGroups) == 0 {
|
|
|
|
return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
|
|
|
|
}
|
2015-03-05 16:45:39 +01:00
|
|
|
if *v.SecurityGroups[0].GroupName != "tf_test_foo" {
|
2014-07-15 06:56:37 +02:00
|
|
|
return fmt.Errorf("no security groups: %#v", v.SecurityGroups)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2015-03-19 19:14:31 +01:00
|
|
|
// Create a volume to cover #1249
|
|
|
|
resource.TestStep{
|
|
|
|
// Need a resource in this config so the provisioner will be available
|
|
|
|
Config: testAccInstanceConfig_pre,
|
|
|
|
Check: func(*terraform.State) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2015-03-19 19:14:31 +01:00
|
|
|
var err error
|
2015-04-16 19:01:10 +02:00
|
|
|
vol, err = conn.CreateVolume(&ec2.CreateVolumeInput{
|
2015-03-19 19:14:31 +01:00
|
|
|
AvailabilityZone: aws.String("us-west-2a"),
|
2015-04-16 19:01:10 +02:00
|
|
|
Size: aws.Long(int64(5)),
|
2015-03-19 19:14:31 +01:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo", &v),
|
2014-07-15 06:56:37 +02:00
|
|
|
testCheck,
|
2014-07-16 18:01:56 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo",
|
2014-07-17 01:41:01 +02:00
|
|
|
"user_data",
|
2015-03-18 14:57:41 +01:00
|
|
|
"3dc39dda39be1205215e776bad998da361a5955d"),
|
2015-03-19 19:14:31 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo", "ebs_block_device.#", "0"),
|
2014-07-17 01:41:01 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
// We repeat the exact same test so that we can be sure
|
|
|
|
// that the user data hash stuff is working without generating
|
|
|
|
// an incorrect diff.
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo", &v),
|
|
|
|
testCheck,
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo",
|
|
|
|
"user_data",
|
2015-03-18 14:57:41 +01:00
|
|
|
"3dc39dda39be1205215e776bad998da361a5955d"),
|
2015-03-19 19:14:31 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo", "ebs_block_device.#", "0"),
|
2014-07-14 22:28:00 +02:00
|
|
|
),
|
|
|
|
},
|
2015-03-19 19:14:31 +01:00
|
|
|
|
|
|
|
// Clean up volume created above
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfig,
|
|
|
|
Check: func(*terraform.State) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2015-04-16 19:01:10 +02:00
|
|
|
_, err := conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeID: vol.VolumeID})
|
|
|
|
return err
|
2015-03-19 19:14:31 +01:00
|
|
|
},
|
|
|
|
},
|
2014-07-14 22:28:00 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
providers/aws: add root_block_device to aws_instance
AWS provides a single `BlockDeviceMapping` to manage three different
kinds of block devices:
(a) The root volume
(b) Ephemeral storage
(c) Additional EBS volumes
Each of these types has slightly different semantics [1].
(a) The root volume is defined by the AMI; it can only be customized
with `volume_size`, `volume_type`, and `delete_on_termination`.
(b) Ephemeral storage is made available based on instance type [2]. It's
attached automatically if _no_ block device mappings are specified, and
must otherwise be defined with block device mapping entries that contain
only DeviceName set to a device like "/dev/sdX" and VirtualName set to
"ephemeralN".
(c) Additional EBS volumes are controlled by mappings that omit
`virtual_name` and can specify `volume_size`, `volume_type`,
`delete_on_termination`, `snapshot_id`, and `encryption`.
After deciding to ignore root block devices to fix #859, we had users
with configurations that were attempting to manage the root block device chime
in on #913.
Terraform does not have the primitives to be able to properly handle a
single collection of resources that is partially managed and partially
computed, so our strategy here is to break out logical sub-resources for
Terraform and hide the BlockDeviceMapping inside the provider
implementation.
Now (a) is supported by the `root_block_device` sub-resource, and (b)
and (c) are still both merged together under `block_device`, though I
have yet to see ephemeral block devices working properly.
Looking into possibly separating out `ephemeral_block_device` and
`ebs_block_device` sub-resources as well, which seem like the logical
next step. We'll wait until the next big release for this, though, since
it will break backcompat.
[1] http://bit.ly/ec2bdmap
[2] http://bit.ly/instancestorebytype
Fixes #913
Refs #858
2015-02-18 18:45:30 +01:00
|
|
|
func TestAccAWSInstance_blockDevices(t *testing.T) {
|
2014-10-31 21:25:16 +01:00
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
testCheck := func() resource.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
|
|
|
|
|
|
|
// Map out the block devices by name, which should be unique.
|
2015-04-16 19:01:10 +02:00
|
|
|
blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping)
|
2015-03-05 16:45:39 +01:00
|
|
|
for _, blockDevice := range v.BlockDeviceMappings {
|
|
|
|
blockDevices[*blockDevice.DeviceName] = blockDevice
|
2014-10-31 21:25:16 +01:00
|
|
|
}
|
|
|
|
|
providers/aws: add root_block_device to aws_instance
AWS provides a single `BlockDeviceMapping` to manage three different
kinds of block devices:
(a) The root volume
(b) Ephemeral storage
(c) Additional EBS volumes
Each of these types has slightly different semantics [1].
(a) The root volume is defined by the AMI; it can only be customized
with `volume_size`, `volume_type`, and `delete_on_termination`.
(b) Ephemeral storage is made available based on instance type [2]. It's
attached automatically if _no_ block device mappings are specified, and
must otherwise be defined with block device mapping entries that contain
only DeviceName set to a device like "/dev/sdX" and VirtualName set to
"ephemeralN".
(c) Additional EBS volumes are controlled by mappings that omit
`virtual_name` and can specify `volume_size`, `volume_type`,
`delete_on_termination`, `snapshot_id`, and `encryption`.
After deciding to ignore root block devices to fix #859, we had users
with configurations that were attempting to manage the root block device chime
in on #913.
Terraform does not have the primitives to be able to properly handle a
single collection of resources that is partially managed and partially
computed, so our strategy here is to break out logical sub-resources for
Terraform and hide the BlockDeviceMapping inside the provider
implementation.
Now (a) is supported by the `root_block_device` sub-resource, and (b)
and (c) are still both merged together under `block_device`, though I
have yet to see ephemeral block devices working properly.
Looking into possibly separating out `ephemeral_block_device` and
`ebs_block_device` sub-resources as well, which seem like the logical
next step. We'll wait until the next big release for this, though, since
it will break backcompat.
[1] http://bit.ly/ec2bdmap
[2] http://bit.ly/instancestorebytype
Fixes #913
Refs #858
2015-02-18 18:45:30 +01:00
|
|
|
// Check if the root block device exists.
|
|
|
|
if _, ok := blockDevices["/dev/sda1"]; !ok {
|
|
|
|
fmt.Errorf("block device doesn't exist: /dev/sda1")
|
|
|
|
}
|
|
|
|
|
2014-10-31 21:25:16 +01:00
|
|
|
// Check if the secondary block device exists.
|
|
|
|
if _, ok := blockDevices["/dev/sdb"]; !ok {
|
|
|
|
fmt.Errorf("block device doesn't exist: /dev/sdb")
|
|
|
|
}
|
|
|
|
|
2015-03-03 07:07:36 +01:00
|
|
|
// Check if the third block device exists.
|
|
|
|
if _, ok := blockDevices["/dev/sdc"]; !ok {
|
|
|
|
fmt.Errorf("block device doesn't exist: /dev/sdc")
|
|
|
|
}
|
|
|
|
|
2015-04-29 00:07:23 +02:00
|
|
|
// Check if the encrypted block device exists
|
|
|
|
if _, ok := blockDevices["/dev/sdd"]; !ok {
|
|
|
|
fmt.Errorf("block device doesn't exist: /dev/sdd")
|
|
|
|
}
|
|
|
|
|
2014-10-31 21:25:16 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigBlockDevices,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo", &v),
|
providers/aws: add root_block_device to aws_instance
AWS provides a single `BlockDeviceMapping` to manage three different
kinds of block devices:
(a) The root volume
(b) Ephemeral storage
(c) Additional EBS volumes
Each of these types has slightly different semantics [1].
(a) The root volume is defined by the AMI; it can only be customized
with `volume_size`, `volume_type`, and `delete_on_termination`.
(b) Ephemeral storage is made available based on instance type [2]. It's
attached automatically if _no_ block device mappings are specified, and
must otherwise be defined with block device mapping entries that contain
only DeviceName set to a device like "/dev/sdX" and VirtualName set to
"ephemeralN".
(c) Additional EBS volumes are controlled by mappings that omit
`virtual_name` and can specify `volume_size`, `volume_type`,
`delete_on_termination`, `snapshot_id`, and `encryption`.
After deciding to ignore root block devices to fix #859, we had users
with configurations that were attempting to manage the root block device chime
in on #913.
Terraform does not have the primitives to be able to properly handle a
single collection of resources that is partially managed and partially
computed, so our strategy here is to break out logical sub-resources for
Terraform and hide the BlockDeviceMapping inside the provider
implementation.
Now (a) is supported by the `root_block_device` sub-resource, and (b)
and (c) are still both merged together under `block_device`, though I
have yet to see ephemeral block devices working properly.
Looking into possibly separating out `ephemeral_block_device` and
`ebs_block_device` sub-resources as well, which seem like the logical
next step. We'll wait until the next big release for this, though, since
it will break backcompat.
[1] http://bit.ly/ec2bdmap
[2] http://bit.ly/instancestorebytype
Fixes #913
Refs #858
2015-02-18 18:45:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo", "root_block_device.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-08 00:05:00 +02:00
|
|
|
"aws_instance.foo", "root_block_device.0.volume_size", "11"),
|
providers/aws: add root_block_device to aws_instance
AWS provides a single `BlockDeviceMapping` to manage three different
kinds of block devices:
(a) The root volume
(b) Ephemeral storage
(c) Additional EBS volumes
Each of these types has slightly different semantics [1].
(a) The root volume is defined by the AMI; it can only be customized
with `volume_size`, `volume_type`, and `delete_on_termination`.
(b) Ephemeral storage is made available based on instance type [2]. It's
attached automatically if _no_ block device mappings are specified, and
must otherwise be defined with block device mapping entries that contain
only DeviceName set to a device like "/dev/sdX" and VirtualName set to
"ephemeralN".
(c) Additional EBS volumes are controlled by mappings that omit
`virtual_name` and can specify `volume_size`, `volume_type`,
`delete_on_termination`, `snapshot_id`, and `encryption`.
After deciding to ignore root block devices to fix #859, we had users
with configurations that were attempting to manage the root block device chime
in on #913.
Terraform does not have the primitives to be able to properly handle a
single collection of resources that is partially managed and partially
computed, so our strategy here is to break out logical sub-resources for
Terraform and hide the BlockDeviceMapping inside the provider
implementation.
Now (a) is supported by the `root_block_device` sub-resource, and (b)
and (c) are still both merged together under `block_device`, though I
have yet to see ephemeral block devices working properly.
Looking into possibly separating out `ephemeral_block_device` and
`ebs_block_device` sub-resources as well, which seem like the logical
next step. We'll wait until the next big release for this, though, since
it will break backcompat.
[1] http://bit.ly/ec2bdmap
[2] http://bit.ly/instancestorebytype
Fixes #913
Refs #858
2015-02-18 18:45:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-08 00:05:00 +02:00
|
|
|
"aws_instance.foo", "root_block_device.0.volume_type", "gp2"),
|
2015-01-28 12:00:05 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-29 00:07:23 +02:00
|
|
|
"aws_instance.foo", "ebs_block_device.#", "3"),
|
providers/aws: add root_block_device to aws_instance
AWS provides a single `BlockDeviceMapping` to manage three different
kinds of block devices:
(a) The root volume
(b) Ephemeral storage
(c) Additional EBS volumes
Each of these types has slightly different semantics [1].
(a) The root volume is defined by the AMI; it can only be customized
with `volume_size`, `volume_type`, and `delete_on_termination`.
(b) Ephemeral storage is made available based on instance type [2]. It's
attached automatically if _no_ block device mappings are specified, and
must otherwise be defined with block device mapping entries that contain
only DeviceName set to a device like "/dev/sdX" and VirtualName set to
"ephemeralN".
(c) Additional EBS volumes are controlled by mappings that omit
`virtual_name` and can specify `volume_size`, `volume_type`,
`delete_on_termination`, `snapshot_id`, and `encryption`.
After deciding to ignore root block devices to fix #859, we had users
with configurations that were attempting to manage the root block device chime
in on #913.
Terraform does not have the primitives to be able to properly handle a
single collection of resources that is partially managed and partially
computed, so our strategy here is to break out logical sub-resources for
Terraform and hide the BlockDeviceMapping inside the provider
implementation.
Now (a) is supported by the `root_block_device` sub-resource, and (b)
and (c) are still both merged together under `block_device`, though I
have yet to see ephemeral block devices working properly.
Looking into possibly separating out `ephemeral_block_device` and
`ebs_block_device` sub-resources as well, which seem like the logical
next step. We'll wait until the next big release for this, though, since
it will break backcompat.
[1] http://bit.ly/ec2bdmap
[2] http://bit.ly/instancestorebytype
Fixes #913
Refs #858
2015-02-18 18:45:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-08 00:05:00 +02:00
|
|
|
"aws_instance.foo", "ebs_block_device.2576023345.device_name", "/dev/sdb"),
|
providers/aws: add root_block_device to aws_instance
AWS provides a single `BlockDeviceMapping` to manage three different
kinds of block devices:
(a) The root volume
(b) Ephemeral storage
(c) Additional EBS volumes
Each of these types has slightly different semantics [1].
(a) The root volume is defined by the AMI; it can only be customized
with `volume_size`, `volume_type`, and `delete_on_termination`.
(b) Ephemeral storage is made available based on instance type [2]. It's
attached automatically if _no_ block device mappings are specified, and
must otherwise be defined with block device mapping entries that contain
only DeviceName set to a device like "/dev/sdX" and VirtualName set to
"ephemeralN".
(c) Additional EBS volumes are controlled by mappings that omit
`virtual_name` and can specify `volume_size`, `volume_type`,
`delete_on_termination`, `snapshot_id`, and `encryption`.
After deciding to ignore root block devices to fix #859, we had users
with configurations that were attempting to manage the root block device chime
in on #913.
Terraform does not have the primitives to be able to properly handle a
single collection of resources that is partially managed and partially
computed, so our strategy here is to break out logical sub-resources for
Terraform and hide the BlockDeviceMapping inside the provider
implementation.
Now (a) is supported by the `root_block_device` sub-resource, and (b)
and (c) are still both merged together under `block_device`, though I
have yet to see ephemeral block devices working properly.
Looking into possibly separating out `ephemeral_block_device` and
`ebs_block_device` sub-resources as well, which seem like the logical
next step. We'll wait until the next big release for this, though, since
it will break backcompat.
[1] http://bit.ly/ec2bdmap
[2] http://bit.ly/instancestorebytype
Fixes #913
Refs #858
2015-02-18 18:45:30 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-08 00:05:00 +02:00
|
|
|
"aws_instance.foo", "ebs_block_device.2576023345.volume_size", "9"),
|
2015-03-03 07:07:36 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-08 00:05:00 +02:00
|
|
|
"aws_instance.foo", "ebs_block_device.2576023345.volume_type", "standard"),
|
2015-03-03 07:07:36 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-08 00:05:00 +02:00
|
|
|
"aws_instance.foo", "ebs_block_device.2554893574.device_name", "/dev/sdc"),
|
2015-03-03 07:07:36 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-08 00:05:00 +02:00
|
|
|
"aws_instance.foo", "ebs_block_device.2554893574.volume_size", "10"),
|
2015-03-03 07:07:36 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-08 00:05:00 +02:00
|
|
|
"aws_instance.foo", "ebs_block_device.2554893574.volume_type", "io1"),
|
2015-03-03 07:07:36 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-08 00:05:00 +02:00
|
|
|
"aws_instance.foo", "ebs_block_device.2554893574.iops", "100"),
|
2015-04-29 00:07:23 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo", "ebs_block_device.2634515331.device_name", "/dev/sdd"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo", "ebs_block_device.2634515331.encrypted", "true"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo", "ebs_block_device.2634515331.volume_size", "12"),
|
2015-02-24 18:00:22 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo", "ephemeral_block_device.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-19 17:40:48 +01:00
|
|
|
"aws_instance.foo", "ephemeral_block_device.1692014856.device_name", "/dev/sde"),
|
2015-02-24 18:00:22 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-19 17:40:48 +01:00
|
|
|
"aws_instance.foo", "ephemeral_block_device.1692014856.virtual_name", "ephemeral0"),
|
2014-10-31 21:25:16 +01:00
|
|
|
testCheck(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-14 23:16:59 +02:00
|
|
|
func TestAccAWSInstance_sourceDestCheck(t *testing.T) {
|
|
|
|
var v ec2.Instance
|
|
|
|
|
2014-07-15 02:38:39 +02:00
|
|
|
testCheck := func(enabled bool) resource.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
2015-03-05 16:45:39 +01:00
|
|
|
if *v.SourceDestCheck != enabled {
|
|
|
|
return fmt.Errorf("bad source_dest_check: %#v", *v.SourceDestCheck)
|
2014-07-15 02:38:39 +02:00
|
|
|
}
|
2014-07-14 23:16:59 +02:00
|
|
|
|
2014-07-15 02:38:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-14 23:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2015-02-21 21:26:46 +01:00
|
|
|
Config: testAccInstanceConfigSourceDestDisable,
|
2014-07-14 23:16:59 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-02-21 21:26:46 +01:00
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
|
|
|
testCheck(false),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigSourceDestEnable,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
2014-07-15 02:38:39 +02:00
|
|
|
testCheck(true),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigSourceDestDisable,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-02-21 21:26:46 +01:00
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
2014-07-15 02:38:39 +02:00
|
|
|
testCheck(false),
|
2014-07-14 23:16:59 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:18:05 +02:00
|
|
|
func TestAccAWSInstance_disableApiTermination(t *testing.T) {
|
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
checkDisableApiTermination := func(expected bool) resource.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
r, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{
|
|
|
|
InstanceID: v.InstanceID,
|
|
|
|
Attribute: aws.String("disableApiTermination"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
got := *r.DisableAPITermination.Value
|
|
|
|
if got != expected {
|
|
|
|
return fmt.Errorf("expected: %t, got: %t", expected, got)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigDisableAPITermination(true),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
|
|
|
checkDisableApiTermination(true),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigDisableAPITermination(false),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
|
|
|
checkDisableApiTermination(false),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:46:32 +02:00
|
|
|
func TestAccAWSInstance_vpc(t *testing.T) {
|
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigVPC,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo", &v),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:36:53 +01:00
|
|
|
func TestAccAWSInstance_multipleRegions(t *testing.T) {
|
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
// record the initialized providers so that we can use them to
|
|
|
|
// check for the instances in each region
|
|
|
|
var providers []*schema.Provider
|
|
|
|
providerFactories := map[string]terraform.ResourceProviderFactory{
|
|
|
|
"aws": func() (terraform.ResourceProvider, error) {
|
|
|
|
p := Provider()
|
|
|
|
providers = append(providers, p.(*schema.Provider))
|
|
|
|
return p, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
ProviderFactories: providerFactories,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroyWithProviders(&providers),
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigMultipleRegions,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExistsWithProviders(
|
|
|
|
"aws_instance.foo", &v, &providers),
|
|
|
|
testAccCheckInstanceExistsWithProviders(
|
|
|
|
"aws_instance.bar", &v, &providers),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-09 19:33:20 +02:00
|
|
|
func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) {
|
2015-03-12 20:26:10 +01:00
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceNetworkInstanceSecurityGroups,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo_instance", &v),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:17:21 +02:00
|
|
|
func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) {
|
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists(
|
|
|
|
"aws_instance.foo_instance", &v),
|
2015-04-22 00:07:30 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo_instance", "security_groups.#", "0"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_instance.foo_instance", "vpc_security_group_ids.#", "1"),
|
2015-04-15 19:17:21 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-05 16:45:39 +01:00
|
|
|
func TestAccAWSInstance_tags(t *testing.T) {
|
2014-10-13 22:55:59 +02:00
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckInstanceConfigTags,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&v.Tags, "foo", "bar"),
|
2015-02-04 02:48:25 +01:00
|
|
|
// Guard against regression of https://github.com/hashicorp/terraform/issues/914
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&v.Tags, "#", ""),
|
2014-10-13 22:55:59 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckInstanceConfigTagsUpdate,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&v.Tags, "foo", ""),
|
|
|
|
testAccCheckTags(&v.Tags, "bar", "baz"),
|
2014-10-13 22:55:59 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-05 16:45:39 +01:00
|
|
|
func TestAccAWSInstance_privateIP(t *testing.T) {
|
2014-12-20 20:47:50 +01:00
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
testCheckPrivateIP := func() resource.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
2015-03-05 16:45:39 +01:00
|
|
|
if *v.PrivateIPAddress != "10.1.1.42" {
|
|
|
|
return fmt.Errorf("bad private IP: %s", *v.PrivateIPAddress)
|
2014-12-20 20:47:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigPrivateIP,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
|
|
|
testCheckPrivateIP(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-05 16:45:39 +01:00
|
|
|
func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) {
|
2014-12-20 20:47:50 +01:00
|
|
|
var v ec2.Instance
|
|
|
|
|
|
|
|
testCheckPrivateIP := func() resource.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
2015-03-05 16:45:39 +01:00
|
|
|
if *v.PrivateIPAddress != "10.1.1.42" {
|
|
|
|
return fmt.Errorf("bad private IP: %s", *v.PrivateIPAddress)
|
2014-12-20 20:47:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInstanceExists("aws_instance.foo", &v),
|
|
|
|
testCheckPrivateIP(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
func testAccCheckInstanceDestroy(s *terraform.State) error {
|
2015-03-23 23:36:53 +01:00
|
|
|
return testAccCheckInstanceDestroyWithProvider(s, testAccProvider)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
for _, provider := range *providers {
|
|
|
|
if provider.Meta() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
|
|
|
|
conn := provider.Meta().(*AWSClient).ec2conn
|
2014-07-14 22:28:00 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-14 22:28:00 +02:00
|
|
|
if rs.Type != "aws_instance" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the resource
|
2015-05-20 13:21:23 +02:00
|
|
|
var err error
|
2015-04-16 19:01:10 +02:00
|
|
|
resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
|
|
|
|
InstanceIDs: []*string{aws.String(rs.Primary.ID)},
|
2015-03-05 16:45:39 +01:00
|
|
|
})
|
2014-07-14 22:28:00 +02:00
|
|
|
if err == nil {
|
|
|
|
if len(resp.Reservations) > 0 {
|
|
|
|
return fmt.Errorf("still exist.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error is what we want
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
2014-07-14 22:28:00 +02:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err.Code() != "InvalidInstanceID.NotFound" {
|
2014-07-14 22:28:00 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc {
|
2015-03-23 23:36:53 +01:00
|
|
|
providers := []*schema.Provider{testAccProvider}
|
|
|
|
return testAccCheckInstanceExistsWithProviders(n, i, &providers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc {
|
2014-07-14 22:28:00 +02:00
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-14 22:28:00 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-14 22:28:00 +02:00
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
2015-03-23 23:36:53 +01:00
|
|
|
for _, provider := range *providers {
|
|
|
|
conn := provider.Meta().(*AWSClient).ec2conn
|
|
|
|
resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
|
|
|
|
InstanceIDs: []*string{aws.String(rs.Primary.ID)},
|
|
|
|
})
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" {
|
2015-03-23 23:36:53 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-14 22:28:00 +02:00
|
|
|
|
2015-03-23 23:36:53 +01:00
|
|
|
if len(resp.Reservations) > 0 {
|
|
|
|
*i = *resp.Reservations[0].Instances[0]
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-14 22:28:00 +02:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:36:53 +01:00
|
|
|
return fmt.Errorf("Instance not found")
|
2014-07-14 22:28:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 11:28:51 +01:00
|
|
|
func TestInstanceTenancySchema(t *testing.T) {
|
|
|
|
actualSchema := resourceAwsInstance().Schema["tenancy"]
|
|
|
|
expectedSchema := &schema.Schema{
|
2014-12-20 20:47:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actualSchema, expectedSchema) {
|
2014-12-03 11:28:51 +01:00
|
|
|
t.Fatalf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
|
|
|
actualSchema,
|
|
|
|
expectedSchema)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 19:14:31 +01:00
|
|
|
const testAccInstanceConfig_pre = `
|
|
|
|
resource "aws_security_group" "tf_test_foo" {
|
|
|
|
name = "tf_test_foo"
|
|
|
|
description = "foo"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "icmp"
|
|
|
|
from_port = -1
|
|
|
|
to_port = -1
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
const testAccInstanceConfig = `
|
2014-07-15 06:56:37 +02:00
|
|
|
resource "aws_security_group" "tf_test_foo" {
|
|
|
|
name = "tf_test_foo"
|
|
|
|
description = "foo"
|
2014-07-16 18:01:56 +02:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "icmp"
|
|
|
|
from_port = -1
|
|
|
|
to_port = -1
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
2014-07-15 06:56:37 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-4fccb37f"
|
2014-08-01 02:35:31 +02:00
|
|
|
availability_zone = "us-west-2a"
|
|
|
|
|
2014-07-14 22:28:00 +02:00
|
|
|
instance_type = "m1.small"
|
2014-07-15 06:56:37 +02:00
|
|
|
security_groups = ["${aws_security_group.tf_test_foo.name}"]
|
2015-03-13 16:54:00 +01:00
|
|
|
user_data = "foo:-with-character's"
|
2014-07-14 22:28:00 +02:00
|
|
|
}
|
|
|
|
`
|
2014-07-14 22:46:32 +02:00
|
|
|
|
2014-10-31 21:25:16 +01:00
|
|
|
const testAccInstanceConfigBlockDevices = `
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-55a7ea65"
|
2015-04-29 00:19:31 +02:00
|
|
|
|
|
|
|
# In order to attach an encrypted volume to an instance you need to have an
|
|
|
|
# m3.medium or larger. See "Supported Instance Types" in:
|
|
|
|
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html
|
2015-04-29 00:07:23 +02:00
|
|
|
instance_type = "m3.medium"
|
2015-02-24 18:00:22 +01:00
|
|
|
|
providers/aws: add root_block_device to aws_instance
AWS provides a single `BlockDeviceMapping` to manage three different
kinds of block devices:
(a) The root volume
(b) Ephemeral storage
(c) Additional EBS volumes
Each of these types has slightly different semantics [1].
(a) The root volume is defined by the AMI; it can only be customized
with `volume_size`, `volume_type`, and `delete_on_termination`.
(b) Ephemeral storage is made available based on instance type [2]. It's
attached automatically if _no_ block device mappings are specified, and
must otherwise be defined with block device mapping entries that contain
only DeviceName set to a device like "/dev/sdX" and VirtualName set to
"ephemeralN".
(c) Additional EBS volumes are controlled by mappings that omit
`virtual_name` and can specify `volume_size`, `volume_type`,
`delete_on_termination`, `snapshot_id`, and `encryption`.
After deciding to ignore root block devices to fix #859, we had users
with configurations that were attempting to manage the root block device chime
in on #913.
Terraform does not have the primitives to be able to properly handle a
single collection of resources that is partially managed and partially
computed, so our strategy here is to break out logical sub-resources for
Terraform and hide the BlockDeviceMapping inside the provider
implementation.
Now (a) is supported by the `root_block_device` sub-resource, and (b)
and (c) are still both merged together under `block_device`, though I
have yet to see ephemeral block devices working properly.
Looking into possibly separating out `ephemeral_block_device` and
`ebs_block_device` sub-resources as well, which seem like the logical
next step. We'll wait until the next big release for this, though, since
it will break backcompat.
[1] http://bit.ly/ec2bdmap
[2] http://bit.ly/instancestorebytype
Fixes #913
Refs #858
2015-02-18 18:45:30 +01:00
|
|
|
root_block_device {
|
|
|
|
volume_type = "gp2"
|
|
|
|
volume_size = 11
|
|
|
|
}
|
2015-02-24 18:00:22 +01:00
|
|
|
ebs_block_device {
|
2015-01-28 12:16:04 +01:00
|
|
|
device_name = "/dev/sdb"
|
providers/aws: add root_block_device to aws_instance
AWS provides a single `BlockDeviceMapping` to manage three different
kinds of block devices:
(a) The root volume
(b) Ephemeral storage
(c) Additional EBS volumes
Each of these types has slightly different semantics [1].
(a) The root volume is defined by the AMI; it can only be customized
with `volume_size`, `volume_type`, and `delete_on_termination`.
(b) Ephemeral storage is made available based on instance type [2]. It's
attached automatically if _no_ block device mappings are specified, and
must otherwise be defined with block device mapping entries that contain
only DeviceName set to a device like "/dev/sdX" and VirtualName set to
"ephemeralN".
(c) Additional EBS volumes are controlled by mappings that omit
`virtual_name` and can specify `volume_size`, `volume_type`,
`delete_on_termination`, `snapshot_id`, and `encryption`.
After deciding to ignore root block devices to fix #859, we had users
with configurations that were attempting to manage the root block device chime
in on #913.
Terraform does not have the primitives to be able to properly handle a
single collection of resources that is partially managed and partially
computed, so our strategy here is to break out logical sub-resources for
Terraform and hide the BlockDeviceMapping inside the provider
implementation.
Now (a) is supported by the `root_block_device` sub-resource, and (b)
and (c) are still both merged together under `block_device`, though I
have yet to see ephemeral block devices working properly.
Looking into possibly separating out `ephemeral_block_device` and
`ebs_block_device` sub-resources as well, which seem like the logical
next step. We'll wait until the next big release for this, though, since
it will break backcompat.
[1] http://bit.ly/ec2bdmap
[2] http://bit.ly/instancestorebytype
Fixes #913
Refs #858
2015-02-18 18:45:30 +01:00
|
|
|
volume_size = 9
|
2014-10-31 21:25:16 +01:00
|
|
|
}
|
2015-02-24 18:00:22 +01:00
|
|
|
ebs_block_device {
|
2015-03-03 07:07:36 +01:00
|
|
|
device_name = "/dev/sdc"
|
|
|
|
volume_size = 10
|
|
|
|
volume_type = "io1"
|
|
|
|
iops = 100
|
|
|
|
}
|
2015-04-29 00:07:23 +02:00
|
|
|
|
|
|
|
# Encrypted ebs block device
|
|
|
|
ebs_block_device {
|
|
|
|
device_name = "/dev/sdd"
|
|
|
|
volume_size = 12
|
|
|
|
encrypted = true
|
|
|
|
}
|
|
|
|
|
2015-02-24 18:00:22 +01:00
|
|
|
ephemeral_block_device {
|
|
|
|
device_name = "/dev/sde"
|
|
|
|
virtual_name = "ephemeral0"
|
|
|
|
}
|
2014-10-31 21:25:16 +01:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2015-02-21 21:26:46 +01:00
|
|
|
const testAccInstanceConfigSourceDestEnable = `
|
2014-07-14 23:16:59 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-07-15 02:38:39 +02:00
|
|
|
const testAccInstanceConfigSourceDestDisable = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
source_dest_check = false
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2015-05-15 21:18:05 +02:00
|
|
|
func testAccInstanceConfigDisableAPITermination(val bool) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
disable_api_termination = %t
|
|
|
|
}
|
|
|
|
`, val)
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:46:32 +02:00
|
|
|
const testAccInstanceConfigVPC = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
2014-07-29 14:06:53 +02:00
|
|
|
associate_public_ip_address = true
|
2014-11-04 12:08:30 +01:00
|
|
|
tenancy = "dedicated"
|
2014-07-14 22:46:32 +02:00
|
|
|
}
|
|
|
|
`
|
2014-10-13 22:55:59 +02:00
|
|
|
|
2015-03-23 23:36:53 +01:00
|
|
|
const testAccInstanceConfigMultipleRegions = `
|
|
|
|
provider "aws" {
|
|
|
|
alias = "west"
|
|
|
|
region = "us-west-2"
|
|
|
|
}
|
|
|
|
|
|
|
|
provider "aws" {
|
|
|
|
alias = "east"
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
provider = "aws.west"
|
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "bar" {
|
|
|
|
# us-east-1
|
|
|
|
provider = "aws.east"
|
|
|
|
ami = "ami-8c6ea9e4"
|
|
|
|
instance_type = "m1.small"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-10-13 22:55:59 +02:00
|
|
|
const testAccCheckInstanceConfigTags = `
|
|
|
|
resource "aws_instance" "foo" {
|
2014-11-24 21:22:18 +01:00
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
2014-10-13 22:55:59 +02:00
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccCheckInstanceConfigTagsUpdate = `
|
2014-10-14 21:15:46 +02:00
|
|
|
resource "aws_instance" "foo" {
|
2014-11-24 21:22:18 +01:00
|
|
|
ami = "ami-4fccb37f"
|
|
|
|
instance_type = "m1.small"
|
2014-10-13 22:55:59 +02:00
|
|
|
tags {
|
|
|
|
bar = "baz"
|
|
|
|
}
|
|
|
|
}
|
2014-10-14 21:20:39 +02:00
|
|
|
`
|
2014-12-20 20:47:50 +01:00
|
|
|
|
|
|
|
const testAccInstanceConfigPrivateIP = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
ami = "ami-c5eabbf5"
|
|
|
|
instance_type = "t2.micro"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
private_ip = "10.1.1.42"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccInstanceConfigAssociatePublicIPAndPrivateIP = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
ami = "ami-c5eabbf5"
|
|
|
|
instance_type = "t2.micro"
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
associate_public_ip_address = true
|
|
|
|
private_ip = "10.1.1.42"
|
|
|
|
}
|
|
|
|
`
|
2015-03-12 20:26:10 +01:00
|
|
|
|
|
|
|
const testAccInstanceNetworkInstanceSecurityGroups = `
|
|
|
|
resource "aws_internet_gateway" "gw" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
2015-03-12 21:01:24 +01:00
|
|
|
tags {
|
|
|
|
Name = "tf-network-test"
|
|
|
|
}
|
2015-03-12 20:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "tf_test_foo" {
|
|
|
|
name = "tf_test_foo"
|
|
|
|
description = "foo"
|
|
|
|
vpc_id="${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "icmp"
|
|
|
|
from_port = -1
|
|
|
|
to_port = -1
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo_instance" {
|
|
|
|
ami = "ami-21f78e11"
|
|
|
|
instance_type = "t1.micro"
|
|
|
|
security_groups = ["${aws_security_group.tf_test_foo.id}"]
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
associate_public_ip_address = true
|
2015-03-13 20:58:05 +01:00
|
|
|
depends_on = ["aws_internet_gateway.gw"]
|
2015-03-12 20:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_eip" "foo_eip" {
|
|
|
|
instance = "${aws_instance.foo_instance.id}"
|
|
|
|
vpc = true
|
2015-03-13 20:58:05 +01:00
|
|
|
depends_on = ["aws_internet_gateway.gw"]
|
2015-03-12 20:26:10 +01:00
|
|
|
}
|
|
|
|
`
|
2015-04-15 19:17:21 +02:00
|
|
|
|
|
|
|
const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = `
|
|
|
|
resource "aws_internet_gateway" "gw" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "tf-network-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "tf_test_foo" {
|
|
|
|
name = "tf_test_foo"
|
|
|
|
description = "foo"
|
|
|
|
vpc_id="${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "icmp"
|
|
|
|
from_port = -1
|
|
|
|
to_port = -1
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "foo" {
|
|
|
|
cidr_block = "10.1.1.0/24"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo_instance" {
|
|
|
|
ami = "ami-21f78e11"
|
|
|
|
instance_type = "t1.micro"
|
|
|
|
vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"]
|
|
|
|
subnet_id = "${aws_subnet.foo.id}"
|
|
|
|
depends_on = ["aws_internet_gateway.gw"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_eip" "foo_eip" {
|
|
|
|
instance = "${aws_instance.foo_instance.id}"
|
|
|
|
vpc = true
|
|
|
|
depends_on = ["aws_internet_gateway.gw"]
|
|
|
|
}
|
|
|
|
`
|