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
+ security_group_ids = [aws_security_group . tailscale . id ]
29
+ instance_type = " t4g.micro"
7
30
}
8
31
32
+ // Remove this to use your own VPC.
9
33
module "vpc" {
10
34
source = " ../internal-modules/aws-vpc"
11
35
12
36
name = local. name
13
- tags = local. tags
37
+ tags = local. aws_tags
14
38
15
39
cidr = " 10.0.80.0/22"
16
40
@@ -23,21 +47,16 @@ resource "tailscale_tailnet_key" "main" {
23
47
preauthorized = true
24
48
reusable = true
25
49
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
32
51
}
33
52
34
53
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
38
57
}
39
58
resource "aws_eip" "primary" {
40
- tags = local. tags
59
+ tags = local. aws_tags
41
60
}
42
61
resource "aws_eip_association" "primary" {
43
62
network_interface_id = aws_network_interface. primary . id
@@ -48,23 +67,51 @@ module "tailscale_aws_ec2_autoscaling" {
48
67
source = " ../internal-modules/aws-ec2-autoscaling/"
49
68
50
69
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
53
72
54
73
network_interfaces = [aws_network_interface . primary . id ]
55
74
56
75
# 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
66
79
67
80
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
69
82
]
70
83
}
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
+ }
0 commit comments