-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
129 lines (109 loc) · 3.45 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
locals {
name = "example-${basename(path.cwd)}"
aws_tags = {
Name = local.name
}
tailscale_acl_tags = [
"tag:example-infra",
"tag:example-exitnode",
"tag:example-subnetrouter",
"tag:example-appconnector",
]
tailscale_set_preferences = [
"--auto-update",
"--ssh",
"--advertise-connector",
"--advertise-exit-node",
"--advertise-routes=${join(",", [
local.vpc_cidr_block,
])}",
]
// Modify these to use your own VPC
vpc_cidr_block = module.vpc.vpc_cidr_block
vpc_id = module.vpc.vpc_id
subnet_id = module.vpc.public_subnets[0]
private_subnet_id = module.vpc.private_subnets[0]
security_group_ids = [aws_security_group.tailscale.id]
instance_type = "c7g.medium"
}
// Remove this to use your own VPC.
module "vpc" {
source = "../internal-modules/aws-vpc"
name = local.name
tags = local.aws_tags
cidr = "10.0.80.0/22"
public_subnets = ["10.0.80.0/24"]
private_subnets = ["10.0.81.0/24"]
}
resource "tailscale_tailnet_key" "main" {
ephemeral = true
preauthorized = true
reusable = true
recreate_if_invalid = "always"
tags = local.tailscale_acl_tags
}
resource "aws_network_interface" "primary" {
subnet_id = local.subnet_id
security_groups = local.security_group_ids
tags = merge(local.aws_tags, { Name = "${local.name}-primary" })
}
resource "aws_eip" "primary" {
tags = local.aws_tags
}
resource "aws_eip_association" "primary" {
network_interface_id = aws_network_interface.primary.id
allocation_id = aws_eip.primary.id
}
resource "aws_network_interface" "secondary" {
subnet_id = local.private_subnet_id
security_groups = local.security_group_ids
tags = merge(local.aws_tags, { Name = "${local.name}-secondary" })
source_dest_check = false
}
module "tailscale_aws_ec2_autoscaling" {
source = "../internal-modules/aws-ec2-autoscaling/"
autoscaling_group_name = local.name
instance_type = local.instance_type
instance_tags = local.aws_tags
network_interfaces = [
aws_network_interface.primary.id, # first NIC must be in PUBLIC subnet
aws_network_interface.secondary.id,
]
# Variables for Tailscale resources
tailscale_hostname = local.name
tailscale_auth_key = tailscale_tailnet_key.main.key
tailscale_set_preferences = local.tailscale_set_preferences
depends_on = [
module.vpc.nat_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
]
}
resource "aws_security_group" "tailscale" {
vpc_id = local.vpc_id
name = local.name
}
resource "aws_security_group_rule" "tailscale_ingress" {
security_group_id = aws_security_group.tailscale.id
type = "ingress"
from_port = 41641
to_port = 41641
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
resource "aws_security_group_rule" "egress" {
security_group_id = aws_security_group.tailscale.id
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
resource "aws_security_group_rule" "internal_vpc_ingress_ipv4" {
security_group_id = aws_security_group.tailscale.id
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [local.vpc_cidr_block]
}