11locals {
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+ private_subnet_id = module. vpc . private_subnets [0 ]
29+ security_group_ids = [aws_security_group . tailscale . id ]
30+ instance_type = " t4g.micro"
731}
832
33+ // Remove this to use your own VPC.
934module "vpc" {
1035 source = " ../internal-modules/aws-vpc"
1136
1237 name = local. name
13- tags = local. tags
38+ tags = local. aws_tags
1439
1540 cidr = " 10.0.80.0/22"
1641
@@ -23,31 +48,26 @@ resource "tailscale_tailnet_key" "main" {
2348 preauthorized = true
2449 reusable = true
2550 recreate_if_invalid = " always"
26- tags = [
27- " tag:example-infra" ,
28- " tag:example-exitnode" ,
29- " tag:example-subnetrouter" ,
30- " tag:example-appconnector" ,
31- ]
51+ tags = local. tailscale_acl_tags
3252}
3353
3454resource "aws_network_interface" "primary" {
35- subnet_id = module . vpc . public_subnets [ 0 ]
36- security_groups = [ module . vpc . tailscale_security_group_id ]
37- tags = merge (local. tags , { Name = " ${ local . name } -primary" })
55+ subnet_id = local . subnet_id
56+ security_groups = local . security_group_ids
57+ tags = merge (local. aws_tags , { Name = " ${ local . name } -primary" })
3858}
3959resource "aws_eip" "primary" {
40- tags = local. tags
60+ tags = local. aws_tags
4161}
4262resource "aws_eip_association" "primary" {
4363 network_interface_id = aws_network_interface. primary . id
4464 allocation_id = aws_eip. primary . id
4565}
4666
4767resource "aws_network_interface" "secondary" {
48- subnet_id = module . vpc . private_subnets [ 0 ]
49- security_groups = [ module . vpc . tailscale_security_group_id ]
50- tags = merge (local. tags , { Name = " ${ local . name } -secondary" })
68+ subnet_id = local . private_subnet_id
69+ security_groups = local . security_group_ids
70+ tags = merge (local. aws_tags , { Name = " ${ local . name } -secondary" })
5171
5272 source_dest_check = false
5373}
@@ -56,26 +76,54 @@ module "tailscale_aws_ec2_autoscaling" {
5676 source = " ../internal-modules/aws-ec2-autoscaling/"
5777
5878 autoscaling_group_name = local. name
59- instance_type = " t4g.micro "
60- instance_tags = local. tags
79+ instance_type = local . instance_type
80+ instance_tags = local. aws_tags
6181
6282 network_interfaces = [
6383 aws_network_interface . primary . id , # first NIC must be in PUBLIC subnet
6484 aws_network_interface . secondary . id ,
6585 ]
6686
6787 # Variables for Tailscale resources
68- tailscale_hostname = local. name
69- tailscale_auth_key = tailscale_tailnet_key. main . key
70- tailscale_set_preferences = [
71- " --auto-update" ,
72- " --ssh" ,
73- " --advertise-connector" ,
74- " --advertise-exit-node" ,
75- " --advertise-routes=${ join (" ," , [module . vpc . vpc_cidr_block ])} " ,
76- ]
88+ tailscale_hostname = local. name
89+ tailscale_auth_key = tailscale_tailnet_key. main . key
90+ tailscale_set_preferences = local. tailscale_set_preferences
7791
7892 depends_on = [
79- module . vpc . natgw_ids , # ensure NAT gateway is available before instance provisioning - primarily for private subnets
93+ module . vpc . natgw_ids , # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
8094 ]
8195}
96+
97+ resource "aws_security_group" "tailscale" {
98+ vpc_id = local. vpc_id
99+ name = local. name
100+ }
101+
102+ resource "aws_security_group_rule" "tailscale_ingress" {
103+ security_group_id = aws_security_group. tailscale . id
104+ type = " ingress"
105+ from_port = 41641
106+ to_port = 41641
107+ protocol = " udp"
108+ cidr_blocks = [" 0.0.0.0/0" ]
109+ ipv6_cidr_blocks = [" ::/0" ]
110+ }
111+
112+ resource "aws_security_group_rule" "egress" {
113+ security_group_id = aws_security_group. tailscale . id
114+ type = " egress"
115+ from_port = 0
116+ to_port = 0
117+ protocol = " -1"
118+ cidr_blocks = [" 0.0.0.0/0" ]
119+ ipv6_cidr_blocks = [" ::/0" ]
120+ }
121+
122+ resource "aws_security_group_rule" "internal_vpc_ingress_ipv4" {
123+ security_group_id = aws_security_group. tailscale . id
124+ type = " ingress"
125+ from_port = 0
126+ to_port = 0
127+ protocol = " -1"
128+ cidr_blocks = [local . vpc_cidr_block ]
129+ }
0 commit comments