I am new to terraform and building my first terraform script to launch instances in GCP. Values from terraform.tfvars
are not being loaded and only default values from variables.tf
are being loaded. Below are the commands that I am using to run to run the script. Can someone let me know the mistake I am doing?
terraform plan -var-file=terraform.tfvars
terraform apply-var-file=terraform.tfvars
Below is the file structure
├── gcp_instance
│ ├── gcp_instance.tf
│ └── variables.tf
├── main.tf
└── terraform.tfvars
My main.tf
file
module gcpinstance {
source = “./gcp_instance”
}
my gcp_instance.tf
file
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.5.0"
}
}
}
provider "google" {
region = "${var.gcp_region}"
project = "${var.gcp_project}"
zone = "${var.gcp_zone}"
credentials = "${var.gcp_credentials}"
}
resource "google_compute_instance" "vm_instance" {
count = length(var.instance_name)
name = var.instance_name[count.index]
machine_type = element(var.instance_type, count.index)
boot_disk {
initialize_params {
image = element(var.instance_image, count.index)
}
}
network_interface {
network = "${var.gcp_network}"
}
}
my variables.tf
file
variable "gcp_project" {
description = "Google Cloud Platform project"
default = "test"
}
variable "gcp_region" {
description = "Google Cloud Platform's selected region"
default = "us-central1"
}
variable "gcp_zone" {
description = "Google Cloud Platform's selected Zone"
default = "us-central1-f"
}
variable "gcp_credentials" {
description = "Credential file to be used for this project"
default = "test"
}
variable "instance_tags" {
type = list
default = ["application1", "application2"]
}
variable "instance_type" {
type = list
default = ["f1-micro","f1-micro"]
}
variable "instance_name" {
type = list(string)
default = ["application1", "application2"]
}
variable "instance_image" {
type = list
default = ["debian-cloud/debian-9","debian-cloud/debian-9"]
}
variable "gcp_network" {
description = "Network to be used"
default = "default"
}
variable "gcp_network_global_cidr" {
description = "CIDR for the Instances"
default = "10.0.0.0/24"
}
my terraform.tfvars
file
gcp_project = "test"
gcp_region = "us-central1"
gcp_zone = "us-central1-f"
gcp_credentials = "test.json"
instance_tags = ["application1", "application2", "application3"]
instance_type = ["f1-micro", "f1-micro", "f1-micro"]
instance_name = ["application1", "application2", "application3"]
instance_image = ["debian-cloud/debian-9", "debian-cloud/debian-9", "debian-cloud/debian-9"]
gcp_network = "default"
gcp_network_global_cidr = "10.0.0.0/24"