Terraform: Get 'value' of particular tag 'key' of aws resource.

Created on 15 Aug 2017  ยท  5Comments  ยท  Source: hashicorp/terraform

I want to get a 'value' of particular tag 'key' of aws resource. (say 'aws_ami')
Using the latest terraform version. (Terraform v0.10.0)

Here is data source.

data "aws_ami" "web" { filter { name = "state" values = ["available"] } filter { name = "tag:Name" values = ["Hello"] } most_recent = true }

Now, I am able to get all the tags associated with above resource as

"${data.aws_ami.web.tags}"

But, I want to get the 'value' of particular 'key' from that returned list. (say 'value' of key 'key1')
Is there a way to achieve this?

Below article explains some syntax. (like tags.#.key)
But its not working.
https://www.terraform.io/docs/providers/aws/d/ami.html#tags-key

Getting below error.

Failed to load root config module: Error loading modules: module compute: Error loading .terraformmodulesc46c475f0ca1e50cfe8501b3ff5285a6compute.tf: Error reading config for output tags: parse error at 1:25: expected "}" but found invalid sequence "#"

Can anyone help me out?

documentation provideaws

Most helpful comment

Hi @dspatil!

The following syntax should work: ${data.aws_ami.web.tags["Name"]} .

Sorry the docs are misleading here. They are trying to explain that a key is needed in that position, but we should update those docs to be clearer about how such a thing is specified in Terraform syntax.

All 5 comments

Hi @dspatil!

The following syntax should work: ${data.aws_ami.web.tags["Name"]} .

Sorry the docs are misleading here. They are trying to explain that a key is needed in that position, but we should update those docs to be clearer about how such a thing is specified in Terraform syntax.

This issue has been automatically migrated to terraform-providers/terraform-provider-aws#1423 because it looks like an issue with that provider. If you believe this is _not_ an issue with the provider, please reply to this issue and let us know.

Hi @apparentlymart

Already tried as you suggested. No luck.
As we need to enclose complete section in 'double' quotes, it gives error.
i.e.
output "ami_information" { value = "${data.aws_ami.my_ami.tags["Name"]}" }

The proposed solutions don't work

* module.k8s.output.k8s_master_name: __builtin_StringToInt: strconv.ParseInt: parsing "Name": invalid syntax in:
${aws_instance.k8s_master.*.tags["Name"]}
* module.k8s.output.k8s_minion_name: __builtin_StringToInt: strconv.ParseInt: parsing "Name": invalid syntax in:
${aws_instance.k8s_minion.*.tags["Name"]}

If have found a working solution using template rendering to by-pass the list of map's issue:

resource "aws_instance" "k8s_master" {
  count                       = "${var.master_count}"
  ami                         = "${var.ami}"
  instance_type               = "${var.instance_type}"
  vpc_security_group_ids      = ["${aws_security_group.k8s_sg.id}"]
  associate_public_ip_address = false
  subnet_id                   = "${element(var.subnet_ids,count.index % length(var.subnet_ids))}"
  user_data                   = "${file("${path.root}/files/user_data.sh")}"
  iam_instance_profile        = "${aws_iam_instance_profile.master_profile.name}"

  tags = "${merge(
    local.k8s_tags,
    map(
      "Name", "k8s-master-${count.index}",
      "Environment", "${var.environment}"
    )
  )}"
}

data "template_file" "k8s_master_names" {
  count    = "${var.master_count}"
  template = "${lookup(aws_instance.k8s_master.*.tags[count.index], "Name")}"
}

output "k8s_master_name" {
  value = [
    "${data.template_file.k8s_master_names.*.rendered}",
  ]
}

This will result in the following output:

k8s_master_name = [
    k8s-master-0,
    k8s-master-1,
    k8s-master-2
]

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ketzacoatl picture ketzacoatl  ยท  3Comments

larstobi picture larstobi  ยท  3Comments

c4milo picture c4milo  ยท  3Comments

rjinski picture rjinski  ยท  3Comments

sprokopiak picture sprokopiak  ยท  3Comments