-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.tf
267 lines (244 loc) · 14 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Managed By : CloudDrove
# Copyright @ CloudDrove. All Right Reserved.
##-----------------------------------------------------------------------------
## Labels module callled that will be used for naming and tags.
##-----------------------------------------------------------------------------
module "labels" {
source = "clouddrove/labels/aws"
version = "1.3.0"
name = var.name
environment = var.environment
managedby = var.managedby
label_order = var.label_order
repository = var.repository
}
##-----------------------------------------------------------------------------
## Below resource will deploy new security group in aws.
##-----------------------------------------------------------------------------
resource "aws_security_group" "default" {
count = var.enable && var.new_sg ? 1 : 0
name = format("%s-sg", module.labels.id)
vpc_id = var.vpc_id
description = var.sg_description
tags = module.labels.tags
lifecycle {
create_before_destroy = true
}
}
##-----------------------------------------------------------------------------
## Below data resource is to get details of existing security group in your aws environment.
## Will be called when you provide existing security group id in 'existing_sg_id' variable.
##-----------------------------------------------------------------------------
data "aws_security_group" "existing" {
count = var.enable && var.existing_sg_id != null ? 1 : 0
id = var.existing_sg_id
vpc_id = var.vpc_id
}
##-----------------------------------------------------------------------------
## Below resource will deploy prefix list resource in aws.
##-----------------------------------------------------------------------------
resource "aws_ec2_managed_prefix_list" "prefix_list" {
count = var.enable && var.prefix_list_enabled && length(var.prefix_list_ids) < 1 ? 1 : 0
address_family = var.prefix_list_address_family
max_entries = var.max_entries
name = format("%s-prefix-list", module.labels.id)
dynamic "entry" {
for_each = var.entry
content {
cidr = lookup(entry.value, "cidr", null)
description = lookup(entry.value, "description", null)
}
}
}
##-----------------------------------------------------------------------------
## Below resource will deploy ingress security group rules for new security group created from this module.
##-----------------------------------------------------------------------------
# Security group rules with "cidr_blocks", but without "source_security_id" and "self"
resource "aws_security_group_rule" "new_sg_ingress_with_cidr_blocks" {
for_each = var.enable ? { for rule in var.new_sg_ingress_rules_with_cidr_blocks : rule.rule_count => rule } : {}
type = "ingress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = aws_security_group.default[0].id
cidr_blocks = lookup(each.value, "cidr_blocks", null)
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
description = lookup(each.value, "description", null)
}
# Security group rules with "self", but without "source_security_id" and "cidr_blocks"
resource "aws_security_group_rule" "new_sg_ingress_with_self" {
for_each = var.enable ? { for rule in var.new_sg_ingress_rules_with_self : rule.rule_count => rule } : {}
type = "ingress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = aws_security_group.default[0].id
self = lookup(each.value, "self", true)
description = lookup(each.value, "description", null)
}
# Security group rules with "source_security_id", but without "cidr_blocks" and "self"
resource "aws_security_group_rule" "new_sg_ingress_with_source_sg_id" {
for_each = var.enable ? { for rule in var.new_sg_ingress_rules_with_source_sg_id : rule.rule_count => rule } : {}
type = "ingress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
source_security_group_id = each.value.source_security_group_id
security_group_id = aws_security_group.default[0].id
description = lookup(each.value, "description", null)
}
# Security group rules with "prefix_list_ids", but without "cidr_blocks", "self" or "source_security_group_id"
resource "aws_security_group_rule" "new_sg_ingress_with_prefix_list" {
for_each = var.enable ? { for rule in var.new_sg_ingress_rules_with_prefix_list : rule.rule_count => rule } : {}
type = "ingress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = aws_security_group.default[0].id
prefix_list_ids = lookup(each.value, "prefix_list_ids", null) == null ? aws_ec2_managed_prefix_list.prefix_list[*].id : lookup(each.value, "prefix_list_ids", null)
description = lookup(each.value, "description", null)
}
##-----------------------------------------------------------------------------
## Below resource will deploy ingress security group rules for existing security group.
##-----------------------------------------------------------------------------
# Security group rules with "cidr_blocks", but without "source_security_id" and "self"
resource "aws_security_group_rule" "existing_sg_ingress_cidr_blocks" {
for_each = var.enable ? { for rule in var.existing_sg_ingress_rules_with_cidr_blocks : rule.rule_count => rule } : {}
type = "ingress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = data.aws_security_group.existing[0].id
cidr_blocks = lookup(each.value, "cidr_blocks", null)
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
description = lookup(each.value, "description", null)
}
# Security group rules with "self", but without "source_security_id" and "cidr_blocks"
resource "aws_security_group_rule" "existing_sg_ingress_with_self" {
for_each = var.enable ? { for rule in var.existing_sg_ingress_rules_with_self : rule.rule_count => rule } : {}
type = "ingress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = data.aws_security_group.existing[0].id
self = lookup(each.value, "self", true)
description = lookup(each.value, "description", null)
}
# Security group rules with "source_security_id", but without "cidr_blocks" and "self"
resource "aws_security_group_rule" "existing_sg_ingress_with_source_sg_id" {
for_each = var.enable ? { for rule in var.existing_sg_ingress_rules_with_source_sg_id : rule.rule_count => rule } : {}
type = "ingress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
source_security_group_id = each.value.source_security_group_id
security_group_id = data.aws_security_group.existing[0].id
description = lookup(each.value, "description", null)
}
# Security group rules with "prefix_list_ids", but without "cidr_blocks", "self" or "source_security_group_id"
resource "aws_security_group_rule" "existing_sg_ingress_with_prefix_list" {
for_each = var.enable ? { for rule in var.existing_sg_ingress_rules_with_prefix_list : rule.rule_count => rule } : {}
type = "ingress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = data.aws_security_group.existing[0].id
prefix_list_ids = lookup(each.value, "prefix_list_ids", null) == null ? aws_ec2_managed_prefix_list.prefix_list[*].id : lookup(each.value, "prefix_list_ids", null)
description = lookup(each.value, "description", null)
}
##-----------------------------------------------------------------------------
## Below resource will deploy egress security group rules for new security group created from this module.
##-----------------------------------------------------------------------------
# Security group rules with "cidr_blocks", but without "source_security_id" and "self"
resource "aws_security_group_rule" "new_sg_egress_with_cidr_blocks" {
for_each = var.enable ? { for rule in var.new_sg_egress_rules_with_cidr_blocks : rule.rule_count => rule } : {}
type = "egress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = aws_security_group.default[0].id
cidr_blocks = lookup(each.value, "cidr_blocks", null)
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
description = lookup(each.value, "description", null)
}
# Security group rules with "self", but without "source_security_id" and "cidr_blocks"
resource "aws_security_group_rule" "new_sg_egress_with_self" {
for_each = var.enable ? { for rule in var.new_sg_egress_rules_with_self : rule.rule_count => rule } : {}
type = "egress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = aws_security_group.default[0].id
self = lookup(each.value, "self", true)
description = lookup(each.value, "description", null)
}
# Security group rules with "source_security_id", but without "cidr_blocks" and "self"
resource "aws_security_group_rule" "new_sg_egress_with_source_sg_id" {
for_each = var.enable ? { for rule in var.new_sg_egress_rules_with_source_sg_id : rule.rule_count => rule } : {}
type = "egress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
source_security_group_id = each.value.source_security_group_id
security_group_id = aws_security_group.default[0].id
description = lookup(each.value, "description", null)
}
# Security group rules with "prefix_list_ids", but without "cidr_blocks", "self" or "source_security_group_id"
resource "aws_security_group_rule" "new_sg_egress_with_prefix_list" {
for_each = var.enable ? { for rule in var.new_sg_egress_rules_with_prefix_list : rule.rule_count => rule } : {}
type = "egress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = aws_security_group.default[0].id
prefix_list_ids = lookup(each.value, "prefix_list_ids", null) == null ? aws_ec2_managed_prefix_list.prefix_list[*].id : lookup(each.value, "prefix_list_ids", null)
description = lookup(each.value, "description", null)
}
##-----------------------------------------------------------------------------
## Below resource will deploy egress security group rules for existing security group.
##-----------------------------------------------------------------------------
# Security group rules with "cidr_blocks", but without "source_security_id" and "self"
resource "aws_security_group_rule" "existing_sg_egress_with_cidr_blocks" {
for_each = var.enable ? { for rule in var.existing_sg_egress_rules_with_cidr_blocks : rule.rule_count => rule } : {}
type = "egress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = data.aws_security_group.existing[0].id
cidr_blocks = lookup(each.value, "cidr_blocks", null)
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
description = lookup(each.value, "description", null)
}
# Security group rules with "self", but without "source_security_id" and "cidr_blocks"
resource "aws_security_group_rule" "existing_sg_egress_with_self" {
for_each = var.enable ? { for rule in var.existing_sg_egress_rules_with_self : rule.rule_count => rule } : {}
type = "egress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = data.aws_security_group.existing[0].id
self = lookup(each.value, "self", true)
description = lookup(each.value, "description", null)
}
# Security group rules with "source_security_id", but without "cidr_blocks" and "self"
resource "aws_security_group_rule" "existing_sg_egress_with_source_sg_id" {
for_each = var.enable ? { for rule in var.existing_sg_egress_rules_with_source_sg_id : rule.rule_count => rule } : {}
type = "egress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
source_security_group_id = each.value.source_security_group_id
security_group_id = data.aws_security_group.existing[0].id
description = lookup(each.value, "source_address_prefix", null)
}
# Security group rules with "prefix_list_ids", but without "cidr_blocks", "self" or "source_security_group_id"
resource "aws_security_group_rule" "existing_sg_egress_with_prefix_list" {
for_each = var.enable ? { for rule in var.existing_sg_egress_rules_with_prefix_list : rule.rule_count => rule } : {}
type = "egress"
from_port = each.value.from_port
protocol = each.value.protocol
to_port = each.value.to_port
security_group_id = data.aws_security_group.existing[0].id
prefix_list_ids = lookup(each.value, "prefix_list_ids", null) == null ? aws_ec2_managed_prefix_list.prefix_list[*].id : lookup(each.value, "prefix_list_ids", null)
description = lookup(each.value, "source_address_prefix", null)
}