我是 Terraform 的新手,正在构建我的第一个 Terraform 脚本以在 GCP 中启动实例。来自的值terraform.tfvars
是不是正在加载,并且仅variables.tf
加载默认值。以下是我用来运行脚本的命令。有人可以告诉我我犯的错误吗?
terraform plan -var-file=terraform.tfvars
terraform apply-var-file=terraform.tfvars
以下是文件结构
├── gcp_instance
│ ├── gcp_instance.tf
│ └── variables.tf
├── main.tf
└── terraform.tfvars
我的main.tf
文件
module gcpinstance {
source = "./gcp_instance"
}
我的gcp_instance.tf
文件
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}"
}
}
我的variables.tf
文件
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"
}
我的 terraform.tfvars
文件
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"