【问题标题】:Terraform data structures: object and list of object issueTerraform 数据结构:对象和对象列表
【发布时间】:2020-09-27 18:52:38
【问题描述】:

我正在尝试使用 terraform 创建一些 aws 资源。 这是我的问题:

我正在创建一些变量,所以我可以从资源中访问它们。 以下是 variables.tf 文件内容:

variables.tf

variable "zones" {
  type = "list"
  default = ["us-east-1a", "us-east-1b"]
}

variable "init" {
    type = object({
                    vpc-id=list(string),
                    public-subnet=string, 
                    aws_region=string, 
                    ami=string,
                    vpc-sec-group= list(string)
            })

    param = {
        vpc-id = ["vpc-1111111"]
        public-subnet = "subnet-98e4567"
        aws_region = "${element(var.zones,0)}"
        ami = "ami-09d95fab7fff3776c",
        vpc-sec-group = ["sg-d60bf3f5"]
    }
}


variable "instances" {
    type = list(object({ type=string, count=string, tags=map(string) }))
    t2-micro ={
        type = "t2.micro"
        count = 4
        tags = { Name = "Test T2"}
    }
    m4-large ={
        type = "m4-large"
        count = 2
        tags = { Name = "Test M4"}
    }
}

我的计划是使用这些变量来创建一些 ec2 实例 这是 ec2.tf

ec2.tf

resource "aws_instance" "Test-T2" {
    type   = lookup(var.insts["t2-micro"],"type")
    ami = lookup(var.init.ami["ami"],var.init.aws_region["aws_region"] )
    count  = lookup(var.insts["t2-micro"],"count")
    tags = lookup(var.insts["t2-micro"],"tags")
    key_name = aws_key_pair.terraform-demo.key_name
    vpc_security_group_ids = toset(lookup(var.init, "vpc-sec-group"))
    subnet_id = lookup(var.init.params["public-subnet"])
}

问题

当我执行时

terraform init

我收到以下错误:

Error: Unsupported argument

  on variables.tf line 26, in variable "instances":
  26:     t2-micro ={

An argument named "t2-micro" is not expected here.


Error: Unsupported argument

  on variables.tf line 32, in variable "instances":
  32:     m4-large ={

An argument named "m4-large" is not expected here.


Terraform has initialized, but configuration upgrades may be needed.

Terraform found syntax errors in the configuration that prevented full
initialization. If you've recently upgraded to Terraform v0.12, this may be
because your configuration uses syntax constructs that are no longer valid,
and so must be updated before full initialization is possible.

有人可以帮我纠正这些错误吗?

更多细节和我采取的一些行动

据我所知,我尝试了不同的方法来创建变量并遵循 Terraform 文档 无济于事。

我只是在模拟 Python:

  • 对象列表(字典列表) 和
  • 一个对象

这是我的 terraform 版本

terraform  -v                                                        
Terraform v0.12.26
+ provider.aws v2.65.0

更多细节

我将最新版本的 Visual Studio Code 1.45.1 与 Terraform 模块 HashiCop 1.4.0 一起用于“Hashicorp 的 Terraform 的语法突出显示、linting、格式化和验证”

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws terraform0.12+


    【解决方案1】:

    您需要将变量声明和赋值分开。它们不能在同一个区块内共存。

    您在variables.tf 中的声明看起来像(带有一些修复和清理):

    variable "instances" {
      type = list(object({
        type  = string # removed commas since you specified object type
        count = number # fixed from string type
        tags  = map(string)
      }))
    }
    

    您的变量赋值应该移动到.tfvars 文件中。通常这个文件会被命名为terraform.tfvars:

    instances = [ # you specified a list so we add the proper syntax here
      { # you specified an object, so we remove the keys and retain the values
        type  = "t2.micro"
        count = 4
        tags  = { "Name" = "Test T2"} # you specified map(string) so Name becomes string
      },
      {
        type  = "m4-large"
        count = 2
        tags  = { "Name" = "Test M4"}
      }
    }]
    

    这将是一个有效的输入变量,并根据您的声明进行正确分配。这将解决您的错误。

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 2011-04-24
      • 2010-12-16
      • 2020-12-27
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多