【问题标题】:How to use terraform with environment variables in .tf file如何在 .tf 文件中使用带有环境变量的 terraform
【发布时间】:2017-09-09 13:34:27
【问题描述】:

我是 Terraform 的新手,在尝试将环境变量与 .tf 文件一起使用时遇到了一些问题,我尝试使用 terraform.tfvars / variables.tf

./terraform apply -var-file="terraform.tfvars"
Failed to load root config module: Error parsing variables.tf: At 54:17: illegal char

我在这里错过了什么?

Terraform 版本:Terraform v0.9.2

main.tf:

provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region = "${var.aws_region}"
  allowed_account_ids = ["${var.aws_account_id}"]
}

resource "aws_instance" "db" {

  ami           = "ami-49c9295"
  instance_type = "t2.micro"

  tags {
    Name = "test"
  }

  connection {
   user = "ubuntu"
  }

  security_groups = ["sg-ccc943b0"]
  availability_zone = "${var.availability_zone}"
  subnet_id = "${var.subnet_id}"
}

terraform.tfvars:

aws_profile = "default"
aws_access_key = "xxxxxx"
aws_secret_key = "xxxxxx"
aws_account_id = "xxxxxx"
key_name = "keyname"
key_path = "/home/user/.ssh/user.pem"
aws_region = "us-east-1"
subnet_id = "subnet-51997e7a"
vpc_security_group_ids = "mysql"
instance_type = "t2.xlarge"
availability_zone = "us-east-1a"

变量.tf:

variable "key_name" {
    description = "Name of the SSH keypair to use in AWS."
    default     = "keypairname"
}

variable "key_path" {
    description = "Path to the private portion of the SSH key specified."
    default     = "/home/user/.ssh/mypem.pem"
}

variable "aws_region" {
    description = "AWS region to launch servers."
    default     = "us-east-1"
}

variable "aws_access_key" {
    decscription = "AWS Access Key"
    default     = "xxxxxx"
}

variable "aws_secret_key" {
    description = "AWS Secret Key"
    default     = "xxxxxx"
}

variable "aws_account_id" {
    description = "AWS Account ID"
    default     = "xxxxxx"
}

variable "subnet_id" {
    description = "Subnet ID to use in VPC"
    default     = "subnet-51997e7a"
}

variable "vpc_security_group_ids" {
    description = "vpc_security_group_ids"
    default     = "sec"
}

variable "instance_type" {
    description = "Instance type"
    default     = "t2.xlarge"
}

variable "instance_name" {
    description = "Instance Name"
    default     = "test"
}

variable "availability_zone" {
    description = "availability_zone"
    default     = "us-east-1a"
}

variable "aws_amis" {
    default = {
        "us-east-1": "ami-49c9295f",
        "eu-west-1": "ami-49c9295f",
        "us-west-1": "ami-49c9295f",
        "us-west-2": "ami-49c9295f"
    }
}

更新

variables.tf 中删除variable "aws_amis" 部分后,我遇到了另一个问题:

Failed to load root config module: Error loading variables.tf: 1 error(s) occurred:
* variable[aws_access_key]: invalid key: decscription

【问题讨论】:

  • 检查 variables.tf 文件第 54 行是否是 availability_zone 变量的右括号,该变量没有 17 个字符 - 它只是一个右括号。您提供的文件中是否缺少某些内容?
  • 你的aws_amis 地图在我看来也是错误的。它应该看起来像 us-east1 = "ami-49c9295f" 等等,而不是 "us-east-1": "ami-49c9295f",。虽然 HCL 是 JSON 的超集,所以我总是对在其中可以使用 JSON 的位置感到困惑。
  • @ydaetskcoR 谢谢,我现在有另一个问题,请查看问题更新。
  • 你刚刚打错了。描述,而不是描述
  • @ydaetskcoR,对,我错过了 :) 我还将 vpc 安全组更改为列表,所以如果您想写关于 aws_amis 地图的答案,我会将您的答案标记为已接受,谢谢你的帮助。

标签: amazon-web-services terraform


【解决方案1】:

用作查找图的aws_amis 变量在我看来格式不正确。相反,它可能应该是以下格式:

variable "aws_amis" {
    default = {
        us-east-1 = "ami-49c9295f"
        eu-west-1 = "ami-49c9295f"
        us-west-1 = "ami-49c9295f"
        us-west-2 = "ami-49c9295f"
    }
}

顺便说一句,Terraform 默认会查找 terraform.tfvars 文件,因此您可以删除 -var-file="terraform.tfvars"。如果您想使用不同名称的文件(例如 prod.tfvars),则需要传递 -var-file 选项,但您可以省略它。

【讨论】:

    猜你喜欢
    • 2020-03-10
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2018-12-11
    • 2021-11-08
    • 2019-04-01
    • 2022-08-19
    • 2019-07-29
    相关资源
    最近更新 更多