generated from cloudposse/terraform-example-module
-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.tf
280 lines (213 loc) · 8.5 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
268
269
270
271
272
273
274
275
276
277
278
279
280
locals {
enabled = module.this.enabled
security_group_enabled = local.enabled && var.create_security_group
mutual_enabled = local.enabled && var.authentication_type == "certificate-authentication"
federated_enabled = local.enabled && var.authentication_type == "federated-authentication"
self_service_portal_enabled = local.federated_enabled && var.self_service_portal_enabled
logging_enabled = local.enabled && var.logging_enabled
export_client_certificate = local.mutual_enabled && var.export_client_certificate
certificate_backends = ["ACM", "SSM"]
saml_provider_arn = local.federated_enabled ? try(aws_iam_saml_provider.default[0].arn, var.saml_provider_arn) : null
root_certificate_chain_arn = local.mutual_enabled ? module.self_signed_cert_root.certificate_arn : null
self_service_saml_provider_arn = local.self_service_portal_enabled ? var.self_service_saml_provider_arn : null
cloudwatch_log_group = local.logging_enabled ? module.cloudwatch_log.log_group_name : null
cloudwatch_log_stream = local.logging_enabled ? var.logging_stream_name : null
ca_common_name = var.ca_common_name != null ? var.ca_common_name : "${module.this.id}.vpn.ca"
root_common_name = var.root_common_name != null ? var.root_common_name : "${module.this.id}.vpn.client"
server_common_name = var.server_common_name != null ? var.server_common_name : "${module.this.id}.vpn.server"
client_conf_tmpl_path = var.client_conf_tmpl_path == null ? "${path.module}/templates/client-config.ovpn.tpl" : var.client_conf_tmpl_path
}
module "self_signed_cert_ca" {
source = "cloudposse/ssm-tls-self-signed-cert/aws"
version = "1.3.0"
attributes = ["self", "signed", "cert", "ca"]
secret_path_format = var.secret_path_format
subject = {
common_name = local.ca_common_name
organization = var.organization_name
}
basic_constraints = {
ca = true
}
allowed_uses = [
"crl_signing",
"cert_signing",
]
certificate_backends = ["SSM"]
context = module.this.context
}
data "aws_ssm_parameter" "ca_key" {
count = local.enabled ? 1 : 0
name = module.self_signed_cert_ca.certificate_key_path
depends_on = [
module.self_signed_cert_ca
]
}
module "self_signed_cert_root" {
source = "cloudposse/ssm-tls-self-signed-cert/aws"
version = "1.3.0"
attributes = ["self", "signed", "cert", "root"]
secret_path_format = var.secret_path_format
enabled = local.mutual_enabled
subject = {
common_name = local.root_common_name
organization = var.organization_name
}
basic_constraints = {
ca = false
}
allowed_uses = [
"key_encipherment",
"digital_signature",
"client_auth",
]
certificate_backends = local.certificate_backends
use_locally_signed = true
certificate_chain = {
cert_pem = module.self_signed_cert_ca.certificate_pem,
private_key_pem = join("", data.aws_ssm_parameter.ca_key[*].value)
}
context = module.this.context
}
module "self_signed_cert_server" {
source = "cloudposse/ssm-tls-self-signed-cert/aws"
version = "1.3.0"
attributes = ["self", "signed", "cert", "server"]
secret_path_format = var.secret_path_format
subject = {
common_name = local.server_common_name
organization = var.organization_name
}
basic_constraints = {
ca = false
}
allowed_uses = [
"key_encipherment",
"digital_signature",
"server_auth"
]
certificate_backends = local.certificate_backends
use_locally_signed = true
certificate_chain = {
cert_pem = module.self_signed_cert_ca.certificate_pem,
private_key_pem = join("", data.aws_ssm_parameter.ca_key[*].value)
}
context = module.this.context
}
module "cloudwatch_log" {
source = "cloudposse/cloudwatch-logs/aws"
version = "0.6.8"
enabled = local.logging_enabled
stream_names = [var.logging_stream_name]
retention_in_days = var.retention_in_days
context = module.this.context
}
resource "aws_iam_saml_provider" "default" {
count = local.enabled && var.saml_metadata_document != null ? 1 : 0
name = module.this.id
saml_metadata_document = var.saml_metadata_document
tags = module.this.tags
}
resource "aws_ec2_client_vpn_endpoint" "default" {
count = local.enabled ? 1 : 0
description = module.this.id
server_certificate_arn = module.self_signed_cert_server.certificate_arn
client_cidr_block = var.client_cidr
self_service_portal = local.self_service_portal_enabled ? "enabled" : "disabled"
transport_protocol = var.transport_protocol
authentication_options {
type = var.authentication_type
saml_provider_arn = local.saml_provider_arn
root_certificate_chain_arn = local.root_certificate_chain_arn
self_service_saml_provider_arn = local.self_service_saml_provider_arn
}
connection_log_options {
enabled = var.logging_enabled
cloudwatch_log_group = local.cloudwatch_log_group
cloudwatch_log_stream = local.cloudwatch_log_stream
}
dns_servers = var.dns_servers
split_tunnel = var.split_tunnel
session_timeout_hours = var.session_timeout_hours
tags = module.this.tags
depends_on = [
module.self_signed_cert_server,
module.self_signed_cert_root,
]
security_group_ids = compact(concat(
[module.vpn_security_group.id],
local.associated_security_group_ids
))
vpc_id = var.vpc_id
}
module "vpn_security_group" {
source = "cloudposse/security-group/aws"
version = "2.2.0"
enabled = local.security_group_enabled
security_group_name = var.security_group_name
create_before_destroy = var.security_group_create_before_destroy
security_group_create_timeout = var.security_group_create_timeout
security_group_delete_timeout = var.security_group_delete_timeout
security_group_description = var.security_group_description
allow_all_egress = true
rules = var.additional_security_group_rules
rule_matrix = [
{
cidr_blocks = var.allowed_cidr_blocks
ipv6_cidr_blocks = var.allowed_ipv6_cidr_blocks
prefix_list_ids = var.allowed_ipv6_prefix_list_ids
source_security_group_ids = var.allowed_security_group_ids
self = var.allow_self_security_group
rules = [{
key = "vpn-self"
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
description = "Allow all ingress from designated sources"
}]
}
]
vpc_id = var.vpc_id
context = module.this.context
}
resource "aws_ec2_client_vpn_network_association" "default" {
count = local.enabled ? length(var.associated_subnets) : 0
client_vpn_endpoint_id = join("", aws_ec2_client_vpn_endpoint.default[*].id)
subnet_id = var.associated_subnets[count.index]
}
resource "aws_ec2_client_vpn_authorization_rule" "default" {
count = local.enabled ? length(var.authorization_rules) : 0
access_group_id = lookup(var.authorization_rules[count.index], "access_group_id", null)
authorize_all_groups = lookup(var.authorization_rules[count.index], "authorize_all_groups", null)
client_vpn_endpoint_id = join("", aws_ec2_client_vpn_endpoint.default[*].id)
description = var.authorization_rules[count.index].description
target_network_cidr = var.authorization_rules[count.index].target_network_cidr
}
resource "aws_ec2_client_vpn_route" "default" {
count = local.enabled ? length(var.additional_routes) : 0
description = try(var.additional_routes[count.index].description, null)
destination_cidr_block = var.additional_routes[count.index].destination_cidr_block
client_vpn_endpoint_id = join("", aws_ec2_client_vpn_endpoint.default[*].id)
target_vpc_subnet_id = var.additional_routes[count.index].target_vpc_subnet_id
depends_on = [
aws_ec2_client_vpn_network_association.default
]
timeouts {
create = "5m"
delete = "5m"
}
}
data "awsutils_ec2_client_vpn_export_client_config" "default" {
count = local.enabled ? 1 : 0
id = join("", aws_ec2_client_vpn_endpoint.default[*].id)
}
data "aws_ssm_parameter" "root_key" {
count = local.export_client_certificate ? 1 : 0
name = module.self_signed_cert_root.certificate_key_path
# Necessary to retrieve the ssm parameter after the module is created
# The implicit output in the name isn't enough.
depends_on = [
module.self_signed_cert_root
]
}