简体   繁体   English

Terraform .tfvars转换解码错误

[英]Terraform .tfvars cast decoding error

I'm trying to set up something really simple with Terraform, but it gives me an error I haven't seen before. 我正在尝试用Terraform设置一些非常简单的东西,但它给了我一个我以前没见过的错误。

When I run terraform validate -var-file=secrets.tfvars I get the following error: 当我运行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

And when I run terraform plan -var-file=secrets.tfvars I get this: 当我运行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

I have three files within the same folder, and their content is minimal: 我在同一个文件夹中有三个文件,其内容很小:

providers.tf providers.tf

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

main.tf main.tf

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

secrets.tfvars secrets.tfvars

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

If I set access_key and secret_key directly, and not via variables, then it works. 如果我直接设置access_keysecret_key ,而不是通过变量,那么它可以工作。 A similar setup with secrets-files and whatnot works on another project of mine; 一个类似的设置与秘密文件和什么适用于我的另一个项目; I just don't understand what's wrong here. 我只是不明白这里有什么不对。

Firstly, terraform validate validates a folder of .tf files to check that the syntax is correct. 首先, terraform validate验证.tf文件的文件夹以检查语法是否正确。 You can't pass a separate vars file to the command. 您无法将单独的vars文件传递给该命令。 In fact, terraform validate won't even check your variables are even set properly. 事实上, terraform validate甚至不会检查你的变量是否设置得当。

Secondly, your secrets.tfvars file is using the wrong syntax. 其次,您的secrets.tfvars文件使用了错误的语法。 Instead you want it to look more like this: 相反,你希望它看起来更像这样:

secrets.tfvars: secrets.tfvars:

access_key = "sampleaccesskey"
secret_key = "samplesecretkey"

But this will error because you haven't actually defined the variables in a .tf file: 但这会出错,因为您实际上没有在.tf文件中定义变量:

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

If you don't have a sensible default for a variable (such as typically in this case) then you can remove the default argument to the variable and this will make Terraform error on the plan because a required variable is not set: 如果您没有变量的合理默认值(例如通常在这种情况下),那么您可以删除变量的default参数,这将使计划出现Terraform错误,因为未设置所需的变量:

providers.tf providers.tf

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

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

Well, I messed up big time. 好吧,我搞砸了很多时间。 I somehow managed to forget the supposed structure (and difference) of *.tf and *.tfvars files. 我以某种方式设法忘记了*.tf*.tfvars文件的假定结构(和差异)。

For those who might run into a similar problem later on: 对于那些可能在以后遇到类似问题的人:

  • *.tf files are for configuration and declaration , which means that any variable s must be defined within a *.tf file. *.tf文件用于配置声明 ,这意味着必须在*.tf文件中定义任何variable s。
  • *.tfvars files are for giving values to already defined variables . *.tfvars文件用于为已定义的变量赋值 These files can be passed with the -var-file flag (which I had misused). 这些文件可以使用-var-file标志传递(我误用了)。
     # 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM