Skip to content

Instantly share code, notes, and snippets.

@calvinfo
Created December 4, 2015 22:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvinfo/1f207e426ede1fb891c3 to your computer and use it in GitHub Desktop.
Save calvinfo/1f207e426ede1fb891c3 to your computer and use it in GitHub Desktop.
/**
* Load balancer.
*/
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}"]
}
/**
*
*/
resource "aws_ecs_task_definition" "main" {
family = "${var.name}"
container_definitions = <<EOF
[
{
"cpu": ${var.cpu},
"environment": [${var.env_vars}],
"essential": true,
"image": "${var.image}",
"memory": ${var.memory},
"name": "${var.name}",
"portMappings": [
{
"containerPort": ${var.container_port},
"hostPort": ${var.port}
}
]
}
]
EOF
}
/**
* Add our service definition
*/
resource "aws_ecs_service" "main" {
name = "${var.name}"
cluster = "${var.cluster}"
task_definition = "${aws_ecs_task_definition.main.arn}"
desired_count = "${var.count}"
iam_role = "${var.iam_role}"
load_balancer {
elb_name = "${aws_elb.main.id}"
container_name = "${var.name}"
container_port = "${var.port}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment