Unable to get tags properly

Gurus,

Trying to get value of particular tag like I have below output from terraform.tfstate

resource “aws_instance” “test-rhel-build”

it creates below tags as designed

“tags”: {
“Name”: “test-rhel-build”,
“ts:environment”: “xxxxxxx”,
“ts:platform”: “aws”,
“ts:region”: “us-east-1”,
“ts:wwid”: “bbbxre”
}

I need to login to this instance to run customise code apart from user-data. This will basically create a dns record with bbbxre.ts.aws.com

How I can fetch the tag aws_instance.test-rhel-build.tags[“ts:wwid”] value and create another variable with aws_instance.test-rhel-build.tags[“ts:wwid”] + “.ts.aws.com” and make ssh to it?

Terraform v0.14.8

The resource already exposes private_ip, public_ip and public_dns. Would those be sufficient to use?

Couldn’t the custom code part be loaded directly using the user-data?
Other options could be use of instance-metadata service so that the instance would write tags itself (requires instance profile and terraform skip additional tags).

Third option could be use of such an solution based on Route 53, Cloudwatch and Lambda.

Thank you for your response. Instance metadata have this tags information but while querying tags, unable to do so as it contains a character :. Setup is behind firewall, which does not allow ip address to login. It just allows internal fqdn. My question is how to query those tags to use it in other codes.

Hm, what’s the difference between the internal FQDN and the private IP?

As tags is a normal map you can just access its values like any other map. If needed you can use a local-exec provisioner to ssh to it.

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"

  tags =  {
    "Name"= "test-rhel-build",
    "ts:environment"= "xxxxxxx",
    "ts:platform"= "aws",
    "ts:region"= "us-east-1",
    "ts:wwid"= "bbbxre"
  }
}

output "fqdn_tagvalue" {
  value = "${aws_instance.web.tags["ts:wwid"]}.mylovelydomain.com"
}