generated from rhythmictech/terraform-terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tf
99 lines (82 loc) · 3.07 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
locals {
tags = merge(
var.tags,
{
terraform_module = basename(abspath(path.module))
}
)
}
#tfsec:ignore:aws-cloudwatch-log-group-customer-key
resource "aws_cloudwatch_log_group" "vpn" {
name_prefix = "vpn-${var.name}"
retention_in_days = var.cloudwatch_log_retention_days
tags = local.tags
}
resource "aws_cloudwatch_log_stream" "vpn" {
name = "vpn-${var.name}"
log_group_name = aws_cloudwatch_log_group.vpn.name
}
resource "aws_iam_saml_provider" "this" {
count = var.saml_metadata_document != null ? 1 : 0
name = var.name
saml_metadata_document = var.saml_metadata_document
}
resource "aws_ec2_client_vpn_endpoint" "this" {
description = "Client VPN"
client_cidr_block = var.client_cidr_block
dns_servers = var.dns_servers
security_group_ids = concat([aws_security_group.this.id], var.additional_security_groups)
server_certificate_arn = var.server_certificate_arn
split_tunnel = var.split_tunnel_enabled
vpc_id = var.vpc_id
tags = local.tags
authentication_options {
type = "federated-authentication"
saml_provider_arn = try(aws_iam_saml_provider.this[0].arn, var.saml_provider_arn)
}
connection_log_options {
enabled = true
cloudwatch_log_group = aws_cloudwatch_log_group.vpn.name
cloudwatch_log_stream = aws_cloudwatch_log_stream.vpn.name
}
}
resource "aws_security_group" "this" {
name_prefix = var.name
description = "Client VPN network associations"
tags = var.tags
vpc_id = var.vpc_id
ingress {
description = "Allow self access only by default"
from_port = 0
protocol = -1
self = true
to_port = 0
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
}
resource "aws_ec2_client_vpn_network_association" "this" {
for_each = toset(var.associated_subnets) #avoid ordering errors by using a for_each instead of count
client_vpn_endpoint_id = aws_ec2_client_vpn_endpoint.this.id
subnet_id = each.key
}
resource "aws_ec2_client_vpn_authorization_rule" "rules" {
count = length(var.authorization_rules)
access_group_id = var.authorization_rules[count.index].access_group_id
authorize_all_groups = var.authorization_rules[count.index].authorize_all_groups
client_vpn_endpoint_id = aws_ec2_client_vpn_endpoint.this.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" "additional" {
count = length(var.additional_routes)
description = try(var.additional_routes[count.index].description, null)
destination_cidr_block = var.additional_routes[count.index].destination_cidr_block
client_vpn_endpoint_id = aws_ec2_client_vpn_endpoint.this.id
target_vpc_subnet_id = var.additional_routes[count.index].target_vpc_subnet_id
}