Skip to content

Commit

Permalink
Add computed field for volume_id of block device (#1489)
Browse files Browse the repository at this point in the history
resource/aws_instance: Add computed field for volume_id of block device
  • Loading branch information
rowleyaj authored and Ninir committed Jan 10, 2018
1 parent ebd68e5 commit 9c1a558
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ func resourceAwsInstance() *schema.Resource {
Computed: true,
ForceNew: true,
},

"volume_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
Set: func(v interface{}) int {
Expand Down Expand Up @@ -409,6 +414,11 @@ func resourceAwsInstance() *schema.Resource {
Computed: true,
ForceNew: true,
},

"volume_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -1152,6 +1162,8 @@ func readBlockDevicesFromInstance(instance *ec2.Instance, conn *ec2.EC2) (map[st
instanceBd := instanceBlockDevices[*vol.VolumeId]
bd := make(map[string]interface{})

bd["volume_id"] = *vol.VolumeId

if instanceBd.Ebs != nil && instanceBd.Ebs.DeleteOnTermination != nil {
bd["delete_on_termination"] = *instanceBd.Ebs.DeleteOnTermination
}
Expand Down
6 changes: 6 additions & 0 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ func TestAccAWSInstance_blockDevices(t *testing.T) {
"aws_instance.foo", &v),
resource.TestCheckResourceAttr(
"aws_instance.foo", "root_block_device.#", "1"),
resource.TestMatchResourceAttr(
"aws_instance.foo", "root_block_device.0.volume_id", regexp.MustCompile("vol-[a-z0-9]+")),
resource.TestCheckResourceAttr(
"aws_instance.foo", "root_block_device.0.volume_size", "11"),
resource.TestCheckResourceAttr(
Expand All @@ -275,12 +277,16 @@ func TestAccAWSInstance_blockDevices(t *testing.T) {
"aws_instance.foo", "ebs_block_device.#", "3"),
resource.TestCheckResourceAttr(
"aws_instance.foo", "ebs_block_device.2576023345.device_name", "/dev/sdb"),
resource.TestMatchResourceAttr(
"aws_instance.foo", "ebs_block_device.2576023345.volume_id", regexp.MustCompile("vol-[a-z0-9]+")),
resource.TestCheckResourceAttr(
"aws_instance.foo", "ebs_block_device.2576023345.volume_size", "9"),
resource.TestCheckResourceAttr(
"aws_instance.foo", "ebs_block_device.2576023345.volume_type", "standard"),
resource.TestCheckResourceAttr(
"aws_instance.foo", "ebs_block_device.2554893574.device_name", "/dev/sdc"),
resource.TestMatchResourceAttr(
"aws_instance.foo", "ebs_block_device.2554893574.volume_id", regexp.MustCompile("vol-[a-z0-9]+")),
resource.TestCheckResourceAttr(
"aws_instance.foo", "ebs_block_device.2554893574.volume_size", "10"),
resource.TestCheckResourceAttr(
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ The following attributes are exported:
* `vpc_security_group_ids` - The associated security groups in non-default VPC
* `subnet_id` - The VPC subnet ID.

For any `root_block_device` and `ebs_block_device` the `volume_id` is exported.
e.g. `aws_instance.web.root_block_device.0.volume_id`

## Import

Expand Down

0 comments on commit 9c1a558

Please sign in to comment.