【问题标题】:Terraform .tfvars cast decoding errorTerraform .tfvars 转换解码错误
【发布时间】:2016-11-29 21:00:42
【问题描述】:

我正在尝试使用 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,而不是通过变量,那么它可以工作。带有秘密文件和其他类似设置的类似设置适用于我的另一个项目;我只是不明白这里出了什么问题。

【问题讨论】:

    标签: validation syntax terraform


    【解决方案1】:

    首先,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}"
    }
    

    【讨论】:

    • 我刚刚回答了我自己的问题,但您的回答提供了更全面的解释。谢谢!
    【解决方案2】:

    好吧,我搞砸了。我不知何故忘记了*.tf*.tfvars 文件的假定结构(和差异)。

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

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

    【讨论】:

      【解决方案3】:
           # 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"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-20
        • 2018-03-21
        • 1970-01-01
        相关资源
        最近更新 更多