问题描述
我正在使用 terraform 启动一个 aws_launch_configuration
实例.
I am launching a aws_launch_configuration
instance using terraform.
我正在为 user_data
变量使用 shell 脚本,如下所示:
I'm using a shell script for the user_data
variable, like so:
resource "aws_launch_configuration" "launch_config" {
...
user_data = "${file("router-init.sh")}"
....
}
在这个 router-init.sh 中,我想做的一件事是访问我通过 terraform 启动的其他实例的 IP 地址.
Within this router-init.sh, one of the things I would like to do, is to have access to the ip addresses for other instances I am launching via terraform.
我知道我可以使用 splat 访问该实例的所有 IP 地址,例如:
I know that I can use a splat to access all the ip addresses of that instance, for instance:
output ip_address {
value = ${aws_instance.myAWSInstance.*.private_ip}"
}
有没有办法在 router-init.sh 脚本中传递/访问这些 IP 地址?
Is there a way to pass / access these ip addresses within the router-init.sh script?
推荐答案
你可以使用 template_file
数据源:
You can do this using a template_file
data source:
data "template_file" "init" {
template = "${file("router-init.sh.tpl")}"
vars = {
some_address = "${aws_instance.some.private_ip}"
}
}
然后在模板中引用它,如:
Then reference it inside the template like:
#!/bin/bash
echo "SOME_ADDRESS = ${some_address}" > /tmp/
然后将其用于 user_data
:
user_data = ${data.template_file.init.rendered}
这篇关于在 user_data 提供者模板文件中访问 Terraform 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!