Terraform-provider-aws: tags support for *_block_device within aws_instance resources

Created on 3 Apr 2018  路  11Comments  路  Source: hashicorp/terraform-provider-aws

It would be useful if tags could be provided inline with EBS volumes in aws_instance resources - via the *_block_device stanzas.

I'm aware of volume_tags, which sprays all volume with the same tags. That is expected because the EC2 API just works that way, but not ideal for instances where different volumes have different tag needs.

Terraform Version

Terraform v0.11.3

  • provider.archive v1.0.0
  • provider.aws v1.11.0

Affected Resource(s)

  • aws_instance

Terraform Configuration Files

resource "aws_instance" "example" {
  ami = "${var.my_ami}"
  tags = {
    InstanceName = "${var.server_name}"
  }
  root_block_device {
    volume_size = "${var.root_volume_size}"
    volume_type = "gp2"
    tags = {
      EBSVol = "RootVol"
    }
  }
  ebs_block_device {
    device_name = "/dev/sdb"  
    volume_size = "${var.data_volume_size}"
    volume_type = "gp2"
    delete_on_termination = true
    tags = {
      Data = "SecretFiles"
    }
  }
}

References

Opening new issue per ask in https://github.com/hashicorp/terraform/issues/3531

enhancement proposal servicec2

Most helpful comment

For now I'm using null_resource to tag instance's root volumes. Maybe someone will find it handy:

resource "null_resource" "tag_volumes" {
  provisioner "local-exec" {
    command = <<EOF
aws ec2 create-tags \
  --resources ${aws_instance.this.root_block_device.0.volume_id} \
  --region ${data.aws_region.current.name} \
  --tags \
    Key=Name,Value=${aws_instance.this.tags["Name"]} \
    Key=DLMSnapshotsPolicyName,Value=daily2d
EOF
  }
}

All 11 comments

hi, @mr-olson

Error: aws_spot_fleet_request.XXX: launch_specification.0.ebs_block_device.0: invalid or unknown key: tags

$ terraform -version
Terraform v0.11.5

  • provider.aws v1.13.0

spot_fleet_request's configuration cannot support EBS tags?

@mr-olson not only different tag needs, also volume_tags are not stateful. If I add a key/value pair to volume_tags, Terraform does not upgrade those changes.

It will be nice to also have support for count under the *_block_device

@heldersepu handling dynamic sub-resource configurations should be handled in the upcoming Terraform 0.12 release using a new for and for_each syntax within the configuration language (HCL). Further tracking for that specific feature request can be found here: https://github.com/hashicorp/terraform/issues/7034

A high level preview of this feature and others coming in that version can be found in: https://www.hashicorp.com/blog/terraform-0-1-2-preview -- I believe we will be releasing additional blog posts going into additional details about some of these as well.

@bflad Awesome! Looking forward to 0.12

Can you give us a code example of how something like that would look under the new release?

Please see the comments in the upstream issue for draft implementations: https://github.com/hashicorp/terraform/issues/7034#issuecomment-359311997

I believe this will specifically be its own blog post in the coming weeks.

(Aside: I would suggest commenting on the upstream Terraform core issue about this topic rather than this one as its unrelated to the original issue and messaging all the issue followers.)

Just stumbled over this issue, too. When assigning many ebs_block_device inline with an aws_instance it is difficult to identify them later on. Since aws_ebs_volume supports tags already, it should not be a general issue.

Could someone please give a status update on this when they have a spare moment? I also have some use cases where this would be useful (setting tags to implement DLM policies on certain devices attached to an EC2 instance)

This is a really needed feature. Not being able to add Name and other custom tags to the volumes makes tag-based resource grouping incomplete. Adding them manually later or using separate script defeats the purpose of managing everything via Terraform :(

For now I'm using null_resource to tag instance's root volumes. Maybe someone will find it handy:

resource "null_resource" "tag_volumes" {
  provisioner "local-exec" {
    command = <<EOF
aws ec2 create-tags \
  --resources ${aws_instance.this.root_block_device.0.volume_id} \
  --region ${data.aws_region.current.name} \
  --tags \
    Key=Name,Value=${aws_instance.this.tags["Name"]} \
    Key=DLMSnapshotsPolicyName,Value=daily2d
EOF
  }
}

For anyone considering the workaround above, you may try using the aws_ec2_tag resource.

resource "aws_ec2_tag" "example" {
  resource_id = element(tolist(aws_instance.example.ebs_block_device.*.volume_id), 0)
  key         = "Name2"
  value       = "Hello World!"
}
Was this page helpful?
0 / 5 - 0 ratings