无法在 Terraform 中运行 metadata_startup_script [英] Unable to run metadata_startup_script in Terraform
问题描述
我有一个 terraform 脚本
I have a terraform script
resource "google_compute_attached_disk" "default3" {
disk = google_compute_disk.default2.id
instance = google_compute_instance.default.id
}
resource "google_compute_instance" "default" {
name = "test"
machine_type = "custom-8-16384"
zone = "asia-south1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = "centos-cloud/centos-7"
}
}
network_interface {
network = "default"
access_config {
}
}
metadata_startup_script = <<-EOF
echo "hello
you" > /test.txt
echo "help
me" > /test2.txt
EOF
lifecycle {
ignore_changes = [attached_disk]
}
}
resource "google_compute_disk" "default2" {
name = "test-disk"
type = "pd-balanced"
zone = "asia-south1-a"
image = "centos-7-v20210609"
size = 100
}
现在我想运行 metadata_startup_script,但它不会执行.创建资源但不执行脚本.我怎样才能运行这个脚本?基本上我有一个很大的 bash 脚本.如何在 GCP 机器上运行此脚本?
Now I want to run metadata_startup_script, but it won`t execute. Resources are created but the script is not executed. How can I run this script? Basically I have a big bash script. How can I run this script in GCP machine?
推荐答案
如果你有一个大脚本要运行,我建议你使用 startup-script-url 从 GCS 加载启动脚本,而不是在 terraform 中硬编码.
If you have a large script to run, I recommend you to use startup-script-url to load the startup script from GCS instead of having it hardcoded in the terraform.
resource "google_compute_instance" "default" {
name = "test"
machine_type = "custom-8-16384"
zone = "asia-south1-a"
metadata = {
startup-script-url = "gs://<bucket>/path/to/file"
}
...
...
}
否则,对于您当前的问题,我还建议使用元数据
Else, for your current issue, I also recommend to use the metadata
metadata = {
startup-script = <<-EOF
echo "hello
you" > /test.txt
echo "help
me" > /test2.txt
EOF
}
这篇关于无法在 Terraform 中运行 metadata_startup_script的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!