-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsg.tf
56 lines (45 loc) · 2.07 KB
/
nsg.tf
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
53
54
55
56
resource "aws_security_group" "nsg_lb" {
name = "${var.app}-${var.environment}-lb"
description = "Allow connections from external resources while limiting connections from ${var.app}-${var.environment}-lb to internal resources"
vpc_id = var.vpc
tags = var.tags
}
resource "aws_security_group" "nsg_task" {
name = "${var.app}-${var.environment}-task"
description = "Limit connections from internal resources while allowing ${var.app}-${var.environment}-task to connect to all external resources"
vpc_id = var.vpc
tags = var.tags
}
# Rules for the LB (Targets the task SG)
resource "aws_security_group_rule" "nsg_lb_egress_rule" {
description = "Only allow SG ${var.app}-${var.environment}-lb to connect to ${var.app}-${var.environment}-task on port ${var.container_port}"
type = "egress"
from_port = var.container_port
to_port = var.container_port
protocol = "tcp"
source_security_group_id = aws_security_group.nsg_task.id
security_group_id = aws_security_group.nsg_lb.id
}
# Rules for the TASK (Targets the LB SG)
resource "aws_security_group_rule" "nsg_task_ingress_rule" {
description = "Only allow connections from SG ${var.app}-${var.environment}-lb on port ${var.container_port}"
type = "ingress"
from_port = var.container_port
to_port = var.container_port
protocol = "tcp"
source_security_group_id = aws_security_group.nsg_lb.id
security_group_id = aws_security_group.nsg_task.id
}
resource "aws_security_group_rule" "nsg_task_egress_rule" {
description = "Allows task to establish connections to all resources"
type = "egress"
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.nsg_task.id
}
# This is the network security group id (sg-blah) for the ALB. This could be useful if you needed to directly add new rules
output "alb_nsg_id" {
value=aws_security_group.nsg_lb.id
}