首页 > 解决方案 > Terraform 变量在 tfvars 中定义时提示我

问题描述

关于 terraform 变量,我有些不理解。当我运行“terraform apply”时,系统会提示我输入两个变量。我不认为我应该在定义 terraform.tfvars 时提示我输入任何内容。系统提示我输入(applicationNamespace 和 staticIpName),但我不知道为什么。我有什么误解?

我创建了一个文件(terraform.tfvars):

#--------------------------------------------------------------
# General
#--------------------------------------------------------------
cluster = "reddiyo-development"
project = "<MYPROJECTID>"
region  = "us-central1"
credentialsLocation = "<MYCERTLOCATION>"
bucket = "reddiyo-terraform-state"
vpcLocation = "us-central1-b"
network = "default"
staticIpName = "dev-env-ip"
#--------------------------------------------------------------
# Specific To NODE
#--------------------------------------------------------------
terraformPrefix = "development"
mainNodeName = "primary-pool"
nodeMachineType = "n1-standard-1"
#--------------------------------------------------------------
# Specific To Application
#--------------------------------------------------------------
applicationNamespace = "application"

我还有一个 terrform 脚本:

variable "cluster" {}
variable "project" {}
variable "region" {}
variable "bucket" {}
variable "terraformPrefix" {}
variable "mainNodeName" {}
variable "vpcLocation" {}
variable "nodeMachineType" {}
variable "credentialsLocation" {}
variable "network" {}
variable "applicationNamespace" {}
variable "staticIpName" {}


data "terraform_remote_state" "remote" {
  backend = "gcs"
  config = {
    bucket = "${var.bucket}"
    prefix = "${var.terraformPrefix}"
  }
}

provider "google" {
  //This needs to be updated to wherever you put your credentials
  credentials = "${file("${var.credentialsLocation}")}"
  project     = "${var.project}"
  region      = "${var.region}"
}

resource "google_container_cluster" "gke-cluster" {
  name     = "${var.cluster}"
  network  = "${var.network}"
  location   = "${var.vpcLocation}"

  remove_default_node_pool = true

  # node_pool {
  #   name = "${var.mainNodeName}"
  # }
  node_locations = [
    "us-central1-a",
    "us-central1-f"
  ]
  //Get your credentials for the newly created cluster so that microservices can be deployed
  provisioner "local-exec" {
    command = "gcloud config set project ${var.project}"
  }
  provisioner "local-exec" {
    command = "gcloud container clusters get-credentials ${var.cluster} --zone ${var.vpcLocation}"
  }
}

resource "google_container_node_pool" "primary_pool" {
  name       = "${var.mainNodeName}"
  cluster    = "${var.cluster}"
  location   = "${var.vpcLocation}"
  node_count = "2"

  node_config {
    machine_type = "${var.nodeMachineType}"

    oauth_scopes = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
      "https://www.googleapis.com/auth/devstorage.read_only",
      "https://www.googleapis.com/auth/service.management.readonly",
      "https://www.googleapis.com/auth/servicecontrol",
      "https://www.googleapis.com/auth/trace.append",
    ]
  }

  management {
    auto_repair  = true
    auto_upgrade = true
  }
  autoscaling {
    min_node_count = 2
    max_node_count = 10
  }
}

# //Reserve a Static IP
resource "google_compute_address" "ip_address" {
  name = "${var.staticIpName}"
}

//Install Ambassador
module "ambassador" {
  source      = "modules/ambassador"
  applicationNamespace = "${var.applicationNamespace}"
}

标签: variablesgoogle-cloud-platformterraform

解决方案


您可以尝试使用以下命令强制它读取您的变量:

terraform apply -var-file=<path_to_your_vars>

推荐阅读