Skip to content

Commit ebed1ad

Browse files
committed
aws-ec2-instance-dual-stack-ipv4-ipv6
1 parent 138e87b commit ebed1ad

File tree

1 file changed

+70
-28
lines changed
  • terraform/aws/aws-ec2-instance-dual-stack-ipv4-ipv6

1 file changed

+70
-28
lines changed
Lines changed: 70 additions & 28 deletions
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

@@ -25,41 +49,59 @@ resource "tailscale_tailnet_key" "main" {
2549
preauthorized = true
2650
reusable = true
2751
recreate_if_invalid = "always"
28-
tags = [
29-
"tag:example-infra",
30-
"tag:example-exitnode",
31-
"tag:example-subnetrouter",
32-
"tag:example-appconnector",
33-
]
52+
tags = local.tailscale_acl_tags
3453
}
3554

3655
module "tailscale_aws_ec2" {
3756
source = "../internal-modules/aws-ec2-instance"
3857

39-
instance_type = "t4g.micro"
40-
instance_tags = local.tags
58+
instance_type = local.instance_type
59+
instance_tags = local.aws_tags
4160

42-
subnet_id = module.vpc.private_subnets[0]
43-
vpc_security_group_ids = [
44-
module.vpc.tailscale_security_group_id,
45-
]
46-
ipv6_address_count = 1
61+
subnet_id = local.subnet_id
62+
vpc_security_group_ids = local.security_group_ids
63+
ipv6_address_count = 1
4764

4865
# Variables for Tailscale resources
49-
tailscale_hostname = local.name
50-
tailscale_auth_key = tailscale_tailnet_key.main.key
51-
tailscale_set_preferences = [
52-
"--auto-update",
53-
"--ssh",
54-
"--advertise-connector",
55-
"--advertise-exit-node",
56-
"--advertise-routes=${join(",", [
57-
module.vpc.vpc_cidr_block,
58-
module.vpc.vpc_ipv6_cidr_block,
59-
])}",
60-
]
66+
tailscale_hostname = local.name
67+
tailscale_auth_key = tailscale_tailnet_key.main.key
68+
tailscale_set_preferences = local.tailscale_set_preferences
6169

6270
depends_on = [
63-
module.vpc.natgw_ids, # ensure NAT gateway is available before instance provisioning - primarily for private subnets
71+
module.vpc.natgw_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
6472
]
6573
}
74+
75+
resource "aws_security_group" "tailscale" {
76+
vpc_id = local.vpc_id
77+
name = local.name
78+
}
79+
80+
resource "aws_security_group_rule" "tailscale_ingress" {
81+
security_group_id = aws_security_group.tailscale.id
82+
type = "ingress"
83+
from_port = 41641
84+
to_port = 41641
85+
protocol = "udp"
86+
cidr_blocks = ["0.0.0.0/0"]
87+
ipv6_cidr_blocks = ["::/0"]
88+
}
89+
90+
resource "aws_security_group_rule" "egress" {
91+
security_group_id = aws_security_group.tailscale.id
92+
type = "egress"
93+
from_port = 0
94+
to_port = 0
95+
protocol = "-1"
96+
cidr_blocks = ["0.0.0.0/0"]
97+
ipv6_cidr_blocks = ["::/0"]
98+
}
99+
100+
resource "aws_security_group_rule" "internal_vpc_ingress_ipv4" {
101+
security_group_id = aws_security_group.tailscale.id
102+
type = "ingress"
103+
from_port = 0
104+
to_port = 0
105+
protocol = "-1"
106+
cidr_blocks = [local.vpc_cidr_block]
107+
}

0 commit comments

Comments
 (0)