1
1
locals {
2
2
name = " example-${ basename (path. cwd )} "
3
3
4
- tags = {
4
+ aws_tags = {
5
5
Name = local.name
6
6
}
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"
7
31
}
8
32
33
+ // Remove this to use your own VPC.
9
34
module "vpc" {
10
35
source = " ../internal-modules/aws-vpc"
11
36
12
37
name = local. name
13
- tags = local. tags
38
+ tags = local. aws_tags
14
39
15
40
cidr = " 10.0.80.0/22"
16
41
@@ -23,31 +48,26 @@ resource "tailscale_tailnet_key" "main" {
23
48
preauthorized = true
24
49
reusable = true
25
50
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
32
52
}
33
53
34
54
resource "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" })
38
58
}
39
59
resource "aws_eip" "primary" {
40
- tags = local. tags
60
+ tags = local. aws_tags
41
61
}
42
62
resource "aws_eip_association" "primary" {
43
63
network_interface_id = aws_network_interface. primary . id
44
64
allocation_id = aws_eip. primary . id
45
65
}
46
66
47
67
resource "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" })
51
71
52
72
source_dest_check = false
53
73
}
@@ -56,26 +76,54 @@ module "tailscale_aws_ec2_autoscaling" {
56
76
source = " ../internal-modules/aws-ec2-autoscaling/"
57
77
58
78
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
61
81
62
82
network_interfaces = [
63
83
aws_network_interface . primary . id , # first NIC must be in PUBLIC subnet
64
84
aws_network_interface . secondary . id ,
65
85
]
66
86
67
87
# 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
77
91
78
92
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
80
94
]
81
95
}
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