forked from jmhale/terraform-aws-wireguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
78 lines (67 loc) · 2.25 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
data "template_file" "user_data" {
template = file("${path.module}/templates/user-data.tpl")
vars = {
wg_server_private_key = data.aws_ssm_parameter.wg_server_private_key.value
peers = join("\n", data.template_file.wg_client_data_json.*.rendered)
eip_id = aws_eip.wireguard_eip.id
}
}
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 = var.wg_client_public_keys[count.index].pub_key
client_ip = var.wg_client_public_keys[count.index].ip
}
}
data "template_cloudinit_config" "config" {
part {
content_type = "text/cloud-config"
content = data.template_file.user_data.rendered
}
}
resource "aws_eip" "wireguard_eip" {
vpc = true
}
resource "aws_launch_configuration" "wireguard_launch_config" {
name_prefix = "wireguard-${var.env}-lc-"
image_id = var.ami_id
instance_type = "t2.micro"
key_name = var.ssh_key_id
iam_instance_profile = aws_iam_instance_profile.wireguard_profile.name
user_data = data.template_cloudinit_config.config.rendered
security_groups = [aws_security_group.sg_wireguard_external.id]
associate_public_ip_address = true
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "wireguard_asg" {
name_prefix = "wireguard-${var.env}-asg-${aws_launch_configuration.wireguard_launch_config.name}-"
max_size = 1
min_size = 1
launch_configuration = aws_launch_configuration.wireguard_launch_config.name
vpc_zone_identifier = var.public_subnet_ids
health_check_type = "EC2"
termination_policies = ["OldestInstance"]
lifecycle {
create_before_destroy = true
}
tags = [
{
key = "Name"
value = "wireguard-${var.env}"
propagate_at_launch = true
},
{
key = "Project"
value = "wireguard"
propagate_at_launch = true
},
{
key = "tf-managed"
value = "True"
propagate_at_launch = true
},
]
}