diff --git a/builtin/providers/aws/resource_aws_launch_configuration.go b/builtin/providers/aws/resource_aws_launch_configuration.go index 84edd9fd4f..854c0feb91 100644 --- a/builtin/providers/aws/resource_aws_launch_configuration.go +++ b/builtin/providers/aws/resource_aws_launch_configuration.go @@ -1,6 +1,7 @@ package aws import ( + "bytes" "crypto/sha1" "encoding/base64" "encoding/hex" @@ -10,6 +11,7 @@ import ( "github.com/hashicorp/aws-sdk-go/aws" "github.com/hashicorp/aws-sdk-go/gen/autoscaling" + "github.com/hashicorp/aws-sdk-go/gen/ec2" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" @@ -90,27 +92,197 @@ func resourceAwsLaunchConfiguration() *schema.Resource { Optional: true, ForceNew: true, }, + + "ebs_optimized": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "placement_tenancy": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "ebs_block_device": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "delete_on_termination": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: true, + ForceNew: true, + }, + + "device_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "iops": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Computed: true, + ForceNew: true, + }, + + "snapshot_id": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + + "volume_size": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Computed: true, + ForceNew: true, + }, + + "volume_type": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + }, + Set: func(v interface{}) int { + var buf bytes.Buffer + m := v.(map[string]interface{}) + buf.WriteString(fmt.Sprintf("%t-", m["delete_on_termination"].(bool))) + buf.WriteString(fmt.Sprintf("%s-", m["device_name"].(string))) + // NOTE: Not considering IOPS in hash; when using gp2, IOPS can come + // back set to something like "33", which throws off the set + // calculation and generates an unresolvable diff. + // buf.WriteString(fmt.Sprintf("%d-", m["iops"].(int))) + buf.WriteString(fmt.Sprintf("%s-", m["snapshot_id"].(string))) + buf.WriteString(fmt.Sprintf("%d-", m["volume_size"].(int))) + buf.WriteString(fmt.Sprintf("%s-", m["volume_type"].(string))) + return hashcode.String(buf.String()) + }, + }, + + "ephemeral_block_device": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Computed: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "device_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "virtual_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + }, + }, + Set: func(v interface{}) int { + var buf bytes.Buffer + m := v.(map[string]interface{}) + buf.WriteString(fmt.Sprintf("%s-", m["device_name"].(string))) + buf.WriteString(fmt.Sprintf("%s-", m["virtual_name"].(string))) + return hashcode.String(buf.String()) + }, + }, + + "root_block_device": &schema.Schema{ + // TODO: This is a set because we don't support singleton + // sub-resources today. We'll enforce that the set only ever has + // length zero or one below. When TF gains support for + // sub-resources this can be converted. + Type: schema.TypeSet, + Optional: true, + Computed: true, + Elem: &schema.Resource{ + // "You can only modify the volume size, volume type, and Delete on + // Termination flag on the block device mapping entry for the root + // device volume." - bit.ly/ec2bdmap + Schema: map[string]*schema.Schema{ + "delete_on_termination": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: true, + ForceNew: true, + }, + + "iops": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Computed: true, + ForceNew: true, + }, + + "volume_size": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Computed: true, + ForceNew: true, + }, + + "volume_type": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + }, + Set: func(v interface{}) int { + var buf bytes.Buffer + m := v.(map[string]interface{}) + buf.WriteString(fmt.Sprintf("%t-", m["delete_on_termination"].(bool))) + // See the NOTE in "ebs_block_device" for why we skip iops here. + // buf.WriteString(fmt.Sprintf("%d-", m["iops"].(int))) + buf.WriteString(fmt.Sprintf("%d-", m["volume_size"].(int))) + buf.WriteString(fmt.Sprintf("%s-", m["volume_type"].(string))) + return hashcode.String(buf.String()) + }, + }, }, } } func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface{}) error { autoscalingconn := meta.(*AWSClient).autoscalingconn + ec2conn := meta.(*AWSClient).ec2conn - var createLaunchConfigurationOpts autoscaling.CreateLaunchConfigurationType - createLaunchConfigurationOpts.LaunchConfigurationName = aws.String(d.Get("name").(string)) - createLaunchConfigurationOpts.ImageID = aws.String(d.Get("image_id").(string)) - createLaunchConfigurationOpts.InstanceType = aws.String(d.Get("instance_type").(string)) + createLaunchConfigurationOpts := autoscaling.CreateLaunchConfigurationType{ + LaunchConfigurationName: aws.String(d.Get("name").(string)), + ImageID: aws.String(d.Get("image_id").(string)), + InstanceType: aws.String(d.Get("instance_type").(string)), + EBSOptimized: aws.Boolean(d.Get("ebs_optimized").(bool)), + } if v, ok := d.GetOk("user_data"); ok { - createLaunchConfigurationOpts.UserData = aws.String(base64.StdEncoding.EncodeToString([]byte(v.(string)))) - } - if v, ok := d.GetOk("associate_public_ip_address"); ok { - createLaunchConfigurationOpts.AssociatePublicIPAddress = aws.Boolean(v.(bool)) + userData := base64.StdEncoding.EncodeToString([]byte(v.(string))) + createLaunchConfigurationOpts.UserData = aws.String(userData) } + if v, ok := d.GetOk("iam_instance_profile"); ok { createLaunchConfigurationOpts.IAMInstanceProfile = aws.String(v.(string)) } + + if v, ok := d.GetOk("placement_tenancy"); ok { + createLaunchConfigurationOpts.PlacementTenancy = aws.String(v.(string)) + } + + if v := d.Get("associate_public_ip_address"); v != nil { + createLaunchConfigurationOpts.AssociatePublicIPAddress = aws.Boolean(v.(bool)) + } else { + createLaunchConfigurationOpts.AssociatePublicIPAddress = aws.Boolean(false) + } + if v, ok := d.GetOk("key_name"); ok { createLaunchConfigurationOpts.KeyName = aws.String(v.(string)) } @@ -120,7 +292,90 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface if v, ok := d.GetOk("security_groups"); ok { createLaunchConfigurationOpts.SecurityGroups = expandStringList( - v.(*schema.Set).List()) + v.(*schema.Set).List(), + ) + } + + var blockDevices []autoscaling.BlockDeviceMapping + + if v, ok := d.GetOk("ebs_block_device"); ok { + vL := v.(*schema.Set).List() + for _, v := range vL { + bd := v.(map[string]interface{}) + ebs := &autoscaling.EBS{ + DeleteOnTermination: aws.Boolean(bd["delete_on_termination"].(bool)), + } + + if v, ok := bd["snapshot_id"].(string); ok && v != "" { + ebs.SnapshotID = aws.String(v) + } + + if v, ok := bd["volume_size"].(int); ok && v != 0 { + ebs.VolumeSize = aws.Integer(v) + } + + if v, ok := bd["volume_type"].(string); ok && v != "" { + ebs.VolumeType = aws.String(v) + } + + if v, ok := bd["iops"].(int); ok && v > 0 { + ebs.IOPS = aws.Integer(v) + } + + blockDevices = append(blockDevices, autoscaling.BlockDeviceMapping{ + DeviceName: aws.String(bd["device_name"].(string)), + EBS: ebs, + }) + } + } + + if v, ok := d.GetOk("ephemeral_block_device"); ok { + vL := v.(*schema.Set).List() + for _, v := range vL { + bd := v.(map[string]interface{}) + blockDevices = append(blockDevices, autoscaling.BlockDeviceMapping{ + DeviceName: aws.String(bd["device_name"].(string)), + VirtualName: aws.String(bd["virtual_name"].(string)), + }) + } + } + + if v, ok := d.GetOk("root_block_device"); ok { + vL := v.(*schema.Set).List() + if len(vL) > 1 { + return fmt.Errorf("Cannot specify more than one root_block_device.") + } + for _, v := range vL { + bd := v.(map[string]interface{}) + ebs := &autoscaling.EBS{ + DeleteOnTermination: aws.Boolean(bd["delete_on_termination"].(bool)), + } + + if v, ok := bd["volume_size"].(int); ok && v != 0 { + ebs.VolumeSize = aws.Integer(v) + } + + if v, ok := bd["volume_type"].(string); ok && v != "" { + ebs.VolumeType = aws.String(v) + } + + if v, ok := bd["iops"].(int); ok && v > 0 { + ebs.IOPS = aws.Integer(v) + } + + if dn, err := fetchRootDeviceName(d.Get("image_id").(string), ec2conn); err == nil { + blockDevices = append(blockDevices, autoscaling.BlockDeviceMapping{ + DeviceName: dn, + EBS: ebs, + }) + } else { + return err + } + } + } + + if len(blockDevices) > 0 { + createLaunchConfigurationOpts.BlockDeviceMappings = blockDevices } if v, ok := d.GetOk("name"); ok { @@ -151,6 +406,7 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}) error { autoscalingconn := meta.(*AWSClient).autoscalingconn + ec2conn := meta.(*AWSClient).ec2conn describeOpts := autoscaling.LaunchConfigurationNamesType{ LaunchConfigurationNames: []string{d.Id()}, @@ -197,6 +453,11 @@ func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{} } else { d.Set("security_groups", nil) } + + if err := readLCBlockDevices(d, &lc, ec2conn); err != nil { + return err + } + return nil } @@ -217,3 +478,62 @@ func resourceAwsLaunchConfigurationDelete(d *schema.ResourceData, meta interface return nil } + +func readLCBlockDevices(d *schema.ResourceData, lc *autoscaling.LaunchConfiguration, ec2conn *ec2.EC2) error { + ibds, err := readBlockDevicesFromLaunchConfiguration(d, lc, ec2conn) + if err != nil { + return err + } + + if err := d.Set("ebs_block_device", ibds["ebs"]); err != nil { + return err + } + if ibds["root"] != nil { + if err := d.Set("root_block_device", []interface{}{ibds["root"]}); err != nil { + return err + } + } + + return nil +} + +func readBlockDevicesFromLaunchConfiguration(d *schema.ResourceData, lc *autoscaling.LaunchConfiguration, ec2conn *ec2.EC2) ( + map[string]interface{}, error) { + blockDevices := make(map[string]interface{}) + blockDevices["ebs"] = make([]map[string]interface{}, 0) + blockDevices["root"] = nil + if len(lc.BlockDeviceMappings) == 0 { + return nil, nil + } + rootDeviceName, err := fetchRootDeviceName(d.Get("image_id").(string), ec2conn) + if err == nil { + return nil, err + } + for _, bdm := range lc.BlockDeviceMappings { + bd := make(map[string]interface{}) + if bdm.EBS != nil && bdm.EBS.DeleteOnTermination != nil { + bd["delete_on_termination"] = *bdm.EBS.DeleteOnTermination + } + if bdm.EBS != nil && bdm.EBS.VolumeSize != nil { + bd["volume_size"] = bdm.EBS.VolumeSize + } + if bdm.EBS != nil && bdm.EBS.VolumeType != nil { + bd["volume_type"] = *bdm.EBS.VolumeType + } + if bdm.EBS != nil && bdm.EBS.IOPS != nil { + bd["iops"] = *bdm.EBS.IOPS + } + if bdm.DeviceName != nil && bdm.DeviceName == rootDeviceName { + blockDevices["root"] = bd + } else { + if bdm.DeviceName != nil { + bd["device_name"] = *bdm.DeviceName + } + if bdm.EBS != nil && bdm.EBS.SnapshotID != nil { + bd["snapshot_id"] = *bdm.EBS.SnapshotID + } + blockDevices["ebs"] = append(blockDevices["ebs"].([]map[string]interface{}), bd) + } + } + return blockDevices, nil +} diff --git a/builtin/providers/aws/resource_aws_launch_configuration_test.go b/builtin/providers/aws/resource_aws_launch_configuration_test.go index eb557f08e6..f300ad258d 100644 --- a/builtin/providers/aws/resource_aws_launch_configuration_test.go +++ b/builtin/providers/aws/resource_aws_launch_configuration_test.go @@ -2,7 +2,10 @@ package aws import ( "fmt" + "math/rand" + "strings" "testing" + "time" "github.com/hashicorp/aws-sdk-go/aws" "github.com/hashicorp/aws-sdk-go/gen/autoscaling" @@ -10,7 +13,7 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccAWSLaunchConfiguration(t *testing.T) { +func TestAccAWSLaunchConfiguration_withBlockDevices(t *testing.T) { var conf autoscaling.LaunchConfiguration resource.Test(t, resource.TestCase{ @@ -26,39 +29,75 @@ func TestAccAWSLaunchConfiguration(t *testing.T) { resource.TestCheckResourceAttr( "aws_launch_configuration.bar", "image_id", "ami-21f78e11"), resource.TestCheckResourceAttr( - "aws_launch_configuration.bar", "name", "foobar-terraform-test"), - resource.TestCheckResourceAttr( - "aws_launch_configuration.bar", "instance_type", "t1.micro"), + "aws_launch_configuration.bar", "instance_type", "m1.small"), resource.TestCheckResourceAttr( "aws_launch_configuration.bar", "associate_public_ip_address", "true"), resource.TestCheckResourceAttr( "aws_launch_configuration.bar", "spot_price", ""), ), }, + }, + }) +} +func TestAccAWSLaunchConfiguration_withSpotPrice(t *testing.T) { + var conf autoscaling.LaunchConfiguration + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy, + Steps: []resource.TestStep{ resource.TestStep{ - Config: TestAccAWSLaunchConfigurationWithSpotPriceConfig, + Config: testAccAWSLaunchConfigurationWithSpotPriceConfig, Check: resource.ComposeTestCheckFunc( testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf), - testAccCheckAWSLaunchConfigurationAttributes(&conf), resource.TestCheckResourceAttr( "aws_launch_configuration.bar", "spot_price", "0.01"), ), }, + }, + }) +} +func TestAccAWSLaunchConfiguration_withGeneratedName(t *testing.T) { + var conf autoscaling.LaunchConfiguration + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy, + Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAWSLaunchConfigurationNoNameConfig, Check: resource.ComposeTestCheckFunc( testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf), - testAccCheckAWSLaunchConfigurationAttributes(&conf), - resource.TestCheckResourceAttr( - "aws_launch_configuration.bar", "name", "terraform-foo"), // FIXME - This should fail?!?!? + testAccCheckAWSLaunchConfigurationGeneratedNamePrefix( + "aws_launch_configuration.bar", "terraform-"), ), }, }, }) } +func testAccCheckAWSLaunchConfigurationGeneratedNamePrefix( + resource, prefix string) resource.TestCheckFunc { + return func(s *terraform.State) error { + r, ok := s.RootModule().Resources[resource] + if !ok { + return fmt.Errorf("Resource not found") + } + name, ok := r.Primary.Attributes["name"] + if !ok { + return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes) + } + if !strings.HasPrefix(name, prefix) { + return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix) + } + return nil + } +} + func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).autoscalingconn @@ -98,14 +137,40 @@ func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfig return fmt.Errorf("Bad image_id: %s", *conf.ImageID) } - if *conf.LaunchConfigurationName != "foobar-terraform-test" { + if !strings.HasPrefix(*conf.LaunchConfigurationName, "terraform-") { return fmt.Errorf("Bad name: %s", *conf.LaunchConfigurationName) } - if *conf.InstanceType != "t1.micro" { + if *conf.InstanceType != "m1.small" { return fmt.Errorf("Bad instance_type: %s", *conf.InstanceType) } + // Map out the block devices by name, which should be unique. + blockDevices := make(map[string]autoscaling.BlockDeviceMapping) + for _, blockDevice := range conf.BlockDeviceMappings { + blockDevices[*blockDevice.DeviceName] = blockDevice + } + + // Check if the root block device exists. + if _, ok := blockDevices["/dev/sda1"]; !ok { + fmt.Errorf("block device doesn't exist: /dev/sda1") + } + + // Check if the secondary block device exists. + if _, ok := blockDevices["/dev/sdb"]; !ok { + fmt.Errorf("block device doesn't exist: /dev/sdb") + } + + // Check if the third block device exists. + if _, ok := blockDevices["/dev/sdc"]; !ok { + fmt.Errorf("block device doesn't exist: /dev/sdc") + } + + // Check if the secondary block device exists. + if _, ok := blockDevices["/dev/sdb"]; !ok { + return fmt.Errorf("block device doesn't exist: /dev/sdb") + } + return nil } } @@ -143,30 +208,47 @@ func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchC } } -const testAccAWSLaunchConfigurationConfig = ` +var testAccAWSLaunchConfigurationConfig = fmt.Sprintf(` resource "aws_launch_configuration" "bar" { - name = "foobar-terraform-test" + name = "terraform-test-%d" image_id = "ami-21f78e11" - instance_type = "t1.micro" + instance_type = "m1.small" user_data = "foobar-user-data" associate_public_ip_address = true -} -` -const TestAccAWSLaunchConfigurationWithSpotPriceConfig = ` + root_block_device { + volume_type = "gp2" + volume_size = 11 + } + ebs_block_device { + device_name = "/dev/sdb" + volume_size = 9 + } + ebs_block_device { + device_name = "/dev/sdc" + volume_size = 10 + volume_type = "io1" + iops = 100 + } + ephemeral_block_device { + device_name = "/dev/sde" + virtual_name = "ephemeral0" + } +} +`, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) + +var testAccAWSLaunchConfigurationWithSpotPriceConfig = fmt.Sprintf(` resource "aws_launch_configuration" "bar" { - name = "foobar-terraform-test" + name = "terraform-test-%d" image_id = "ami-21f78e11" instance_type = "t1.micro" - user_data = "foobar-user-data" - associate_public_ip_address = true spot_price = "0.01" } -` +`, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) const testAccAWSLaunchConfigurationNoNameConfig = ` resource "aws_launch_configuration" "bar" { - image_id = "ami-21f78e12" + image_id = "ami-21f78e11" instance_type = "t1.micro" user_data = "foobar-user-data-change" associate_public_ip_address = false diff --git a/website/source/docs/providers/aws/r/launch_config.html.markdown b/website/source/docs/providers/aws/r/launch_config.html.markdown index 677f3b0880..5a80a97b8d 100644 --- a/website/source/docs/providers/aws/r/launch_config.html.markdown +++ b/website/source/docs/providers/aws/r/launch_config.html.markdown @@ -33,6 +33,57 @@ The following arguments are supported: * `security_groups` - (Optional) A list of associated security group IDS. * `associate_public_ip_address` - (Optional) Associate a public ip address with an instance in a VPC. * `user_data` - (Optional) The user data to provide when launching the instance. +* `block_device_mapping` - (Optional) A list of block devices to add. Their keys are documented below. + + +## Block devices + +Each of the `*_block_device` attributes controls a portion of the AWS +Launch Configuration's "Block Device Mapping". It's a good idea to familiarize yourself with [AWS's Block Device +Mapping docs](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html) +to understand the implications of using these attributes. + +The `root_block_device` mapping supports the following: + +* `volume_type` - (Optional) The type of volume. Can be `"standard"`, `"gp2"`, + or `"io1"`. (Default: `"standard"`). +* `volume_size` - (Optional) The size of the volume in gigabytes. +* `iops` - (Optional) The amount of provisioned + [IOPS](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-io-characteristics.html). + This must be set with a `volume_type` of `"io1"`. +* `delete_on_termination` - (Optional) Whether the volume should be destroyed + on instance termination (Default: `true`). + +Modifying any of the `root_block_device` settings requires resource +replacement. + +Each `ebs_block_device` supports the following: + +* `device_name` - The name of the device to mount. +* `snapshot_id` - (Optional) The Snapshot ID to mount. +* `volume_type` - (Optional) The type of volume. Can be `"standard"`, `"gp2"`, + or `"io1"`. (Default: `"standard"`). +* `volume_size` - (Optional) The size of the volume in gigabytes. +* `iops` - (Optional) The amount of provisioned + [IOPS](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-io-characteristics.html). + This must be set with a `volume_type` of `"io1"`. +* `delete_on_termination` - (Optional) Whether the volume should be destroyed + on instance termination (Default: `true`). + +Modifying any `ebs_block_device` currently requires resource replacement. + +Each `ephemeral_block_device` supports the following: + +* `device_name` - The name of the block device to mount on the instance. +* `virtual_name` - The [Instance Store Device + Name](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html#InstanceStoreDeviceNames) + (e.g. `"ephemeral0"`) + +Each AWS Instance type has a different set of Instance Store block devices +available for attachment. AWS [publishes a +list](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html#StorageOnInstanceTypes) +of which ephemeral devices are available on each type. The devices are always +identified by the `virtual_name` in the format `"ephemeral{0..N}"`. ## Attributes Reference