forked from jmhale/terraform-aws-wireguard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
101 lines (90 loc) · 3.3 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
data "template_file" "user_data" {
template = file("${path.module}/templates/user-data.txt")
vars = {
wg_server_private_key = data.aws_ssm_parameter.wg_server_private_key.value
wg_server_net = var.wg_server_net
wg_server_port = var.wg_server_port
peers = join("\n", data.template_file.wg_client_data_json.*.rendered)
eip_id = var.eip_id
wg_server_interface = var.wg_server_interface
}
}
data "template_file" "wg_client_data_json" {
template = file("${path.module}/templates/client-data.tpl")
count = length(var.wg_client_public_keys)
vars = {
client_pub_key = element(values(var.wg_client_public_keys[count.index]), 0)
client_ip = element(keys(var.wg_client_public_keys[count.index]), 0)
persistent_keepalive = var.wg_persistent_keepalive
}
}
# We're using ubuntu images - this lets us grab the latest image for our region from Canonical
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-*-16.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
# turn the sg into a sorted list of string
locals {
sg_wireguard_external = sort([aws_security_group.sg_wireguard_external.id])
}
# clean up and concat the above wireguard default sg with the additional_security_group_ids
locals {
security_groups_ids = compact(concat(var.additional_security_group_ids, local.sg_wireguard_external))
}
resource "aws_launch_configuration" "wireguard_launch_config" {
name_prefix = "wireguard-${var.env}-"
image_id = var.ami_id == null ? data.aws_ami.ubuntu.id : var.ami_id
instance_type = var.instance_type
key_name = var.ssh_key_id
iam_instance_profile = (var.eip_id != "disabled" ? aws_iam_instance_profile.wireguard_profile[0].name : null)
user_data = data.template_file.user_data.rendered
security_groups = local.security_groups_ids
associate_public_ip_address = (var.eip_id != "disabled" ? true : false)
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "wireguard_asg" {
name = aws_launch_configuration.wireguard_launch_config.name
launch_configuration = aws_launch_configuration.wireguard_launch_config.name
min_size = var.asg_min_size
desired_capacity = var.asg_desired_capacity
max_size = var.asg_max_size
vpc_zone_identifier = var.subnet_ids
health_check_type = "EC2"
termination_policies = ["OldestLaunchConfiguration", "OldestInstance"]
target_group_arns = var.target_group_arns
lifecycle {
create_before_destroy = true
}
tags = [
{
key = "Name"
value = aws_launch_configuration.wireguard_launch_config.name
propagate_at_launch = true
},
{
key = "Project"
value = "wireguard"
propagate_at_launch = true
},
{
key = "env"
value = var.env
propagate_at_launch = true
},
{
key = "tf-managed"
value = "True"
propagate_at_launch = true
},
]
}