【发布时间】:2026-01-13 01:15:01
【问题描述】:
我已经使用Terraforming 导出了我当前的资源,并得到了一个包含所有安全组的大文件。
问题是,在每个安全组中都有一些引用安全组 ID 的规则 - 在我计划运行 terraform 的新区域中不存在这些规则。例如:
resource "aws_security_group" "my-group" {
name = "my-group"
description = ""
vpc_id = "${var.vpc["production"]}"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = ["sg-25bee542"] <-- this ID doesnt exists in the new region i'm planning to work on
self = false
}
我已经创建了一个包含所有旧安全组的地图:
variable "security_groups" {
type = "map"
default = {
"sg-acd22fdb" = "default"
"sg-52cd3025" = "my-group"
"sg-25bee542" = "my-group2"
...
}
}
现在我正在尝试将硬编码的sg-*id* 解析为相应的安全组名称并将其插入到变量中,以便第一个示例可以这样工作:
resource "aws_security_group" "my-group" {
name = "my-group"
description = ""
vpc_id = "${var.vpc["production"]}"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = ["${aws_security_group.my-group2.id}"] <-- the 'my-group2' should be resolved from the map variable
self = false
}
类似:
resource "aws_security_group" "my-group" {
name = "my-group"
description = ""
vpc_id = "${var.vpc["production"]}"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = ["${aws_security_group.[lookup(security_groups,sg-25bee542]].id}"] <-- the 'my-group2' string should be resolved from the map variable by looking its sg ID
self = false
}
我希望我在这个问题上表达清楚......有什么想法吗?
【问题讨论】:
-
既然您已经知道要用什么替换它们,为什么不直接在编辑器中查找和替换呢?为什么要让 Terraform 这样做?
-
这将是最后的选择,要替换所有这些安全组还有很多工作。
标签: terraform