?? Simplify AWS Security Group Rules with Dynamic Blocks in Terraform! ??
Abhishek kumar
Serving Notice Period || AWS || DevOps || Jenkins || Docker || Kubernetes || Terraform || Argo CD || Shell Scripting
?? Simplify AWS Security Group Rules with Dynamic Blocks in Terraform! ??
Tired of writing multiple Ingress rules manually for each port? ?? Let Terraform’s dynamic blocks do the heavy lifting for you! ??
In Terraform, a dynamic block allows you to generate multiple nested blocks dynamically. Instead of manually repeating similar blocks, dynamic blocks let you loop through lists or maps to create resources more efficiently. ??
领英推荐
provider "aws" {
region = "ap-south-1"
}
variable "sg-ports" {
type = list(number)
default = [22, 8080, 443]
description = "list of ingress ports"
}
resource "aws_security_group" "dynamicsg" {
name = "dynamic-sg"
description = "Ingress for Vault"
dynamic "ingress" {
for_each = var.sg-ports
content {
from_port = ingress.value
protocol = "tcp"
to_port = ingress.value
cidr_blocks = ["0.0.0.0/0"]
}
}
}
?? How does this work? With for_each, Terraform automatically loops through a list of ports (in this case 22, 8080, 443) and creates corresponding Ingress rules. No more repetitive code or manual errors! ??
?? Benefits: ? Cleaner code ? Easy scalability ? Less time configuring, more time innovating
?? When & Why to Use Dynamic Blocks?