Syntax 如何使用TF_VAR_名称在terraform中使用环境变量?
我试图导出列表变量,并通过TF_VAR_name使用它们,在将它们与Syntax 如何使用TF_VAR_名称在terraform中使用环境变量?,syntax,terraform,interpolation,Syntax,Terraform,Interpolation,我试图导出列表变量,并通过TF_VAR_name使用它们,在将它们与toset函数组合时出错 成功场景: 地形应用-自动批准 # Variables variable "sg_name" { default = ["SG1", "SG2", "SG3", "SG4", "SG5"] } variable "Project"
toset
函数组合时出错
成功场景:
地形应用-自动批准
# Variables
variable "sg_name" { default = ["SG1", "SG2", "SG3", "SG4", "SG5"] }
variable "Project" { default = "POC" }
variable "Owner" { default = "Me" }
variable "Environment" { default = "Testing" }
locals {
common_tags = {
Project = var.Project
Owner = var.Owner
Environment = var.Environment
}
}
# Create Security Group
resource "aws_security_group" "application_sg" {
for_each = toset(var.sg_name)
name = each.value
description = "${each.value} security group"
tags = merge(local.common_tags, { "Name" = each.value })
}
# Output the SG IDs
output "sg_id" {
value = values(aws_security_group.application_sg)[*].id
}
# Variables
variable "sg_name" { }
variable "Project" { default = "POC" }
variable "Owner" { default = "Me" }
variable "Environment" { default = "Testing" }
locals {
common_tags = {
Project = var.Project
Owner = var.Owner
Environment = var.Environment
}
}
# Create Security Group
resource "aws_security_group" "application_sg" {
for_each = toset(var.sg_name)
name = each.value
description = "${each.value} security group"
tags = merge(local.common_tags, { "Name" = each.value })
}
# Output the SG IDs
output "sg_id" {
value = values(aws_security_group.application_sg)[*].id
}
故障场景:
TF_VAR_sg_name='[“SG1”、“SG2”、“SG3”、“SG4”、“SG5”]'地形应用-自动批准
# Variables
variable "sg_name" { default = ["SG1", "SG2", "SG3", "SG4", "SG5"] }
variable "Project" { default = "POC" }
variable "Owner" { default = "Me" }
variable "Environment" { default = "Testing" }
locals {
common_tags = {
Project = var.Project
Owner = var.Owner
Environment = var.Environment
}
}
# Create Security Group
resource "aws_security_group" "application_sg" {
for_each = toset(var.sg_name)
name = each.value
description = "${each.value} security group"
tags = merge(local.common_tags, { "Name" = each.value })
}
# Output the SG IDs
output "sg_id" {
value = values(aws_security_group.application_sg)[*].id
}
# Variables
variable "sg_name" { }
variable "Project" { default = "POC" }
variable "Owner" { default = "Me" }
variable "Environment" { default = "Testing" }
locals {
common_tags = {
Project = var.Project
Owner = var.Owner
Environment = var.Environment
}
}
# Create Security Group
resource "aws_security_group" "application_sg" {
for_each = toset(var.sg_name)
name = each.value
description = "${each.value} security group"
tags = merge(local.common_tags, { "Name" = each.value })
}
# Output the SG IDs
output "sg_id" {
value = values(aws_security_group.application_sg)[*].id
}
错误
Error: Invalid function argument
on main.tf line 16, in resource "aws_security_group" "application_sg":
16: for_each = toset(var.sg_name)
|----------------
| var.sg_name is "[\"SG1\", \"SG2\", \"SG3\", \"SG4\", \"SG5\"]"
Invalid value for "v" parameter: cannot convert string to set of any single
type.
您需要指定变量的类型(即,在您的情况下,
type=list(string)
),然后它就可以工作了
我使用以下配置对其进行了测试:
variable "sg_name" {
type = list(string)
}
resource "null_resource" "application_sg" {
for_each = toset(var.sg_name)
triggers = {
name = each.key
}
}
然后一个TF_VAR_sg_name='[“SG1”、“SG2”、“SG3”、“SG4”、“SG5”]”地形应用
如果我删除
type=list(string)
它会像您所说的那样出错。您需要指定变量的类型(即在您的情况下type=list(string)
),那么它应该可以工作
我使用以下配置对其进行了测试:
variable "sg_name" {
type = list(string)
}
resource "null_resource" "application_sg" {
for_each = toset(var.sg_name)
triggers = {
name = each.key
}
}
然后一个TF_VAR_sg_name='[“SG1”、“SG2”、“SG3”、“SG4”、“SG5”]”地形应用
如果我删除type=list(string)
它就会像你说的那样出错