Skip to content

Instantly share code, notes, and snippets.

@YakDriver
Created March 1, 2018 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YakDriver/5fe3bf451c951ee511686f38e4093968 to your computer and use it in GitHub Desktop.
Save YakDriver/5fe3bf451c951ee511686f38e4093968 to your computer and use it in GitHub Desktop.
Set the name tag of an AWS instance from the AWS instance (e.g., in userdata)
# terraform locals (in a .tf file)
locals {
name_prefix = "terrafirm"
full_build_id = format("notcb:%s", uuid()) #128-bit rfc 4122 v4 UUID
build_id = "${substr(element(split(":",local.full_build_id),1), 0, 8)}${substr(element(split(":",local.full_build_id),1), 9, 4)}"
resource_name = "${local.name_prefix}-${local.build_id}"
}
# terraform template declaration (in a .tf file)
data "template_file" "lx_userdata" {
template = "${file("linux/userdata.sh")}"
vars {
tfi_name_tag = "${local.resource_name}"
}
}
# userdata.sh file
#!/bin/bash
aws ec2 create-tags --resources $(curl -s http://169.254.169.254/latest/meta-data/instance-id) --tags Key=Name,Value="${tfi_name_tag}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment