Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

Add multiple GCP instances in Terraform

in my main.tf file when i run it creates for me 3 instances wrk-1-3 and 3 instances of mgr-1-3 But lets say that i need 50 wrk-1 instances and 50 mgr-1 instances. Is there a way to modify the count so that i dont have to type 50 instances manually in the locals?

Below you will find my main.tf sample code.

    main.tf
    
    variable "instance_name" {}
        variable "instance_zone" {}
        variable "instance_type" {
          default = "n1-standard-1"
          }
        variable "instance_subnetwork" {}
        
        locals {
          names = [ 
            "wrk-1",
            "wrk-2",
            "wrk-3",
            "mgr-1",
            "mgr-2",
            "mgr-3"
          ]
        }
        
        resource "google_compute_instance" "vm_instance" {
          count         = length(local.names)
          name          = local.names[count.index]
          zone         = "${var.instance_zone}"
          machine_type = "${var.instance_type}"
          boot_disk {
            initialize_params {
              #image = "debian-cloud/debian-9"
              image = "ubuntu-os-cloud/ubuntu-1804-lts"
              }
          }

 network_interface {
    subnetwork = "${var.instance_subnetwork}"
    access_config {
      # Allocate a one-to-one NAT IP to the instance
    }
  }
}

I am also using my other mynetwork.tf file

  mynetwork.tf
    
    # Create the mynetwork network
    resource "google_compute_network" "mynetworknet" {
      name                    = "mynetworknet"
      auto_create_subnetworks = "false"
    }
    
    # Create mynetworksubnet-us subnetwork
    resource "google_compute_subnetwork" "mynetworksubnet-us" {
      name          = "mynetworksubnet-us"
      region        = "us-central1"
      network       = google_compute_network.mynetworknet.self_link
      ip_cidr_range = "10.130.0.0/20"
    }
    
    # Add a firewall rule to allow HTTP, SSH, and RDP traffic on mynetwork
    resource "google_compute_firewall" "mynetworknet-allow-http-ssh-rdp-icmp" {
      name    = "mynetworknet-allow-http-ssh-rdp-icmp"
      network = google_compute_network.mynetworknet.self_link
      allow {
        protocol = "tcp"
        ports    = ["22", "80", "3389"]
      }
      allow {
        protocol = "icmp"
      }
    }
    
    # Add the mynetworknet-us-vm instance
    module "mynetworknet-us-vm" {
      source              = "./instance"
      instance_name       = ""
      instance_zone       = "us-central1-a"
      instance_subnetwork = google_compute_subnetwork.mynetworksubnet-us.self_link
    }
question from:https://stackoverflow.com/questions/65713775/add-multiple-gcp-instances-in-terraform

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...