我使用 Terraform 在 GCP 中创建了一个 VPC,现在我想添加两个子网公共和私有。在创建虚拟机时。如果我通过了公共子网,VM 应该获得公共 IP;如果我通过私有子网,VM 应该获得唯一的私有内部 IP 地址
## Create VPC
resource "google_compute_network" "vpc" {
name = "${var.name}-vpc"
auto_create_subnetworks = "false"
}
## Create Subnet
## Create public subnet
resource "google_compute_subnetwork" "pub-subnet" {
name = "${var.name}-public-subnet"
ip_cidr_range = "${var.pub-subnet-cidr}"
network = "${var.name}-vpc"
depends_on = ["google_compute_network.vpc"]
region = "asia-south1"
}
## Create private subnet
resource "google_compute_subnetwork" "private-subnet" {
name = "${var.name}-private-subnet"
ip_cidr_range = "${var.private-subnet-cidr}"
network = "${var.name}-vpc"
depends_on = ["google_compute_network.vpc"]
region = "asia-south1"
private_ip_google_access = "true"
}
##### 创建虚拟机实例
resource "google_compute_instance" "default" {
name = "qa7-web-linux-${count.index + 1}"
count = "${var.count}"
machine_type = "${var.machine_type}"
boot_disk {
initialize_params {
image = "${var.web-srv-image}"
}
}
network_interface {
subnetwork = "${var.subnet-private}"
access_config {
}
}
metadata {
sshKeys = "indra:${file(".//modules//instances//key.pub")}"
}
}