forked from terraform-aws-modules/terraform-aws-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
95 lines (75 loc) · 3.75 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
################################################################################
# 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)
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 = data.aws_vpc_endpoint_service.this[each.key].service_name
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
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)
}
}
tags = merge(var.tags, 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)
}