在 Terraform 中添加多个 GCP 实例

时间:2021-01-14 05:16:27

标签: terraform-provider-gcp

在我的 main.tf 文件中,当我运行它时,它为我创建了 3 个实例 wrk-1-3 和 3 个 mgr-1-3 实例但是可以说我需要 50 个 wrk-1 实例和 50 个 mgr-1 实例。有没有办法修改计数,这样我就不必在本地手动输入 50 个实例?

您会在下面找到我的 main.tf 示例代码。

    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
    }
  }
}

我也在使用我的另一个 mynetwork.tf 文件

  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
    }

0 个答案:

没有答案