forked from d7oss/terraform-aws-tailscale-vpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
92 lines (81 loc) · 1.95 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
locals {
common_name = "${var.env_name}-vpn"
user_data = templatefile("${path.module}/user-data.yml.tpl", {
tailscale_auth_key = var.tailscale_auth_key
tailscale_advertise_routes = join(",", var.tailscale_advertise_routes)
hostname = var.hostname
ssh_users = var.extra_ssh_users
})
}
data "aws_ami" "main" {
/*
Download latest AMI info for Amazon Linux 2
*/
most_recent = true # This will keep the server up to date. RECOMMENDED.
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-*-x86_64-gp2"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
resource "random_shuffle" "subnet" {
/*
Pick a random subnet ID from the list
*/
input = var.subnet_ids
result_count = 1
}
resource "aws_eip" "main" {
/*
A fixed IP address
*/
vpc = true
tags = { "Name" = local.common_name }
}
resource "aws_instance" "main" {
/*
The VPN-bastion server
*/
ami = data.aws_ami.main.id
instance_type = "t3.micro" # Usually more than enough
key_name = var.key_name
subnet_id = random_shuffle.subnet.result[0]
vpc_security_group_ids = concat([
module.security_group.id,
], var.extra_security_groups)
user_data = local.user_data
tags = { "Name" = local.common_name }
}
resource "aws_eip_association" "main" {
/*
Associate the server to the IP address
*/
instance_id = aws_instance.main.id
allocation_id = aws_eip.main.id
}
module "security_group" {
/*
The security group specific for the server
*/
source = "emyller/security-group/aws"
version = "~> 1.0"
name = "i-${local.common_name}"
vpc_id = var.vpc_id
ingress_cidr_blocks = var.ingress_cidr_blocks
ingress_security_groups = var.ingress_security_groups
}
resource "aws_route53_record" "main" {
/*
DNS record pointing to the VPN server
*/
count = var.dns_zone_id == null ? 0 : 1
zone_id = var.dns_zone_id
name = var.hostname
type = "A"
ttl = 300
records = [aws_eip.main.public_ip]
}