Terraform .tfvars 转换解码错误

Terraform .tfvars cast decoding error

我正在尝试使用 Terraform 设置一些非常简单的东西,但它给了我一个以前从未见过的错误。

当我 运行 terraform validate -var-file=secrets.tfvars 我得到以下错误:

Error loading files open /home/MYUSER/Documents/git/packer-with-terraform/terratest/-var-file=secrets.tfvars: no such file or directory

当我 运行 terraform plan -var-file=secrets.tfvars 我得到这个:

invalid value "secrets.tfvars" for flag -var-file: Error decoding Terraform vars file: At 1:10: root.variable: unknown type for string *ast.ObjectList

我在同一个文件夹中有三个文件,它们的内容很少:

providers.tf

provider "aws" {
    region                      = "us-west-1"
    access_key                  = "${var.access_key}"
    secret_key                  = "${var.secret_key}"
}

main.tf

resource "aws_instance" "master_proxy" {
    ami                         = "ami-123sample"
    instance_type               = "t2.micro"
}

secrets.tfvars

variable "access_key" { default = "sampleaccesskey" }
variable "secret_key" { default = "samplesecretkey" }

如果我直接设置 access_keysecret_key,而不是通过变量,那么它就可以工作。与秘密文件类似的设置和诸如此类的东西适用于我的另一个项目;我只是不明白这里出了什么问题。

好吧,我把时间搞砸了。我以某种方式设法忘记了 *.tf*.tfvars 文件的假定结构(和差异)。

对于以后可能 运行 遇到类似问题的人:

  • *.tf 文件用于 配置 声明 ,这意味着必须定义任何 variable*.tf 文件中。
  • *.tfvars 文件用于为已定义的变量赋值。这些文件可以通过 -var-file 标志(我误用了)来传递。

首先,terraform validate 验证包含 .tf 个文件的文件夹以检查语法是否正确。您不能将单独的 vars 文件传递​​给命令。事实上,terraform validate 甚至不会检查您的变量是否设置正确。

其次,您的 secrets.tfvars 文件使用了错误的语法。相反,您希望它看起来更像这样:

secrets.tfvars:

access_key = "sampleaccesskey"
secret_key = "samplesecretkey"

但这会出错,因为您实际上没有定义 .tf 文件中的变量:

providers.tf

variable "access_key" { default = "sampleaccesskey" }
variable "secret_key" { default = "samplesecretkey" }

provider "aws" {
    region                      = "us-west-1"
    access_key                  = "${var.access_key}"
    secret_key                  = "${var.secret_key}"
}

如果您没有合理的变量默认值(例如在这种情况下通常如此),那么您可以删除变量的 default 参数,这将使 Terraform 计划出错,因为未设置所需变量:

providers.tf

variable "access_key" {}
variable "secret_key" {}

provider "aws" {
    region                      = "us-west-1"
    access_key                  = "${var.access_key}"
    secret_key                  = "${var.secret_key}"
}
     # Set a Provider
    provider "aws" {
      region     = "${var.region}"
      access_key = "${var.access_key}"
      secret_key = "${var.secret_key}"
    }

    resource "aws_security_group" "test-server-sg" {
      name = "test-server-sg"

      ingress {
        from_port   = 8080
        to_port     = 8080
        protocol    = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
      }
    }

    resource "aws_instance" "test-server" {
      ami           = "${var.ami}"
      instance_type = "${var.instance_type}"

      user_data = <<-EOF
                  #!/bin/bash
                  echo "Hello, World" > index.html
                  nohup busybox httpd -fp 8080 &
                  EOF

      tags {
        name        = "Test Web Server"
        environment = "${var.environment}"
        project     = "${var.project}"
      }
    } 


     variable "region" {
      type        = "string"
      description = "AWS region"
    }

    variable "access_key" {
      type        = "string"
      description = "AWS access key"
    }

    variable "secret_key" {
      type        = "string"
      description = "AWS secret key"
    }

    variable "ami" {
      type        = "string"
      description = "AWS image id"
    }

    variable "instance_type" {
      type        = "string"
      description = "AWS instance type"
    }

    variable "environment" {
      type        = "string"
      description = "AWS environment name"
    }

    variable "project" {
      type        = "string"
      description = "AWS project name"
    }



    output "Test Server Public DNS" {
      value = "${aws_instance.test-server.public_dns}"
    }

    output "Test Server Public IP" {
      value = "${aws_instance.test-server.public_ip}"
    }

    region = "us-east-1"
    access_key = "put your aws access key here"
    secret_key = "put your aws secret key here"
    ami = "ami-40d28157"
    instance_type = "t2.micro"
    environment = "Test"
    project = "Master Terraform"