我正在使用terraform启动aws_launch_configuration
实例。
我使用user_data
变量的shell脚本,如下所示:
resource "aws_launch_configuration" "launch_config" {
...
user_data = "${file("router-init.sh")}"
....
}
在这个router-init.sh中,我想做的其中一件事就是访问我通过terraform启动的其他实例的ip地址。
我知道我可以使用splat访问该实例的所有IP地址,例如:
output ip_address {
value = ${aws_instance.myAWSInstance.*.private_ip}"
}
有没有办法在router-init.sh脚本中传递/访问这些ip地址?
答案 0 :(得分:12)
您可以使用template_file
data source:
data "template_file" "init" {
template = "${file("router-init.sh.tpl")}"
vars {
some_address = "${aws_instance.some.private_ip}"
}
}
然后在模板中引用它,如:
#!/bin/bash
echo "SOME_ADDRESS = ${some_address}" > /tmp/
然后将其用于user_data
:
user_data = ${data.template_file.init.rendered}
答案 1 :(得分:2)
对于从Terraform 0.12及更高版本开始来到这里的人们,应该使用templatefile function而不是使用template_file资源,因此对于该示例中的示例,它类似于
exports.myFunctionName = functions.firestore
.document('docName').onUpdate((change, context) => {
const usernames = []; //a list of usernames which I'm not filling here
let topValueIds = [];
return axios.get('an-endpoint')
.then(resp => {
// I guess you use the value of resp somewhere in this then() block
// But it is not clear where, from your question...
const topValues = []; //some filtered array
let topPicks = [];
const promises = [];
usernames.forEach(usr => {
// No use of the usr value??? You probably use it to define the URL called by axios...
promises.push(axios.get('endpoint'));
});
return Promises.all(promises);
})
.then(resultsArray => {
resultsArray.forEach(result => {
// .... Up to you to adapt this part!
// resultArray is an Array containing the results of all the promises passed to Promise.all(), i.e. the results of the axios calls
});
const docRef = db.collection('collection-name').doc('doc-name');
return docRef.set({
user_list: topPicks,
});
});
});