Inside a Terraform templatefile(), ${variable} is Terraform interpolation. A bare $VAR is passed through unchanged into the rendered script. We wrote $$DB thinking Terraform needed the $ escaped — but in a single-quoted heredoc (<< 'EOF'), the shell doesn’t expand anything at template-render time anyway, and at runtime $$ is still the shell’s special variable for the current PID.
Before (broken):
gsutil cp "$$DB" "gs://$$BUCKET/zoo_bot.db"
# Ran as: gsutil cp "4001" "gs://4001BUCKET/zoo_bot.db"After (fixed):
gsutil cp "$DB" "gs://$BUCKET/zoo_bot.db"Tip
Rule — In Terraform’s
templatefile(), only${...}is template syntax. Use plain$VARinside single-quoted heredocs;$$is a shell PID reference, not an escape sequence.
See also
- gcp-vm — the
metadata_startup_scriptheredoc this bug was found in - linux-file-ownership — another Linux ownership/permissions gotcha from a GCP VM startup script