是否可以在 terraform 中将对象定义为 AWS 中资源的变量?

Is it possible in terrafom to define an object as variable for resource in AWS?

我的config.tf

 variable "tag_for_ec2" {

  description = "Tags for ec2"
   type        = any
 }

 

 resource "aws_instance" "terraform-ec2" {
 ami           = "ami-02f26adf094f51167"
 instance_type = "t2.micro"
 tags          = var.tag_for_ec2.Name
 }

以上是通过 terraform 部署 ec2 的配置语法,我正在尝试 标签属性的整个对象。 我知道 terraform 文档中的标签是这样写的

tags = {
     Name = "Created by Terraform"
   }

terraform.vars 文件

tag_for_ec2 = {
    Name = "created by jatin/terraform"
}

在 运行 terraform 应用时出现此错误

Inappropriate value for attribute "tags": map of string required.

tags是地图,你的var.tag_for_ec2已经是地图了。所以应该是:

resource "aws_instance" "terraform-ec2" {
   ami           = "ami-02f26adf094f51167"
   instance_type = "t2.micro"
   tags          = var.tag_for_ec2
 }