user_data 中的命令不会在 terraform 中执行 [英] Commands in user_data are not executed in terraform

查看:36
本文介绍了user_data 中的命令不会在 terraform 中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好 EC2 实例已创建,但作为 userdata.sh 一部分的命令未得到执行.当我通过 putty 手动连接到 EC2 时,我发现 EC2 实例中没有安装 nginx.为了验证脚本是否正在执行,我添加了回显消息,但是当我运行 terraform apply 时,命令提示符中没有显示任何输出.如何验证用户数据是否正在执行?

Hi EC2 instance is created, but commands as part of userdata.sh are not gettingexecuted. When I manually connect to EC2 via putty, i found that nginx is not installed in EC2 instance. To verify if the script is getting executed or not I added echo message, but no output is display in command prompt when i run terraform apply. How can i verify if the user-data is getting executed or not?

我已将 Terraform 安装在 C 驱动器中,并且以下脚本存在于同一文件夹 C:/Terraform/userdata.sh、C:/Terraform/main.tf 中,我尝试将路径指定为 ${file("./userdata.sh")}" 但还是不行.

I have installed Terraform in C drive and below script are present in same folder C:/Terraform/userdata.sh, C:/Terraform/main.tf, i tried giving path as ${file("./userdata.sh")}" but still it does not work.

请指教,因为我刚刚学习 terraform.谢谢.

Please advice as I am just learning terraform. Thanks.

#!/bin/bash -v
echo "userdata-start"
sudo apt-get update -y
sudo apt-get install -y nginx > /tmp/nginx.log
sudo service nginx start
echo "userdata-end"

这在我的 terraform 程序 [main.tf] 中被调用如下:

This is been called in my terraform program [main.tf] as below:

# resource "template_file" "user_data" {
#    template = "userdata.sh"
# }

data "template_file" "user_data" {
template = "${file("userdata.sh")}"
}

resource "aws_instance" "web" {
instance_type = "t2.micro"

ami = "ami-5e8bb23b"

key_name = "sptest"

vpc_security_group_ids = ["${aws_security_group.default.id}"]
subnet_id              = "${aws_subnet.tf_test_subnet.id}"

user_data               = "${data.template_file.user_data.template}"
#user_data              = "${template_file.user_data.rendered}"
#user_data              = "${file("userdata.sh")}"
#user_data              = "${file("./userdata.sh")}"


tags {
Name = "tf-example-ec2"
}
} 

推荐答案

我可以看到你发布的代码有一个问题,user_data 变量应该是这样的

I could see one issue with the code you have posted, the user_data variable should be like

user_data = "${data.template_file.user_data.rendered}"

此外,作为一项建议,我建议您尝试在脚本中创建一个日志文件,以检查所有步骤已执行.了解脚本是否完全运行也会对您有所帮助.

Moreover, as a suggestion i will recommend you to try creating a log file in your script to check what all steps have been executed. It will also benefit you to know whether the script ran at all or not.

我们代码中的一个示例,您可以根据自己的标准对其进行修改

One sample from our code, you can modify it based on your standards

logdir=/var/log
logfile=${logdir}/mongo_setup.log
exec >> $logfile 2>&1

希望这会有所帮助.

这篇关于user_data 中的命令不会在 terraform 中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆