Created
March 1, 2018 14:56
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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