Last active
November 2, 2021 01:07
-
-
Save mscribellito/4448f422eeec82dc38b68958ce02fae1 to your computer and use it in GitHub Desktop.
Google Compute Engine - VM Instance with multiple startup scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Google Compute Engine | |
# VM Instance with multiple startup scripts | |
data "google_compute_default_service_account" "default" { | |
} | |
resource "google_compute_instance" "default" { | |
name = "test" | |
machine_type = "e2-small" | |
zone = "us-east1-b" | |
boot_disk { | |
initialize_params { | |
image = "ubuntu-2004-focal-v20210927" | |
} | |
} | |
network_interface { | |
network = "default" | |
access_config { | |
} | |
} | |
# this could also be specified at metadata.startup-script | |
metadata_startup_script = join("\n", [ | |
# iterates over each script, reads file and renders content using variables. | |
for script in local.startup_scripts : templatefile( | |
"${path.module}/${script["file"]}", | |
script["vars"] | |
) | |
]) | |
service_account { | |
email = data.google_compute_default_service_account.default.email | |
scopes = ["cloud-platform"] | |
} | |
} | |
locals { | |
# startup_scripts is a list of objects where you can specify multiple scripts | |
# and their respective variables. | |
# | |
# list(object({ | |
# file = string | |
# vars = object({...}) | |
# })) | |
startup_scripts = [ | |
{ | |
file = "script1.sh" | |
vars = { | |
var1 = "hello" | |
} | |
}, | |
{ | |
file = "script2.sh" | |
vars = { | |
var2 = "world" | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment