Skip to content

Commit 1d9efcc

Browse files
Scott WinklerScott Winkler
authored andcommitted
intial commit
0 parents  commit 1d9efcc

File tree

22 files changed

+964
-0
lines changed

22 files changed

+964
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
terraform
11+
.DS_Store

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# terraform-aws-nomad
2+
3+
a Terraform module to deploy a Nomad + Consul cluster on AWS

main.tf

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
module "resourcegroup" {
2+
source = "./modules/resourcegroup"
3+
4+
namespace = var.namespace
5+
}
6+
7+
module "networking" {
8+
source = "./modules/networking"
9+
namespace = module.resourcegroup.namespace
10+
}
11+
12+
module "loadbalancing" {
13+
source = "./modules/loadbalancing"
14+
15+
namespace = module.resourcegroup.namespace
16+
sg = module.networking.sg
17+
vpc = module.networking.vpc
18+
}
19+
20+
module "consul_servers" {
21+
source = "./modules/cluster"
22+
associate_public_ips = var.associate_public_ips
23+
ssh_keypair = var.ssh_keypair
24+
instance_count = var.consul.servers_count
25+
instance_type = var.consul.server_instance_type
26+
datacenter = var.datacenter
27+
join_wan = var.join_wan
28+
consul = {
29+
version = var.consul.version
30+
mode = "server"
31+
}
32+
33+
namespace = module.resourcegroup.namespace
34+
vpc = module.networking.vpc
35+
security_group_id = module.networking.sg.consul_server
36+
target_group_arns = module.loadbalancing.target_group_arns.consul
37+
}
38+
39+
module "nomad_servers" {
40+
source = "./modules/cluster"
41+
associate_public_ips = var.associate_public_ips
42+
ssh_keypair = var.ssh_keypair
43+
instance_count = var.nomad.servers_count
44+
instance_type = var.nomad.server_instance_type
45+
datacenter = var.datacenter
46+
nomad = {
47+
version = var.nomad.version
48+
mode = "server"
49+
}
50+
consul = {
51+
version = var.consul.version
52+
mode = "client"
53+
}
54+
55+
namespace = module.resourcegroup.namespace
56+
vpc = module.networking.vpc
57+
security_group_id = module.networking.sg.nomad_server
58+
target_group_arns = module.loadbalancing.target_group_arns.nomad
59+
}
60+
61+
module "nomad_clients" {
62+
source = "./modules/cluster"
63+
associate_public_ips = var.associate_public_ips
64+
ssh_keypair = var.ssh_keypair
65+
instance_count = var.nomad.clients_count
66+
instance_type = var.nomad.client_instance_type
67+
datacenter = var.datacenter
68+
nomad = {
69+
version = var.nomad.version
70+
mode = "client"
71+
}
72+
consul = {
73+
version = var.consul.version
74+
mode = "client"
75+
}
76+
77+
namespace = module.resourcegroup.namespace
78+
security_group_id = module.networking.sg.nomad_client
79+
vpc = module.networking.vpc
80+
target_group_arns = module.loadbalancing.target_group_arns.fabio
81+
}

modules/cluster/main.tf

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
module "iam_instance_profile" {
3+
source = "scottwinkler/iip/aws"
4+
actions = ["logs:*", "ec2:DescribeInstances"]
5+
}
6+
7+
data "aws_region" "current" {}
8+
9+
locals {
10+
consul_config = var.consul.mode != "disabled" ? templatefile("${path.module}/templates/consul_${var.consul.mode}.json", {
11+
instance_count = var.instance_count,
12+
namespace = var.namespace,
13+
datacenter = var.datacenter,
14+
join_wan = join(",",[for s in var.join_wan: join("",["\"",s,"\""])]),
15+
}) : ""
16+
nomad_config = var.nomad.mode != "disabled" ? templatefile("${path.module}/templates/nomad_${var.nomad.mode}.hcl", {
17+
instance_count = var.instance_count
18+
datacenter = var.datacenter
19+
region = data.aws_region.current.name
20+
}) : ""
21+
startup = templatefile("${path.module}/templates/startup.sh", {
22+
consul_version = var.consul.version,
23+
consul_config = local.consul_config,
24+
consul_mode = var.consul.mode
25+
nomad_version = var.nomad.version,
26+
nomad_config = local.nomad_config,
27+
nomad_mode = var.nomad.mode,
28+
})
29+
namespace = "${var.namespace}_N${var.nomad.mode}_C${var.consul.mode}"
30+
}
31+
32+
data "aws_ami" "ubuntu" {
33+
most_recent = true
34+
filter {
35+
name = "name"
36+
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
37+
}
38+
owners = ["099720109477"]
39+
}
40+
41+
resource "aws_launch_template" "server" {
42+
name_prefix = local.namespace
43+
image_id = data.aws_ami.ubuntu.id
44+
instance_type = var.instance_type
45+
user_data = base64encode(local.startup)
46+
key_name = var.ssh_keypair
47+
iam_instance_profile {
48+
name = module.iam_instance_profile.name
49+
}
50+
network_interfaces {
51+
associate_public_ip_address = var.associate_public_ips
52+
security_groups = [var.security_group_id]
53+
delete_on_termination = true
54+
}
55+
56+
tags = {
57+
ResourceGroup = var.namespace
58+
}
59+
}
60+
61+
resource "aws_autoscaling_group" "server" {
62+
name = local.namespace
63+
health_check_grace_period = 300
64+
health_check_type = "ELB"
65+
target_group_arns = var.target_group_arns
66+
default_cooldown = 3600
67+
min_size = var.instance_count
68+
max_size = var.instance_count
69+
vpc_zone_identifier = var.associate_public_ips ? var.vpc.public_subnets : var.vpc.private_subnets
70+
launch_template {
71+
id = aws_launch_template.server.id
72+
version = aws_launch_template.server.latest_version
73+
}
74+
tags = [
75+
{
76+
key = "ResourceGroup"
77+
value = var.namespace
78+
propagate_at_launch = true
79+
},
80+
{
81+
key = "Name"
82+
value = local.namespace
83+
propagate_at_launch = true
84+
}
85+
]
86+
}
87+
88+
data "aws_instances" "instances" {
89+
depends_on = [aws_autoscaling_group.server]
90+
count = var.associate_public_ips ? 1 : 0
91+
instance_tags = {
92+
ResourceGroup = var.namespace
93+
Name = local.namespace
94+
}
95+
96+
instance_state_names = ["running", "pending"]
97+
}

modules/cluster/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "public_ips" {
2+
value = var.associate_public_ips&&length(data.aws_instances.instances)>=1 ? data.aws_instances.instances[0].public_ips : []
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"datacenter":"${datacenter}",
3+
"bind_addr": "0.0.0.0",
4+
"advertise_addr": "$PUBLIC_IP",
5+
"addresses": {
6+
"http": "0.0.0.0"
7+
},
8+
"data_dir": "/mnt/consul",
9+
"disable_remote_exec": true,
10+
"disable_update_check": true,
11+
"leave_on_terminate": true,
12+
"retry_join": [ "provider=aws tag_key=ResourceGroup tag_value=${namespace}" ],
13+
"server": false
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"datacenter":"${datacenter}",
3+
"bind_addr": "$PRIVATE_IP",
4+
"advertise_addr": "$PRIVATE_IP",
5+
"advertise_addr_wan": "$PUBLIC_IP",
6+
"translate_wan_addrs": true,
7+
"data_dir": "/mnt/consul",
8+
"disable_remote_exec": true,
9+
"disable_update_check": true,
10+
"bootstrap_expect": ${instance_count},
11+
"leave_on_terminate": true,
12+
"retry_join": [ "provider=aws tag_key=ResourceGroup tag_value=${namespace}" ],
13+
"retry_join_wan" : [ ${join_wan}],
14+
"server": true,
15+
"raft_protocol": 3,
16+
"ui": true,
17+
"autopilot": {
18+
"cleanup_dead_servers": true,
19+
"last_contact_threshold": "200ms",
20+
"max_trailing_logs": 250,
21+
"server_stabilization_time": "10s"
22+
},
23+
"addresses": {
24+
"http": "0.0.0.0"
25+
},
26+
"log_level" : "DEBUG",
27+
"enable_syslog" : true
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
data_dir = "/mnt/nomad"
2+
datacenter = "${datacenter}"
3+
region = "${region}"
4+
bind_addr = "0.0.0.0"
5+
client {
6+
enabled = true
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
data_dir = "/mnt/nomad"
2+
datacenter = "${datacenter}"
3+
region = "${region}"
4+
bind_addr = "0.0.0.0"
5+
advertise {
6+
http = "$PUBLIC_IP"
7+
rpc = "$PUBLIC_IP"
8+
serf = "$PUBLIC_IP"
9+
}
10+
server {
11+
enabled = true
12+
bootstrap_expect = ${instance_count}
13+
}
14+
15+
telemetry {
16+
publish_allocation_metrics = true
17+
publish_node_metrics = true
18+
}
19+
enable_syslog = true
20+
log_level = "DEBUG"

0 commit comments

Comments
 (0)