在tfvars中定义的Terraform变量提示我

时间:2019-06-20 23:21:49

标签: variables google-cloud-platform terraform

对于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"

我也有一个不错的脚本:

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}"
}

1 个答案:

答案 0 :(得分:0)

您可以尝试通过以下方式强制其读取变量:

terraform apply -var-file=<path_to_your_vars>