forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity-groups-dynamic.tf
More file actions
52 lines (46 loc) · 1.68 KB
/
Copy pathsecurity-groups-dynamic.tf
File metadata and controls
52 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Terraform Dynamic Blocks — DRY security group and tag configurations
# ------------------------------------------------------------------
# Reusable locals for ingress rules (replaces repetitive ingress blocks)
# ------------------------------------------------------------------
locals {
# Define all ingress rules as a list — dynamic block iterates over this
app_ingress_rules = [
{ port = 3000, description = "Backend API", source_sg = "alb" },
{ port = 8080, description = "Health endpoint", source_sg = "alb" },
{ port = 9090, description = "Metrics scrape", source_sg = "monitoring" },
]
# Tag map applied to all resources via dynamic block
resource_tags = merge(
local.common_tags,
{
ManagedBy = "terraform"
UpdatedAt = timestamp()
}
)
}
# ------------------------------------------------------------------
# Security group using dynamic ingress blocks (replaces repeated ingress{} stanzas)
# ------------------------------------------------------------------
resource "aws_security_group" "app_dynamic" {
name = "${var.project_name}-${var.environment}-app-dynamic-sg"
description = "Application SG managed with dynamic blocks"
vpc_id = var.vpc_id
dynamic "ingress" {
for_each = local.app_ingress_rules
content {
from_port = ingress.value.port
to_port = ingress.value.port
protocol = "tcp"
security_groups = [aws_security_group.alb.id]
description = ingress.value.description
}
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow all outbound"
}
tags = local.common_tags
}