-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
96 lines (76 loc) · 1.76 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
resource "aws_vpc" "main" {
cidr_block = var.cidr_block
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "${var.name}_vpc"
}
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
tags = {
Name = "${var.name}_public_subnet"
}
cidr_block = var.cidr_block
map_public_ip_on_launch = var.auto_public_ip
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "${var.name}_internet_gateway"
}
}
resource "aws_default_security_group" "default" {
vpc_id = aws_vpc.main.id
}
locals {
disabled_rule = {
enabled = false
description = ""
from_port = 0
to_port = 0
protocol = "tcp"
cidr_blocks = []
}
sg_ingress_list = {
ssh = var.enable_ssh ? {
enabled = true
description = "SSH into VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
} : local.disabled_rule
}
}
resource "aws_security_group" "ec2" {
vpc_id = aws_vpc.main.id
dynamic "ingress" {
for_each = [for i in local.sg_ingress_list :
{
enabled = i.enabled
description = i.description
from_port = i.from_port
to_port = i.to_port
protocol = i.protocol
cidr_blocks = i.cidr_blocks
} if i.enabled == true
]
content {
description = ingress.value.description
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
}
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.name}_ec2_sg"
}
}