consul/test/load/terraform/consul.tf

144 lines
4.0 KiB
Terraform
Raw Normal View History

# Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
# SPDX-License-Identifier: BUSL-1.1
2020-10-06 01:16:09 +00:00
data "aws_ami" "consul" {
most_recent = true
owners = var.ami_owners
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "is-public"
values = ["false"]
}
filter {
name = "name"
values = ["consul-ubuntu-*"]
}
}
# ---------------------------------------------------------------------------------------------------------------------
# Deploy consul cluster
# ---------------------------------------------------------------------------------------------------------------------
module "consul_servers" {
source = "git::git@github.com:hashicorp/terraform-aws-consul.git//modules/consul-cluster?ref=v0.8.0"
cluster_name = "${var.cluster_name}-server"
cluster_size = var.num_servers
instance_type = var.instance_type
cluster_tag_key = var.cluster_tag_key
cluster_tag_value = var.cluster_name
ami_id = var.consul_ami_id == null ? data.aws_ami.consul.id : var.consul_ami_id
user_data = data.template_file.user_data_server.rendered
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.public_subnets
2022-05-16 20:49:46 +00:00
allowed_ssh_cidr_blocks = [var.vpc_cidr]
2022-05-16 20:49:46 +00:00
allowed_inbound_cidr_blocks = [var.vpc_cidr]
ssh_key_name = module.keys.key_name
}
module "consul_clients" {
source = "git::git@github.com:hashicorp/terraform-aws-consul.git//modules/consul-cluster?ref=v0.8.0"
cluster_name = "${var.cluster_name}-client"
cluster_size = var.num_clients
instance_type = var.instance_type
cluster_tag_key = var.cluster_tag_key
cluster_tag_value = var.cluster_name
ami_id = var.consul_ami_id == null ? data.aws_ami.consul.id : var.consul_ami_id
user_data = data.template_file.user_data_client.rendered
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.public_subnets
2022-05-16 20:49:46 +00:00
allowed_ssh_cidr_blocks = [var.vpc_cidr]
2022-05-16 20:49:46 +00:00
allowed_inbound_cidr_blocks = [var.vpc_cidr]
ssh_key_name = module.keys.key_name
2020-10-06 01:16:09 +00:00
}
2020-10-06 01:16:09 +00:00
# ---------------------------------------------------------------------------------------------------------------------
# This script will configure and start Consul agents
# ---------------------------------------------------------------------------------------------------------------------
data "template_file" "user_data_server" {
template = file("${path.module}/user-data-server.sh")
vars = {
consul_version = var.consul_version
consul_download_url = var.consul_download_url
cluster_tag_key = var.cluster_tag_key
cluster_tag_value = var.cluster_name
2020-10-06 01:16:09 +00:00
}
}
data "template_file" "user_data_client" {
template = file("${path.module}/user-data-client.sh")
vars = {
consul_version = var.consul_version
consul_download_url = var.consul_download_url
cluster_tag_key = var.cluster_tag_key
cluster_tag_value = var.cluster_name
2020-10-06 01:16:09 +00:00
}
}
#
# Set up ALB for test-servers to talk to consul clients
#
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 5.0"
name = "${var.cluster_name}-alb"
2020-10-06 01:16:09 +00:00
load_balancer_type = "application"
vpc_id = module.vpc.vpc_id
subnets = module.vpc.public_subnets
security_groups = [module.consul_clients.security_group_id]
2020-10-06 01:16:09 +00:00
internal = true
target_groups = [
{
#name_prefix has a six char limit
name_prefix = "test-"
backend_protocol = "HTTP"
backend_port = 8500
target_type = "instance"
2020-12-15 23:03:44 +00:00
health_check = {
interval = 5
timeout = 3
protocol = "HTTP"
healthy_threshold = 2
path = "/v1/status/leader"
}
2020-10-06 01:16:09 +00:00
}
]
http_tcp_listeners = [
{
port = 8500
protocol = "HTTP"
target_group_index = 0
}
]
}
# Attach ALB to Consul clients
resource "aws_autoscaling_attachment" "asg_attachment_bar" {
autoscaling_group_name = module.consul_clients.asg_name
2020-10-06 01:16:09 +00:00
alb_target_group_arn = module.alb.target_group_arns[0]
}