terraform-google-modules / terraform-google-bastion-host

Generates a bastion host VM compatible with OS Login and IAP Tunneling that can be used to access internal VMs
https://registry.terraform.io/modules/terraform-google-modules/bastion-host/google
Apache License 2.0
124 stars 90 forks source link

Passing a startup script as metadata #177

Closed dmytro closed 6 months ago

dmytro commented 8 months ago

TL;DR

When passing a metadata block with startup script to the instance other metadata don't work.

Expected behavior

According to the documentation additional metadata should be added.

Observed behavior

1) Below is a working terraform code WITHOUT startup script. We are using roles/user groups to authorize login to the host.

module "iap_bastion" {
 source    = "terraform-google-modules/bastion-host/google"
 name     = "bastion-host"
 project    = var.project_id
 machine_type = "e2-medium"
 zone     = "${var.region}-a"
 network    = module.vpc.network_name
 subnet    = module.vpc.subnets_names[0]
 image     = "debian-11-bullseye-v20210817"
 image_family = "debian-11"
 image_project = "debian-cloud"
 scopes    = ["cloud-platform"]
 version    = "5.3.0"
 members = [
  "group:<redacted>@<redacted>",
  "group:<redacted>@<redacted>",
  "group:<redacted>@<redacted>",
 ]

  random_role_id = false
}

2) Following terraform code below is example of NOT WORKING code. The only difference is added metadata block with startup-script string.

module "iap_bastion" {
 source    = "terraform-google-modules/bastion-host/google"
 name     = "bastion-host"
 project    = var.project_id
 machine_type = "e2-medium"
 zone     = "${var.region}-a"
 network    = module.vpc.network_name
 subnet    = module.vpc.subnets_names[0]
 image     = "debian-11-bullseye-v20210817"
 image_family = "debian-11"
 image_project = "debian-cloud"
 scopes    = ["cloud-platform"]
 version    = "5.3.0"
 members = [
  "group:<redacted>@<redacted>",
  "group:<redacted>@<redacted>",
  "group:<redacted>@<redacted>",
 ]

 random_role_id = false

 metadata = {
  startup-script = <<-EOF
#!/bin/bash
apt update
apt -y upgrade
apt -y install postgresql-client
apt -y install redis-server stunnel4 telnet
EOF
 }
}

Problem:

Terraform Configuration

module "iap_bastion" {
 source    = "terraform-google-modules/bastion-host/google"
 name     = "bastion-host"
 project    = var.project_id
 machine_type = "e2-medium"
 zone     = "${var.region}-a"
 network    = module.vpc.network_name
 subnet    = module.vpc.subnets_names[0]
 image     = "debian-11-bullseye-v20210817"
 image_family = "debian-11"
 image_project = "debian-cloud"
 scopes    = ["cloud-platform"]
 version    = "5.3.0"
 members = [
  "group:<redacted>@<redacted>",
  "group:<redacted>@<redacted>",
  "group:<redacted>@<redacted>",
 ]

 random_role_id = false

 metadata = {
  startup-script = <<-EOF
#!/bin/bash
apt update
apt -y upgrade
apt -y install postgresql-client
apt -y install redis-server stunnel4 telnet
EOF
 }
}

### Terraform Version

```sh
Terraform v1.6.6

Additional information

No response

dmytro commented 8 months ago

I've found, that I should use startup_script argument. Unfortunately it's not documented on the https://registry.terraform.io/modules/terraform-google-modules/bastion-host/google/latest page.

dmytro commented 8 months ago

I have to reopen the issue since I've found that using startup_script argument gives the same result.

Below is full example of the code:

module "iap_bastion" {
  source        = "terraform-google-modules/bastion-host/google"
  name          = "bastion-host"
  project       = var.project_id
  machine_type  = "e2-medium"
  zone          = "${var.region}-a"
  network       = module.vpc.network_name
  subnet        = module.vpc.subnets_names[0]
  image         = "debian-11-bullseye-v20210817"
  image_family  = "debian-11"
  image_project = "debian-cloud"
  scopes        = ["cloud-platform"]
  version       = "5.3.0"
  members = [
    "group:<REDACTED>@<REDACTED>",
    "group:<REDACTED>@<REDACTED>",
    "group:<REDACTED>@<REDACTED>",
  ]

  random_role_id = false

   startup_script = <<-EOF
 #!/bin/bash
 apt update
 apt -y upgrade
 apt -y install postgresql-client
 apt -y install redis-server stunnel4 telnet
EOF
}
github-actions[bot] commented 6 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days