英文:
terraform not updating content from user-data file
问题
我有一个附加到 main.tf 的用户数据文件。每当我对用户数据文件进行更改,然后运行 terraform apply,更改不会立即反映在服务器上,直到我销毁并重新创建资源。请问这是默认操作还是我遗漏了什么。感谢回答。
在对用户数据文件进行更改后,我期望 terraform apply 会创建一个新的实例,其中包含更新后的用户数据文件内容,但这并没有发生。
英文:
I have a user-data file attached to the main.tf. Whenever I make changes to the user-data file, and then run the terraform apply, the changes do not reflect on the server until I destroy and recreate the resources. Please is this the default operation or am I missing something. Thank you for answers.
After making the changes on the user date file, I expect that terraform apply will create a new instance with the updated user-data file content, but that is not happening.
答案1
得分: 1
根据terraform上的aws_instance资源文档,你需要将user_data_replace_on_change属性设置为true。默认情况下为false。
user_data_replace_on_change - (可选)与user_data或user_data_base64一起使用时,如果设置为true,将触发销毁和重新创建。如果未设置,默认为false。
还请注意,默认情况下,user_data仅在实例创建时应用,因此当设置了此标志时,terraform将销毁并重新创建资源。
英文:
As documented on the aws_instance resource on terraform, you need to set the user_data_replace_on_change attribute to true. It is false by default.
> user_data_replace_on_change - (Optional) When used in combination with user_data or user_data_base64 will trigger a destroy and recreate when set to true. Defaults to false if not set.
Also note that by default user_data is only applied at the time of instance creation so terraform will destroy and create the resource again when this flag is set.
答案2
得分: 0
Chris Doyle - 非常感谢,是的,我使用了下面的代码,它在一个shell脚本中运行。
resource "aws_instance" "ec2_instance" {
ami = data.aws_ami.base_ami.id
count = "1"
subnet_id = "subnet-xxxxxxxx"
instance_type = "t3a.medium"
key_name = "keyname"
user_data_replace_on_change = true
user_data = "${file("init.sh")}"
}
注意:下面的部分与相同的配置不起作用,所以我使用了shell脚本。
<<EOF
#!/bin/bash
echo "输出内容"
sudo yum install httpd -y && sudo service httpd start
EOF
英文:
Chris Doyle - thank you so much, yes I used the code below, it's working with a shell script.
resource "aws_instance" "ec2_instance" {
ami = data.aws_ami.base_ami.id
count = "1"
subnet_id = "subnet-xxxxxxxx"
instance_type = "t3a.medium"
key_name = "keyname"
user_data_replace_on_change = true
user_data = "${file("init.sh")}"
}
}
}
Note: the below part is not working with the same config. So I used shell script.
<< EOF
#!/bin/bash
echo "echo the content"
sudo yum install httpd -y && sudo service httpd start
EOF
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论