问题
Ok, so most of this is working except...
We have a user data template file for getting each new AWS server to register with Chef Automate. Chef refers to each client by the "node_name" set in the user data script, which is the instance id by default. But when viewing in the Chef UI or "knife node list", the instance id isn't exactly user friendly. We were able to write out a meaningful node_name using the template. Something like:
data "template_file" "user-data-qa" {
count = "${var.QA_numhosts}"
template = "${file("userdata.tpl")}"
vars {
node_name = "${var.customer_name}-QA-${format("%d", count.index + 1)}"
}
}
However, If we rebuild the instance, we get an error from Chef since the new instance tries to register with the same name, but a newly generated key.
So, we added a random number suffix to the node_name. We'd like this random number to be updated every time the instance is rebuilt. The first attempt was to set the instance id as the "keeper" for the random number. This resulted in the cycle error: ( -> means "depends on") Instance -> User Data -> Random -> Instance
Also tried dumping random generation and just appending a substring of the instance id to the node_name. Same problem, although a shorter cycle: Instance -> User Data -> Instance.
Any ideas for getting around this? In short, we want to append a string to the node_name, which gets inserted into the user data, and that string should update every time the instance is terminated and re-launched. Without cycle errors from Terraform.
回答1:
instance-id
, as you have seen, is calculated, so you can't use it in the definition of the resource itself, or through dependency cycles.
Rather than populate the instance ID inside Terraform, try having the user data script itself get the instance ID and populate node_name.rb
with that value. You can do this with a simple curl
and echo:
echo "node_name '$(curl --silent /dev/null http://169.254.169.254/latest/meta-data/instance-id)'" > path/to/node_name.rb
来源:https://stackoverflow.com/questions/49741807/terraform-cyclic-dependency-challenge