Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Required Variables.
*/
variable "name" {}
variable "port" {}
variable "elb_security_group" {}
variable "elb_subnets" {}
variable "zone_id" {}
variable "environment" {}
variable "iam_role" {}
variable "cluster" {}
variable "task" {}
/**
* Optional Variables.
*/
variable "healthcheck" { default = "/internal/health" }
variable "desired_count" { default = 2 }
/**
* Resources.
*/
# The main ELB.
resource "aws_elb" "main" {
name = "${var.name}"
internal = true
cross_zone_load_balancing = true
subnets = ["${split(",", var.elb_subnets)}"]
security_groups = ["${var.elb_security_group}"]
idle_timeout = 30
connection_draining = true
connection_draining_timeout = 15
listener {
lb_port = 80
lb_protocol = "http"
instance_port = "${var.port}"
instance_protocol = "http"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 5
target = "HTTP:${var.port}${var.healthcheck}"
interval = 30
}
tags {
Name = "${var.name}-balancer"
Service = "${var.name}"
}
}
# <name>.segment.local. CNAME for the load balancer.
resource "aws_route53_record" "main" {
zone_id = "${var.zone_id}"
name = "${var.name}.segment.local"
type = "CNAME"
ttl = 60
records = ["${aws_elb.main.dns_name}"]
}
# Creates a version of the service for the cluster.
resource "aws_ecs_service" "main" {
name = "${var.name}"
cluster = "${var.cluster}"
task_definition = "${var.task}"
desired_count = "${var.desired_count}"
iam_role = "${var.iam_role}"
load_balancer {
elb_name = "${aws_elb.main.dns_name}"
container_name = "${var.name}"
container_port = "${var.container_port}"
}
}
/**
* Outputs.
*/
output "name" { value = "${var.name}" }
output "address" { value = "${var.name}.segment.local" }
output "elb" { value = "${aws_elb.main.id}" }
output "fqdn" { value = "${aws_route53_record.main.fqdn}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.