Skip to content

Commit 7eaa53f

Browse files
committed
terraform: usability improvements
updates #22
1 parent 80d208c commit 7eaa53f

File tree

3 files changed

+71
-73
lines changed

3 files changed

+71
-73
lines changed
+71-24
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
locals {
22
name = "example-${basename(path.cwd)}"
33

4-
tags = {
4+
aws_tags = {
55
Name = local.name
66
}
7+
8+
tailscale_acl_tags = [
9+
"tag:example-infra",
10+
"tag:example-exitnode",
11+
"tag:example-subnetrouter",
12+
"tag:example-appconnector",
13+
]
14+
tailscale_set_preferences = [
15+
"--auto-update",
16+
"--ssh",
17+
"--advertise-connector",
18+
"--advertise-exit-node",
19+
"--advertise-routes=${join(",", [
20+
local.vpc_cidr_block,
21+
])}",
22+
]
23+
24+
// Modify these to use your own VPC
25+
vpc_cidr_block = module.vpc.vpc_cidr_block
26+
vpc_id = module.vpc.vpc_id
27+
subnet_id = module.vpc.public_subnets[0]
28+
security_group_ids = [aws_security_group.tailscale.id]
29+
instance_type = "t4g.micro"
730
}
831

32+
// Remove this to use your own VPC.
933
module "vpc" {
1034
source = "../internal-modules/aws-vpc"
1135

1236
name = local.name
13-
tags = local.tags
37+
tags = local.aws_tags
1438

1539
cidr = "10.0.80.0/22"
1640

@@ -23,21 +47,16 @@ resource "tailscale_tailnet_key" "main" {
2347
preauthorized = true
2448
reusable = true
2549
recreate_if_invalid = "always"
26-
tags = [
27-
"tag:example-infra",
28-
"tag:example-exitnode",
29-
"tag:example-subnetrouter",
30-
"tag:example-appconnector",
31-
]
50+
tags = local.tailscale_acl_tags
3251
}
3352

3453
resource "aws_network_interface" "primary" {
35-
subnet_id = module.vpc.public_subnets[0]
36-
security_groups = [module.vpc.tailscale_security_group_id]
37-
tags = local.tags
54+
subnet_id = local.subnet_id
55+
security_groups = local.security_group_ids
56+
tags = local.aws_tags
3857
}
3958
resource "aws_eip" "primary" {
40-
tags = local.tags
59+
tags = local.aws_tags
4160
}
4261
resource "aws_eip_association" "primary" {
4362
network_interface_id = aws_network_interface.primary.id
@@ -48,23 +67,51 @@ module "tailscale_aws_ec2_autoscaling" {
4867
source = "../internal-modules/aws-ec2-autoscaling/"
4968

5069
autoscaling_group_name = local.name
51-
instance_type = "t4g.micro"
52-
instance_tags = local.tags
70+
instance_type = local.instance_type
71+
instance_tags = local.aws_tags
5372

5473
network_interfaces = [aws_network_interface.primary.id]
5574

5675
# Variables for Tailscale resources
57-
tailscale_auth_key = tailscale_tailnet_key.main.key
58-
tailscale_hostname = local.name
59-
tailscale_set_preferences = [
60-
"--auto-update",
61-
"--ssh",
62-
"--advertise-connector",
63-
"--advertise-exit-node",
64-
"--advertise-routes=${join(",", [module.vpc.vpc_cidr_block])}",
65-
]
76+
tailscale_auth_key = tailscale_tailnet_key.main.key
77+
tailscale_hostname = local.name
78+
tailscale_set_preferences = local.tailscale_set_preferences
6679

6780
depends_on = [
68-
module.vpc.natgw_ids, # ensure NAT gateway is available before instance provisioning - primarily for private subnets
81+
module.vpc.natgw_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
6982
]
7083
}
84+
85+
resource "aws_security_group" "tailscale" {
86+
vpc_id = local.vpc_id
87+
name = local.name
88+
}
89+
90+
resource "aws_security_group_rule" "tailscale_ingress" {
91+
security_group_id = aws_security_group.tailscale.id
92+
type = "ingress"
93+
from_port = 41641
94+
to_port = 41641
95+
protocol = "udp"
96+
cidr_blocks = ["0.0.0.0/0"]
97+
ipv6_cidr_blocks = ["::/0"]
98+
}
99+
100+
resource "aws_security_group_rule" "egress" {
101+
security_group_id = aws_security_group.tailscale.id
102+
type = "egress"
103+
from_port = 0
104+
to_port = 0
105+
protocol = "-1"
106+
cidr_blocks = ["0.0.0.0/0"]
107+
ipv6_cidr_blocks = ["::/0"]
108+
}
109+
110+
resource "aws_security_group_rule" "internal_vpc_ingress_ipv4" {
111+
security_group_id = aws_security_group.tailscale.id
112+
type = "ingress"
113+
from_port = 0
114+
to_port = 0
115+
protocol = "-1"
116+
cidr_blocks = [local.vpc_cidr_block]
117+
}

terraform/aws/internal-modules/aws-vpc/main.tf

-45
Original file line numberDiff line numberDiff line change
@@ -30,48 +30,3 @@ module "vpc" {
3030
public_subnet_ipv6_prefixes = range(0, length(var.public_subnets))
3131
private_subnet_ipv6_prefixes = range(10, 10 + length(var.private_subnets))
3232
}
33-
34-
resource "aws_security_group" "tailscale" {
35-
vpc_id = module.vpc.vpc_id
36-
name = var.name
37-
}
38-
39-
resource "aws_security_group_rule" "egress" {
40-
security_group_id = aws_security_group.tailscale.id
41-
type = "egress"
42-
from_port = 0
43-
to_port = 0
44-
protocol = "-1"
45-
cidr_blocks = ["0.0.0.0/0"]
46-
ipv6_cidr_blocks = ["::/0"]
47-
}
48-
49-
resource "aws_security_group_rule" "internal_vpc_ingress_ipv4" {
50-
security_group_id = aws_security_group.tailscale.id
51-
type = "ingress"
52-
from_port = 0
53-
to_port = 0
54-
protocol = "-1"
55-
cidr_blocks = [var.cidr]
56-
}
57-
58-
resource "aws_security_group_rule" "internal_vpc_ingress_ipv6" {
59-
count = var.enable_ipv6 == false ? 0 : 1
60-
61-
security_group_id = aws_security_group.tailscale.id
62-
type = "ingress"
63-
from_port = 0
64-
to_port = 0
65-
protocol = "-1"
66-
ipv6_cidr_blocks = [module.vpc.vpc_ipv6_cidr_block]
67-
}
68-
69-
resource "aws_security_group_rule" "tailscale_ingress" {
70-
security_group_id = aws_security_group.tailscale.id
71-
type = "ingress"
72-
from_port = 41641
73-
to_port = 41641
74-
protocol = "udp"
75-
cidr_blocks = ["0.0.0.0/0"]
76-
ipv6_cidr_blocks = ["::/0"]
77-
}

terraform/aws/internal-modules/aws-vpc/outputs.tf

-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ output "natgw_ids" {
3030
value = module.vpc.natgw_ids
3131
}
3232

33-
output "tailscale_security_group_id" {
34-
value = aws_security_group.tailscale.id
35-
}
36-
3733
output "public_route_table_ids" {
3834
value = module.vpc.public_route_table_ids
3935
}

0 commit comments

Comments
 (0)