-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathmain.tf
112 lines (90 loc) · 4.35 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
################################################################################
# Endpoint(s)
################################################################################
locals {
endpoints = { for k, v in var.endpoints : k => v if var.create && try(v.create, true) }
security_group_ids = var.create && var.create_security_group ? concat(var.security_group_ids, [aws_security_group.this[0].id]) : var.security_group_ids
}
data "aws_vpc_endpoint_service" "this" {
for_each = local.endpoints
service = try(each.value.service, null)
service_name = try(each.value.service_name, null)
service_regions = try(coalescelist(compact([each.value.service_region])), null)
filter {
name = "service-type"
values = [try(each.value.service_type, "Interface")]
}
}
resource "aws_vpc_endpoint" "this" {
for_each = local.endpoints
vpc_id = var.vpc_id
service_name = try(each.value.service_endpoint, data.aws_vpc_endpoint_service.this[each.key].service_name)
service_region = try(each.value.service_region, null)
vpc_endpoint_type = try(each.value.service_type, "Interface")
auto_accept = try(each.value.auto_accept, null)
security_group_ids = try(each.value.service_type, "Interface") == "Interface" ? length(distinct(concat(local.security_group_ids, lookup(each.value, "security_group_ids", [])))) > 0 ? distinct(concat(local.security_group_ids, lookup(each.value, "security_group_ids", []))) : null : null
subnet_ids = try(each.value.service_type, "Interface") == "Interface" ? distinct(concat(var.subnet_ids, lookup(each.value, "subnet_ids", []))) : null
route_table_ids = try(each.value.service_type, "Interface") == "Gateway" ? lookup(each.value, "route_table_ids", null) : null
policy = try(each.value.policy, null)
private_dns_enabled = try(each.value.service_type, "Interface") == "Interface" ? try(each.value.private_dns_enabled, null) : null
ip_address_type = try(each.value.ip_address_type, null)
dynamic "dns_options" {
for_each = try([each.value.dns_options], [])
content {
dns_record_ip_type = try(dns_options.value.dns_options.dns_record_ip_type, null)
private_dns_only_for_inbound_resolver_endpoint = try(dns_options.value.private_dns_only_for_inbound_resolver_endpoint, null)
}
}
dynamic "subnet_configuration" {
for_each = try(each.value.subnet_configurations, [])
content {
ipv4 = try(subnet_configuration.value.ipv4, null)
ipv6 = try(subnet_configuration.value.ipv6, null)
subnet_id = try(subnet_configuration.value.subnet_id, null)
}
}
tags = merge(
var.tags,
{ "Name" = replace(each.key, ".", "-") },
try(each.value.tags, {}),
)
timeouts {
create = try(var.timeouts.create, "10m")
update = try(var.timeouts.update, "10m")
delete = try(var.timeouts.delete, "10m")
}
}
################################################################################
# Security Group
################################################################################
resource "aws_security_group" "this" {
count = var.create && var.create_security_group ? 1 : 0
name = var.security_group_name
name_prefix = var.security_group_name_prefix
description = var.security_group_description
vpc_id = var.vpc_id
tags = merge(
var.tags,
var.security_group_tags,
{ "Name" = try(coalesce(var.security_group_name, var.security_group_name_prefix), "") },
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "this" {
for_each = { for k, v in var.security_group_rules : k => v if var.create && var.create_security_group }
# Required
security_group_id = aws_security_group.this[0].id
protocol = try(each.value.protocol, "tcp")
from_port = try(each.value.from_port, 443)
to_port = try(each.value.to_port, 443)
type = try(each.value.type, "ingress")
# Optional
description = try(each.value.description, null)
cidr_blocks = lookup(each.value, "cidr_blocks", null)
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
prefix_list_ids = lookup(each.value, "prefix_list_ids", null)
self = try(each.value.self, null)
source_security_group_id = lookup(each.value, "source_security_group_id", null)
}