Skip to content

Issue While | Building VMs using same IP (again) removing previous | TFE | module "gce_instance" #532

@hardikbadjatiya

Description

@hardikbadjatiya

TL;DR

This issue is common when reusing VMs: residual files, packages, or state from previous runs can cause idempotency problems in your startup_script.

Expected behavior

First Provisioning (Fresh VM):
The startup_script runs on a clean VM.
All required packages (e.g., podman) are installed.
Application setup completes without errors.
Success messages are logged (e.g., Podman installed successfully., Startup script completed successfully.).
Subsequent Runs (Same VM, Script Re-run):
The script checks if packages like podman are already installed and skips reinstallation if present.
Any temporary or residual files from previous runs are cleaned up at the start.
The script is idempotent: running it multiple times does not cause failures or duplicate installations.
Success messages are logged again, indicating a clean, successful run.
On Failure:
If any step fails, the script logs a clear error message and exits with a non-zero status.
The provisioning process reports the failure, allowing for troubleshooting.

Observed behavior

This issue is common when reusing VMs: residual files, packages, or state from previous runs can cause idempotency problems in your startup_script.

To resolve it make your script and ensure clean, repeatable runs, But ideally it shouldn't be the case:

Make your script idempotent:
Check if podman is already installed before installing.
Clean up or reset any files, configs, or state that may interfere with re-runs.
Log success/failure clearly:
Output clear success messages at the end.
Exit with a non-zero code on failure.

Terraform Configuration

module "gce_instance_app" {
source = "tfe.example.com/org/compute/google"
version = "0.1.16"
name = "app"
project_id = "example-project-id"
zone = "us-central1-a"
boot_disk_size = 75
machine_type = "n2-standard-8"
boot_disk_type = "pd-standard"
service_account_email = "EMAIL"
additional_service_scopes = []

min_cpu_platform = var.min_cpu_platform
subnetwork = "projects/example-project-id/regions/us-central1/subnetworks/example-subnet"
static_ip = "192.168.16.254"
vpc = "example-vpc"
image_id = "custom-image-id"
google_kms_key_ring = "example-key-ring"
google_kms_crypto_key = "example-crypto-key"
key_ring_location = "us-central1"
additional_network_tags = []
network_tags = ["allow-health-check","allow-i-health-check","custom-tag-1","custom-tag-2"]
startup_script = templatefile("./init-platform-playwright.sh", { user = "some", testEnabled = "true" })

labels
app_name = "example-app"
app_env = "dev"
provisioner_repo = "example-repo"
}

Terraform Version

1.7.4

Terraform Provider Versions

google = {
      source  = "hashicorp/google"
      version = ">=3.6.3"
}

Additional information

Steps to Reproduce
terraform init
terraform apply
#!/bin/bash
set -e

echo "Starting startup script..."

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions