Terraform 可以添加单独的 Digital Ocean 防火墙规则吗?
Can Terraform add individual Digital Ocean firewall rules?
我正在构建 EC2 实例和 DO droplet 的混合体,它们必须能够以全网状方式进行通信,这需要 AWS 和 DO 的防火墙配置才能了解所有已创建资源的 WAN 和 LAN 地址.每个平台的资源数量是可变的。
将 Terraform 与 AWS 提供商一起使用时,我可以创建一个安全组,然后向其中添加单独的规则:
resource "aws_security_group" "sg-1" {
ingress {
...
}
egress {
...
}
}
resource "aws_security_group_rule" "sg1-rule-1" {
security_group_id = aws_security_group.sg-1.id
...
}
DigitalOcean 提供商提供 digitalocean_firewall
资源,但与 aws_security_group_rule
资源没有任何相似之处,这意味着必须提前了解整个防火墙规则集并在 digitalocean_firewall
资源中应用块。
到目前为止,我唯一的解决方案是在创建资源后从模板动态创建 digitalocean_firewall.tf 文件,但这需要 second terraform apply
应用动态规则。
在没有digitalocean_firewall_rule
资源的情况下,有没有更好的方法?
回答我自己的问题!
解决方案是使用 Terraform 的 dynamic
块资源:
resource "digitalocean_firewall" "firewall" {
name = "${var.project_name}-${var.environment}-firewall"
inbound_rule {
protocol = "tcp"
port_range = "22"
source_addresses = ["x.x.x.x/32"]
}
dynamic "inbound_rule" {
for_each = module.ec2_node
content {
protocol = "tcp"
port_range = "1-65535"
source_addresses = [inbound_rule.value.public_ip]
}
}
dynamic "inbound_rule" {
for_each = module.ec2_node
content {
protocol = "udp"
port_range = "1-65535"
source_addresses = [inbound_rule.value.public_ip]
}
}
}
我正在构建 EC2 实例和 DO droplet 的混合体,它们必须能够以全网状方式进行通信,这需要 AWS 和 DO 的防火墙配置才能了解所有已创建资源的 WAN 和 LAN 地址.每个平台的资源数量是可变的。
将 Terraform 与 AWS 提供商一起使用时,我可以创建一个安全组,然后向其中添加单独的规则:
resource "aws_security_group" "sg-1" {
ingress {
...
}
egress {
...
}
}
resource "aws_security_group_rule" "sg1-rule-1" {
security_group_id = aws_security_group.sg-1.id
...
}
DigitalOcean 提供商提供 digitalocean_firewall
资源,但与 aws_security_group_rule
资源没有任何相似之处,这意味着必须提前了解整个防火墙规则集并在 digitalocean_firewall
资源中应用块。
到目前为止,我唯一的解决方案是在创建资源后从模板动态创建 digitalocean_firewall.tf 文件,但这需要 second terraform apply
应用动态规则。
在没有digitalocean_firewall_rule
资源的情况下,有没有更好的方法?
回答我自己的问题!
解决方案是使用 Terraform 的 dynamic
块资源:
resource "digitalocean_firewall" "firewall" {
name = "${var.project_name}-${var.environment}-firewall"
inbound_rule {
protocol = "tcp"
port_range = "22"
source_addresses = ["x.x.x.x/32"]
}
dynamic "inbound_rule" {
for_each = module.ec2_node
content {
protocol = "tcp"
port_range = "1-65535"
source_addresses = [inbound_rule.value.public_ip]
}
}
dynamic "inbound_rule" {
for_each = module.ec2_node
content {
protocol = "udp"
port_range = "1-65535"
source_addresses = [inbound_rule.value.public_ip]
}
}
}