Skip to content

Commit 283d310

Browse files
authored
terraform: azure, google - usability improvements (#24)
closes #22
1 parent 5dc08a0 commit 283d310

File tree

14 files changed

+165
-132
lines changed

14 files changed

+165
-132
lines changed

terraform/aws/aws-ec2-autoscaling-dual-subnet/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module "tailscale_aws_ec2_autoscaling" {
9090
tailscale_set_preferences = local.tailscale_set_preferences
9191

9292
depends_on = [
93-
module.vpc.natgw_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
93+
module.vpc.nat_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
9494
]
9595
}
9696

terraform/aws/aws-ec2-autoscaling-session-recorder/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ module "tailscale_aws_ec2_autoscaling" {
190190
]
191191

192192
depends_on = [
193-
module.vpc.natgw_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
193+
module.vpc.nat_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
194194
]
195195
}
196196

terraform/aws/aws-ec2-autoscaling/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module "tailscale_aws_ec2_autoscaling" {
7878
tailscale_set_preferences = local.tailscale_set_preferences
7979

8080
depends_on = [
81-
module.vpc.natgw_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
81+
module.vpc.nat_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
8282
]
8383
}
8484

terraform/aws/aws-ec2-instance-dual-stack-ipv4-ipv6/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module "tailscale_aws_ec2" {
6868
tailscale_set_preferences = local.tailscale_set_preferences
6969

7070
depends_on = [
71-
module.vpc.natgw_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
71+
module.vpc.nat_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
7272
]
7373
}
7474

terraform/aws/aws-ec2-instance/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module "tailscale_aws_ec2" {
6565
tailscale_set_preferences = local.tailscale_set_preferences
6666

6767
depends_on = [
68-
module.vpc.natgw_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
68+
module.vpc.nat_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
6969
]
7070
}
7171

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ output "nat_public_ips" {
2525
value = module.vpc.nat_public_ips
2626
}
2727

28-
output "natgw_ids" {
28+
output "nat_ids" {
2929
description = "Useful for using within `depends_on` for other resources"
30-
value = module.vpc.natgw_ids
30+
value = module.vpc.nat_ids
3131
}
3232

3333
output "public_route_table_ids" {
+63-27
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,51 @@
11
locals {
22
name = "example-${basename(path.cwd)}"
33

4-
tags = {
4+
azure_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(",", coalescelist(
20+
local.vpc_cidr_block,
21+
))}",
22+
]
23+
24+
// Modify these to use your own VPC
25+
resource_group_name = azurerm_resource_group.main.name
26+
location = azurerm_resource_group.main.location
27+
28+
vpc_cidr_block = module.vpc.vnet_address_space
29+
vpc_id = module.vpc.vnet_id
30+
subnet_id = module.vpc.public_subnet_id
31+
network_security_group_id = azurerm_network_security_group.tailscale_ingress.id
32+
instance_type = "Standard_DS1_v2"
33+
admin_public_key_path = var.admin_public_key_path
734
}
835

936
resource "azurerm_resource_group" "main" {
1037
location = "centralus"
1138
name = local.name
1239
}
1340

14-
module "network" {
41+
module "vpc" {
1542
source = "../internal-modules/azure-network"
1643

1744
name = local.name
18-
tags = local.tags
45+
tags = local.azure_tags
1946

20-
location = azurerm_resource_group.main.location
21-
resource_group_name = azurerm_resource_group.main.name
47+
location = local.location
48+
resource_group_name = local.resource_group_name
2249

2350
cidrs = ["10.0.0.0/22"]
2451
subnet_cidrs = [
@@ -39,40 +66,49 @@ resource "tailscale_tailnet_key" "main" {
3966
preauthorized = true
4067
reusable = true
4168
recreate_if_invalid = "always"
42-
tags = [
43-
"tag:example-infra",
44-
"tag:example-exitnode",
45-
"tag:example-subnetrouter",
46-
"tag:example-appconnector",
47-
]
69+
tags = local.tailscale_acl_tags
4870
}
4971

5072
module "tailscale_azure_linux_virtual_machine" {
5173
source = "../internal-modules/azure-linux-vm"
5274

53-
location = azurerm_resource_group.main.location
54-
resource_group_name = azurerm_resource_group.main.name
75+
location = local.location
76+
resource_group_name = local.resource_group_name
5577

5678
# public subnet
57-
primary_subnet_id = module.network.public_subnet_id
79+
primary_subnet_id = local.subnet_id
80+
network_security_group_id = local.network_security_group_id
5881

5982
machine_name = local.name
60-
machine_size = "Standard_DS1_v2"
61-
admin_public_key_path = var.admin_public_key_path
62-
resource_tags = local.tags
83+
machine_size = local.instance_type
84+
admin_public_key_path = local.admin_public_key_path
85+
resource_tags = local.azure_tags
6386

6487
# Variables for Tailscale resources
65-
tailscale_hostname = local.name
66-
tailscale_auth_key = tailscale_tailnet_key.main.key
67-
tailscale_set_preferences = [
68-
"--auto-update",
69-
"--ssh",
70-
"--advertise-connector",
71-
"--advertise-exit-node",
72-
"--advertise-routes=${join(",", module.network.vnet_address_space)}",
73-
]
88+
tailscale_hostname = local.name
89+
tailscale_auth_key = tailscale_tailnet_key.main.key
90+
tailscale_set_preferences = local.tailscale_set_preferences
7491

7592
depends_on = [
76-
module.network.natgw_ids, # for private subnets - ensure NAT gateway is available before instance provisioning
93+
module.vpc.nat_ids, # remove if using your own VPC otherwise ensure provisioned NAT gateway is available
7794
]
7895
}
96+
97+
resource "azurerm_network_security_group" "tailscale_ingress" {
98+
location = local.location
99+
resource_group_name = local.resource_group_name
100+
101+
name = "nsg-tailscale-ingress"
102+
103+
security_rule {
104+
name = "AllowTailscaleInbound"
105+
access = "Allow"
106+
direction = "Inbound"
107+
priority = 100
108+
protocol = "Udp"
109+
source_address_prefix = "Internet"
110+
source_port_range = "*"
111+
destination_address_prefix = "*"
112+
destination_port_range = "41641"
113+
}
114+
}

terraform/azure/azure-linux-vm/outputs.tf

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
output "vpc_id" {
2-
value = module.network.vnet_id
2+
value = module.vpc.vnet_id
33
}
44

55
output "nat_public_ips" {
6-
value = module.network.nat_public_ips
6+
value = module.vpc.nat_public_ips
77
}
88

99
output "public_subnet_id" {
10-
value = module.network.public_subnet_id
10+
value = module.vpc.public_subnet_id
1111
}
1212
output "private_subnet_id" {
13-
value = module.network.private_subnet_id
13+
value = module.vpc.private_subnet_id
1414
}
1515

1616
output "private_dns_resolver_inbound_endpoint_ip" {
17-
value = module.network.private_dns_resolver_inbound_endpoint_ip
17+
value = module.vpc.private_dns_resolver_inbound_endpoint_ip
1818
}
1919
output "internal_domain_name_suffix" {
2020
value = module.tailscale_azure_linux_virtual_machine.internal_domain_name_suffix

terraform/azure/internal-modules/azure-linux-vm/main.tf

+1-20
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,7 @@ resource "azurerm_network_interface" "primary" {
2828

2929
resource "azurerm_network_interface_security_group_association" "tailscale" {
3030
network_interface_id = azurerm_network_interface.primary.id
31-
network_security_group_id = azurerm_network_security_group.tailscale_ingress.id
32-
}
33-
34-
resource "azurerm_network_security_group" "tailscale_ingress" {
35-
location = var.location
36-
resource_group_name = var.resource_group_name
37-
38-
name = "nsg-tailscale-ingress"
39-
40-
security_rule {
41-
name = "AllowTailscaleInbound"
42-
access = "Allow"
43-
direction = "Inbound"
44-
priority = 100
45-
protocol = "Udp"
46-
source_address_prefix = "Internet"
47-
source_port_range = "*"
48-
destination_address_prefix = "*"
49-
destination_port_range = "41641"
50-
}
31+
network_security_group_id = var.network_security_group_id
5132
}
5233

5334
resource "azurerm_linux_virtual_machine" "tailscale_instance" {

terraform/azure/internal-modules/azure-linux-vm/variables.tf

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ variable "primary_subnet_id" {
2525
description = "The primary subnet (typically PUBLIC) to assign to the virtual machine"
2626
type = string
2727
}
28+
variable "network_security_group_id" {
29+
description = "The network security group to assign to the virtual machine"
30+
type = string
31+
}
2832
variable "machine_size" {
2933
description = "The machine size to assign the virtual machine"
3034
type = string

terraform/azure/internal-modules/azure-network/main.tf

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module "network" {
1+
module "vpc" {
22
# https://registry.terraform.io/modules/Azure/network/azurerm/latest
33
source = "Azure/network/azurerm"
44
version = ">= 5.0, < 6.0"
@@ -37,28 +37,28 @@ module "network" {
3737
data "azurerm_subnet" "public" {
3838
resource_group_name = var.resource_group_name
3939

40-
virtual_network_name = module.network.vnet_name
40+
virtual_network_name = module.vpc.vnet_name
4141
name = var.subnet_name_public
4242

43-
depends_on = [module.network.vnet_subnets]
43+
depends_on = [module.vpc.vnet_subnets]
4444
}
4545

4646
data "azurerm_subnet" "private" {
4747
resource_group_name = var.resource_group_name
4848

49-
virtual_network_name = module.network.vnet_name
49+
virtual_network_name = module.vpc.vnet_name
5050
name = var.subnet_name_private
5151

52-
depends_on = [module.network.vnet_subnets]
52+
depends_on = [module.vpc.vnet_subnets]
5353
}
5454

5555
data "azurerm_subnet" "dns-inbound" {
5656
resource_group_name = var.resource_group_name
5757

58-
virtual_network_name = module.network.vnet_name
58+
virtual_network_name = module.vpc.vnet_name
5959
name = var.subnet_name_private_dns_resolver
6060

61-
depends_on = [module.network.vnet_subnets]
61+
depends_on = [module.vpc.vnet_subnets]
6262
}
6363
#
6464
# Private DNS resolver resources
@@ -70,7 +70,7 @@ resource "azurerm_private_dns_resolver" "main" {
7070
name = var.name
7171
tags = var.tags
7272

73-
virtual_network_id = module.network.vnet_id
73+
virtual_network_id = module.vpc.vnet_id
7474
}
7575

7676
resource "azurerm_private_dns_resolver_inbound_endpoint" "main" {

terraform/azure/internal-modules/azure-network/outputs.tf

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
output "vnet_id" {
2-
value = module.network.vnet_id
2+
value = module.vpc.vnet_id
33
}
44
output "vnet_name" {
5-
value = module.network.vnet_name
5+
value = module.vpc.vnet_name
66
}
77
output "vnet_address_space" {
8-
value = module.network.vnet_address_space
8+
value = module.vpc.vnet_address_space
99
}
1010
output "vnet_subnets" {
11-
value = module.network.vnet_subnets
11+
value = module.vpc.vnet_subnets
1212
}
1313

1414
output "public_subnet_id" {
@@ -40,7 +40,7 @@ output "nat_public_ips" {
4040
value = azurerm_public_ip.nat.*.ip_address
4141
}
4242

43-
output "natgw_ids" {
43+
output "nat_ids" {
4444
description = "Useful for using within `depends_on` for other resources"
4545
value = azurerm_nat_gateway.nat.*.id
4646
}

0 commit comments

Comments
 (0)