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

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

问题描述

已创建Hi EC2实例,但未执行作为userdata.sh一部分的命令.当我通过腻子手动连接到EC2时,我发现EC2实例中未安装nginx.为了验证脚本是否正在执行,我添加了echo消息,但是当我运行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?

我已经在C驱动器中安装了Terraform,并且以下脚本存在于同一文件夹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天全站免登陆