Skip to content

Commit 7d1db83

Browse files
committed
Adding dns logic and az round robin
1 parent 7b3820c commit 7d1db83

7 files changed

Lines changed: 109 additions & 43 deletions

File tree

terraform/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.terraform/
22
*.tfvars
33
*tfstate*
4+
!main.auto.tfvars

terraform/.terraform.lock.hcl

Lines changed: 30 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/dns.tf

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
11

2-
data "aws_route53_zone" "name" {
3-
count = var.zone_name ? 1 : 0
2+
data "aws_route53_zone" "root_zone" {
3+
count = var.create_dns ? 1 : 0
44
name = var.zone_name
55

66
}
77

8-
resource "aws_route53_record" "name" {
9-
count = var.zone_name ? var.num_teams : 0
10-
zone_id = data.aws_route53_zone.name[0].zone_id
11-
name = "team-${count.index + 1}"
8+
resource "aws_route53_zone" "workshop_zone" {
9+
count = var.create_dns ? 1 : 0
10+
name = "${var.event_name}.${var.zone_name}"
11+
}
12+
13+
resource "aws_route53_record" "workshop" {
14+
count = var.create_dns ? 1 : 0
15+
zone_id = data.aws_route53_zone.root_zone[0].zone_id
16+
name = aws_route53_zone.workshop_zone[0].name
17+
type = "NS"
18+
ttl = "300"
19+
records = aws_route53_zone.workshop_zone[0].name_servers
20+
}
21+
22+
resource "aws_route53_record" "teams" {
23+
count = var.create_dns ? var.num_teams : 0
24+
zone_id = aws_route53_zone.workshop_zone[0].zone_id
25+
name = "team-${count.index + 1}"
26+
type = "A"
27+
ttl = 300 #5 mins
28+
records = [module.team_servers[count.index].public_ip]
29+
depends_on = [aws_route53_record.hub]
30+
}
31+
32+
resource "aws_route53_record" "hub" {
33+
count = var.create_dns ? 1 : 0
34+
zone_id = aws_route53_zone.workshop_zone[0].zone_id
35+
name = "hub"
1236
type = "A"
13-
ttl = var.ttl
14-
records = [aws_instance.instance[count.index].public_ip]
37+
ttl = 300 #5 mins
38+
records = [module.hub.public_ip]
1539
}

terraform/main.auto.tfvars

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
num_teams = 2
2+
event_name = "codemash"
3+
custom_security_group_ingress = [{
4+
from_port = 2332,
5+
to_port = 2332,
6+
protocol = "tcp",
7+
description = "ssh",
8+
cidr_blocks = "0.0.0.0/0"
9+
},
10+
{
11+
from_port = 8080,
12+
to_port = 8080,
13+
protocol = "tcp",
14+
description = "http",
15+
cidr_blocks = "0.0.0.0/0"
16+
}
17+
# Wetty config
18+
, {
19+
from_port = 0,
20+
to_port = 6556,
21+
protocol = "tcp",
22+
description = "http",
23+
cidr_blocks = "0.0.0.0/0"
24+
}
25+
]
26+
zone_name = "sbx.justindebo.com"
27+
create_dns = true

terraform/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ module "vpc" {
2020
name = local.name
2121
cidr = "10.0.0.0/16"
2222

23-
azs = [data.aws_availability_zones.available.names]
24-
public_subnets = ["10.0.1.0/24"]
23+
azs = [data.aws_availability_zones.available.names[0], data.aws_availability_zones.available.names[1]]
24+
public_subnets = cidrsubnets("10.0.0.0/16", 8, 8)
2525

2626
enable_nat_gateway = false
2727

@@ -124,7 +124,7 @@ module "team_servers" {
124124
instance_type = "t3a.micro"
125125
key_name = aws_key_pair.main.key_name
126126
vpc_security_group_ids = [module.security_group.security_group_id]
127-
subnet_id = module.vpc.public_subnets[0]
127+
subnet_id = module.vpc.public_subnets[count.index % 2]
128128
associate_public_ip_address = true
129129

130130
user_data = <<-EOF

terraform/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ output "instance_ips" {
22
value = { for instance in module.team_servers : instance.tags_all["Name"] => instance.public_ip }
33
}
44

5+
output "instance_dns" {
6+
value = { for dns in aws_route53_record.teams : "${dns.name}.${aws_route53_zone.workshop_zone[0].name}" => dns.records }
7+
}
8+
59
output "hub_pub_ip" {
610
value = module.hub.public_ip
711
}

terraform/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,15 @@ variable "ssh_local_key_path" {
3232
type = string
3333
default = "~/.ssh/id_rsa.pub"
3434
}
35+
36+
variable "create_dns" {
37+
description = "Whether to create a Route53 DNS zone for the workshop instances"
38+
type = bool
39+
default = false
40+
}
41+
42+
variable "zone_name" {
43+
description = "Route53 zone name to use for workshop instances"
44+
type = string
45+
default = null
46+
}

0 commit comments

Comments
 (0)