1data "aws_ami" "consul" {
2  most_recent = true
3
4  owners = var.ami_owners
5
6  filter {
7    name   = "virtualization-type"
8    values = ["hvm"]
9  }
10
11  filter {
12    name   = "is-public"
13    values = ["false"]
14  }
15
16  filter {
17    name   = "name"
18    values = ["consul-ubuntu-*"]
19  }
20}
21
22# ---------------------------------------------------------------------------------------------------------------------
23# Deploy consul cluster
24# ---------------------------------------------------------------------------------------------------------------------
25
26module "consul_servers" {
27  source = "git::git@github.com:hashicorp/terraform-aws-consul.git//modules/consul-cluster?ref=v0.8.0"
28
29  cluster_name      = "${var.cluster_name}-server"
30  cluster_size      = var.num_servers
31  instance_type     = var.instance_type
32  cluster_tag_key   = var.cluster_tag_key
33  cluster_tag_value = var.cluster_name
34
35  ami_id    = var.consul_ami_id == null ? data.aws_ami.consul.id : var.consul_ami_id
36  user_data = data.template_file.user_data_server.rendered
37
38  vpc_id                  = module.vpc.vpc_id
39  subnet_ids              = module.vpc.public_subnets
40  allowed_ssh_cidr_blocks = ["0.0.0.0/0"]
41
42  allowed_inbound_cidr_blocks = ["0.0.0.0/0"]
43  ssh_key_name                = module.keys.key_name
44
45}
46
47module "consul_clients" {
48  source            = "git::git@github.com:hashicorp/terraform-aws-consul.git//modules/consul-cluster?ref=v0.8.0"
49  cluster_name      = "${var.cluster_name}-client"
50  cluster_size      = var.num_clients
51  instance_type     = var.instance_type
52  cluster_tag_key   = var.cluster_tag_key
53  cluster_tag_value = var.cluster_name
54
55  ami_id    = var.consul_ami_id == null ? data.aws_ami.consul.id : var.consul_ami_id
56  user_data = data.template_file.user_data_client.rendered
57
58  vpc_id                  = module.vpc.vpc_id
59  subnet_ids              = module.vpc.public_subnets
60  allowed_ssh_cidr_blocks = ["0.0.0.0/0"]
61
62  allowed_inbound_cidr_blocks = ["0.0.0.0/0"]
63  ssh_key_name                = module.keys.key_name
64}
65
66
67# ---------------------------------------------------------------------------------------------------------------------
68# This script will configure and start Consul agents
69# ---------------------------------------------------------------------------------------------------------------------
70
71data "template_file" "user_data_server" {
72  template = file("${path.module}/user-data-server.sh")
73
74  vars = {
75    consul_version      = var.consul_version
76    consul_download_url = var.consul_download_url
77    cluster_tag_key     = var.cluster_tag_key
78    cluster_tag_value   = var.cluster_name
79  }
80}
81
82data "template_file" "user_data_client" {
83  template = file("${path.module}/user-data-client.sh")
84
85  vars = {
86    consul_version      = var.consul_version
87    consul_download_url = var.consul_download_url
88    cluster_tag_key     = var.cluster_tag_key
89    cluster_tag_value   = var.cluster_name
90  }
91}
92
93#
94#  Set up ALB for test-servers to talk to consul clients
95#
96module "alb" {
97
98  source  = "terraform-aws-modules/alb/aws"
99  version = "~> 5.0"
100
101  name = "${var.cluster_name}-alb"
102
103  load_balancer_type = "application"
104
105  vpc_id          = module.vpc.vpc_id
106  subnets         = module.vpc.public_subnets
107  security_groups = [module.consul_clients.security_group_id]
108  internal        = true
109
110  target_groups = [
111    {
112      #name_prefix has a six char limit
113      name_prefix      = "test-"
114      backend_protocol = "HTTP"
115      backend_port     = 8500
116      target_type      = "instance"
117      health_check = {
118        interval          = 5
119        timeout           = 3
120        protocol          = "HTTP"
121        healthy_threshold = 2
122        path              = "/v1/status/leader"
123      }
124    }
125  ]
126
127  http_tcp_listeners = [
128    {
129      port               = 8500
130      protocol           = "HTTP"
131      target_group_index = 0
132    }
133  ]
134}
135
136# Attach ALB to Consul clients
137resource "aws_autoscaling_attachment" "asg_attachment_bar" {
138  autoscaling_group_name = module.consul_clients.asg_name
139  alb_target_group_arn   = module.alb.target_group_arns[0]
140}
141