I'm trying to run a terraform script which accepts the parameter of VM size, network details and image details to spin up a new VM from the user. If I run the script second time, it destroys the resources created the first time, which I don't want. What could be the possible solution to keep the resources it created the first time?
Just a glimpse of what I'm running through:
resource "google_compute_network" "custom" {
name = "test-ntwrk"
auto_create_subnetworks = false,
}
resource "google_compute_subnetwork" "custom-subnet" {
count = "${var.count}"
name = "${var.name[count.index]}",
ip_cidr_range = "${var.cidrs[count.index]}",
region = "asia-south1",
network = "${google_compute_network.custom.self_link}"
}
This makes the subnet with the given range, which, lets say ["10.0.1.0/24", "10.0.2.0/24"].
When I re-run the same script to add 2 more subnets with CIDR range ["10.0.3.0/24, 10.0.4.0/24] to the already created script, the terraform plan shows me something like this:
Google_compute_subnetwork.custom-subnet[0] (new resource required)
id: "asia-south1/subnet-tf-0" => <computed> (forces new resource)
creation_timestamp: "2019-02-06T11:34:34.371-08:00" => <computed>
gateway_address: "10.0.1.1" => <computed>
ip_cidr_range: "10.0.1.0/24" => "10.3.0.0/24" (forces new resource)
name: "subnet-tf-0" => "subnet-tf-0"
network: "https://www.googleapis.com/compute/v1/projects/cio-demopoc-project-228209/global/networks/test-ntwrk" => "https://www.googleapis.com/compute/v1/projects/cio-demopoc-project-228209/global/networks/test-ntwrk"
project: "xxxxxxxxxx" => <computed>
region: "asia-south1" => "asia-south1"
secondary_ip_range.#: "0" => <computed>
self_link: "https://www.googleapis.com/compute/v1/projects/cio-demopoc-project-228209/regions/asia-south1/subnetworks/subnet-tf-0" => <computed>
-/+ google_compute_subnetwork.custom-subnet[1] (new resource required)
id: "asia-south1/subnet-tf-1" => <computed> (forces new resource)
creation_timestamp: "2019-02-06T22:31:39.600-08:00" => <computed>
fingerprint: "rt9soQpV_Nw=" => <computed>
gateway_address: "10.0.2.1" => <computed>
ip_cidr_range: "10.0.2.0/24" => "10.0.4.0/24" (forces new resource)
name: "subnet-tf-1" => "subnet-tf-1"
network: "https://www.googleapis.com/compute/v1/projects/cio-demopoc-project-228209/global/networks/test-ntwrk" => "https://www.googleapis.com/compute/v1/projects/cio-demopoc-project-228209/global/networks/test-ntwrk"
project: "xxxxxxxxxx" => <computed>
region: "asia-south1" => "asia-south1"
secondary_ip_range.#: "0" => <computed>
self_link: "https://www.googleapis.com/compute/v1/project
Running this script 2nd time, forces the creation of resources and replacing the existing one, which I don't want.
Expected output: All the 4 subnets should be created within the VPC.
apply
and then show the change to the variables for the nextplan
with the plan output shown as you have now. – ydaetskcoR