Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform: add block_device support to resource_aws_launch_configuration #935

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions builtin/providers/aws/resource_aws_launch_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,63 @@ func resourceAwsLaunchConfiguration() *schema.Resource {
Optional: true,
ForceNew: true,
},

"block_device": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"device_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"virtual_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"snapshot_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"volume_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"volume_size": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},

"delete_on_termination": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
ForceNew: true,
},

"encrypted": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
},
},
Set: resourceAwsInstanceBlockDevicesHash,
},
},
}
}
Expand All @@ -109,6 +166,23 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface
v.(*schema.Set).List())
}

if v := d.Get("block_device"); v != nil {
vs := v.(*schema.Set).List()
if len(vs) > 0 {
createLaunchConfigurationOpts.BlockDevices = make([]autoscaling.BlockDeviceMapping, len(vs))
for i, v := range vs {
bd := v.(map[string]interface{})
createLaunchConfigurationOpts.BlockDevices[i].DeviceName = bd["device_name"].(string)
createLaunchConfigurationOpts.BlockDevices[i].VirtualName = bd["virtual_name"].(string)
createLaunchConfigurationOpts.BlockDevices[i].SnapshotId = bd["snapshot_id"].(string)
createLaunchConfigurationOpts.BlockDevices[i].VolumeType = bd["volume_type"].(string)
createLaunchConfigurationOpts.BlockDevices[i].VolumeSize = int64(bd["volume_size"].(int))
createLaunchConfigurationOpts.BlockDevices[i].DeleteOnTermination = bd["delete_on_termination"].(bool)
createLaunchConfigurationOpts.BlockDevices[i].Encrypted = bd["encrypted"].(bool)
}
}
}

log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts)
_, err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
if err != nil {
Expand Down Expand Up @@ -159,6 +233,18 @@ func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}
d.Set("security_groups", lc.SecurityGroups)
d.Set("spot_price", lc.SpotPrice)

bds := make([]map[string]interface{}, len(lc.BlockDevices))
for i, m := range lc.BlockDevices {
bds[i] = make(map[string]interface{})
bds[i]["device_name"] = m.DeviceName
bds[i]["snapshot_id"] = m.SnapshotId
bds[i]["volume_type"] = m.VolumeType
bds[i]["volume_size"] = m.VolumeSize
bds[i]["delete_on_termination"] = m.DeleteOnTermination
bds[i]["encrypted"] = m.Encrypted
}
d.Set("block_device", bds)

return nil
}

Expand Down
16 changes: 16 additions & 0 deletions builtin/providers/aws/resource_aws_launch_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfig
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.BlockDevices {
blockDevices[blockDevice.DeviceName] = blockDevice
}

// 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
}
}
Expand Down Expand Up @@ -139,6 +150,11 @@ resource "aws_launch_configuration" "bar" {
instance_type = "t1.micro"
user_data = "foobar-user-data"
associate_public_ip_address = true
block_device {
device_name = "/dev/sdb"
volume_type = "gp2"
volume_size = 10
}
}
`

Expand Down
11 changes: 11 additions & 0 deletions website/source/docs/providers/aws/r/launch_config.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ The following arguments are supported:
* `key_name` - (Optional) The key name that should be used for the instance.
* `security_groups` - (Optional) A list of associated security group IDS.
* `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.

Each `block_device_mapping` supports the following:

* `device_name` - The name of the device to mount.
* `virtual_name` - (Optional) The virtual device name.
* `snapshot_id` - (Optional) The Snapshot ID to mount.
* `volume_type` - (Optional) The type of volume. Can be standard, gp2, or io1. Defaults to standard.
* `volume_size` - (Optional) The size of the volume in gigabytes.
* `delete_on_termination` - (Optional) Should the volume be destroyed on instance termination (defaults true).
* `encrypted` - (Optional) Should encryption be enabled (defaults false).

## Attributes Reference

Expand Down