Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't add metadata / startup script to compute_instance from terragrunt. #448

Open
Om-Jumaili opened this issue Dec 4, 2024 · 0 comments

Comments

@Om-Jumaili
Copy link

Hello Everyone, Sorry if this is obvious or if it's been answered before, I looked far and wide for an answer but I couldn't find anything.

I used terragrunt to import the instance_template, and now I'm trying to import the compute_instance, and everything works save for not being able to define a startup script for each instance I'm trying to create.

I know I can define a startup script for the template, but I need different startup scripts for each compute_instance, and I can't find the input I need for the compute_instance module.

Here is my Instance_template:

include "root" {
  path   = find_in_parent_folders()
  expose = true
}

terraform {
  source = "git::https://github.com/terraform-google-modules/terraform-google-vm.git//modules/instance_template?ref=v12.1.2"
}

inputs = {
  project_id               = include.root.locals.project_id
  region                   = include.root.locals.region
  network                  = include.root.locals.network_name
  subnetwork               = "dev-private"
  subnetwork_project      = include.root.locals.project_id
  tags                     = ["terragrunt-temp"]
  disk_type                = "pd-standard"
  disk_labels              = {environment = "dev"}
  disk_size_gb             = 100
  source_image             = "projects/ubuntu-os-cloud/global/images/ubuntu-2404-noble-amd64-v20241115"
  machine_type             = "e2-standard-2"
  service_account          = {
    email = "*****"
  }
}

and here are the configurations I tried for compute_instance:

include "root" {
    path = find_in_parent_folders()
    expose = true
}
terraform {
    source = "git::https://github.com/terraform-google-modules/terraform-google-vm.git//modules/compute_instance?ref=v12.1.1"
}

inputs = {
    project_id                      = include.root.locals.project_id
    hostname                        = "terragrunt-clam-av"
    network                         = include.root.locals.network_name
    network_tier                    = "STANDARD"
    subnetwork                      = dependency.Instance_Template.inputs.subnetwork
    subnetwork_project              = dependency.Instance_Template.inputs.subnetwork_project
    region                          = include.root.locals.region
    zone                            = include.root.locals.Zone
    static_ips                      = ["10.10.20.2"]
    instance_template               = dependency.Instance_Template.outputs.self_link
    service_account                 = dependency.Instance_Template.inputs.service_account
    access_config                   = [{
      nat_ip = null
      network_tier = "STANDARD"
    }]
    metadata={ 
startup-script = <<-EOT
        #!/bin/bash
        apt-get update
        apt-get upgrade -y
        touch /iwashere.txt
      EOT
    }
}

dependency "Instance_Template" {
  config_path  = "../Instance_Template"
  skip_outputs = false
}
include "root" {
    path = find_in_parent_folders()
    expose = true
}
terraform {
    source = "git::https://github.com/terraform-google-modules/terraform-google-vm.git//modules/compute_instance?ref=v12.1.1"
}

inputs = {
    project_id                      = include.root.locals.project_id
    hostname                        = "terragrunt-clam-av"
    network                         = include.root.locals.network_name
    network_tier                    = "STANDARD"
    subnetwork                      = dependency.Instance_Template.inputs.subnetwork
    subnetwork_project              = dependency.Instance_Template.inputs.subnetwork_project
    region                          = include.root.locals.region
    zone                            = include.root.locals.Zone
    static_ips                      = ["10.10.20.2"]
    instance_template               = dependency.Instance_Template.outputs.self_link
    service_account                 = dependency.Instance_Template.inputs.service_account
    access_config                   = [{
      nat_ip = null
      network_tier = "STANDARD"
    }]
    metadata_startup_script  = <<-EOT
        #!/bin/bash
        apt-get update
        apt-get upgrade -y
        touch /iwashere.txt
      EOT
    }

dependency "Instance_Template" {
  config_path  = "../Instance_Template"
  skip_outputs = false
}
include "root" {
    path = find_in_parent_folders()
    expose = true
}
terraform {
    source = "git::https://github.com/terraform-google-modules/terraform-google-vm.git//modules/compute_instance?ref=v12.1.1"
}

inputs = {
    project_id                      = include.root.locals.project_id
    hostname                        = "terragrunt-clam-av"
    network                         = include.root.locals.network_name
    network_tier                    = "STANDARD"
    subnetwork                      = dependency.Instance_Template.inputs.subnetwork
    subnetwork_project              = dependency.Instance_Template.inputs.subnetwork_project
    region                          = include.root.locals.region
    zone                            = include.root.locals.Zone
    static_ips                      = ["10.10.20.2"]
    instance_template               = dependency.Instance_Template.outputs.self_link
    service_account                 = dependency.Instance_Template.inputs.service_account
    access_config                   = [{
      nat_ip = null
      network_tier = "STANDARD"
    }]
    instance_metadata={ 
startup-script = <<-EOT
        #!/bin/bash
        apt-get update
        apt-get upgrade -y
        touch /iwashere.txt
      EOT
    }
}

dependency "Instance_Template" {
  config_path  = "../Instance_Template"
  skip_outputs = false
}

any help is highly appreciated, Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant