我正在尝试用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
我在同一个文件夹中有三个文件,其内容很小:
provider "aws" {
region = "us-west-1"
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
}
resource "aws_instance" "master_proxy" {
ami = "ami-123sample"
instance_type = "t2.micro"
}
variable "access_key" { default = "sampleaccesskey" }
variable "secret_key" { default = "samplesecretkey" }
如果我直接设置access_key
和secret_key
,而不是通过变量设置,则可行。一个类似的设置与秘密文件和什么适用于我的另一个项目;我只是不明白这里有什么不对。
答案 0 :(得分:2)
首先,terraform validate
验证.tf
个文件的文件夹,以检查语法是否正确。您无法将单独的vars文件传递给该命令。实际上,terraform validate
甚至不会检查您的变量是否设置正确。
其次,您的secrets.tfvars
文件使用了错误的语法。相反,你希望它看起来更像这样:
access_key = "sampleaccesskey"
secret_key = "samplesecretkey"
但是这会出错,因为你实际上 已经定义 .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错误,因为所需的变量未设置:
variable "access_key" {}
variable "secret_key" {}
provider "aws" {
region = "us-west-1"
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
}
答案 1 :(得分:0)
*.tf
和*.tfvars
文件的假设结构(和差异)。
对于那些可能在以后遇到类似问题的人:
*.tf
文件用于配置和声明,这意味着必须在variable
文件中定义任何*.tf
*.tfvars
个文件用于为已定义的变量提供值。这些文件可以使用-var-file
标志(我误用了)传递。答案 2 :(得分:0)
# 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"