Skip to content

Commit 0a6650f

Browse files
committed
Module first commit
1 parent 7b76fdd commit 0a6650f

File tree

15 files changed

+566
-0
lines changed

15 files changed

+566
-0
lines changed

TODO.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- change to launch template
2+
- add spot
3+
- dns records

_data.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
data "aws_ami" "amzn" {
2+
most_recent = true
3+
owners = ["amazon"]
4+
5+
filter {
6+
name = "name"
7+
values = ["amzn-ami-*"]
8+
}
9+
10+
name_regex = ".+-amazon-ecs-optimized$"
11+
}
12+
13+
data "aws_caller_identity" "current" {}
14+
15+
#-------
16+
# KMS
17+
data "aws_kms_key" "ebs" {
18+
key_id = "alias/aws/ebs"
19+
}

_outputs.tf

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
output "alb_id" {
2+
value = "${aws_lb.ecs.*.id}"
3+
}
4+
5+
output "alb_arn" {
6+
value = "${aws_lb.ecs.*.arn}"
7+
}
8+
9+
output "alb_dns_name" {
10+
value = "${aws_lb.ecs.*.dns_name}"
11+
}
12+
13+
output "alb_zone_id" {
14+
value = "${aws_lb.ecs.*.zone_id}"
15+
}
16+
17+
output "ecs_iam_role_arn" {
18+
value = "${aws_iam_role.ecs.arn}"
19+
}
20+
21+
output "ecs_iam_role_name" {
22+
value = "${aws_iam_role.ecs.name}"
23+
}
24+
25+
output "ecs_service_iam_role_arn" {
26+
value = "${aws_iam_role.ecs_service.arn}"
27+
}
28+
29+
output "ecs_service_iam_role_name" {
30+
value = "${aws_iam_role.ecs_service.name}"
31+
}
32+
33+
output "ecs_task_iam_role_arn" {
34+
value = "${aws_iam_role.ecs_task.arn}"
35+
}
36+
37+
output "ecs_task_iam_role_name" {
38+
value = "${aws_iam_role.ecs_task.name}"
39+
}
40+
41+
output "ecs_id" {
42+
value = "${aws_ecs_cluster.ecs.id}"
43+
}
44+
45+
output "ecs_arn" {
46+
value = "${aws_ecs_cluster.ecs.arn}"
47+
}
48+
49+
output "ecs_name" {
50+
value = "${aws_ecs_cluster.ecs.name}"
51+
}
52+
53+
output "alb_listener_https_arn" {
54+
value = "${aws_lb_listener.ecs_https.*.arn}"
55+
}
56+
57+
output "ecs_nodes_secgrp_id" {
58+
value = "${aws_security_group.ecs_nodes.id}"
59+
}

_variables.tf

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# == REQUIRED VARS
2+
3+
variable "name" {
4+
description = "Name of this ECS cluster"
5+
}
6+
7+
variable "instance_type_1" {
8+
description = "Instance type for ECS workers (first priority)"
9+
}
10+
11+
variable "instance_type_2" {
12+
description = "Instance type for ECS workers (second priority)"
13+
}
14+
15+
variable "instance_type_3" {
16+
description = "Instance type for ECS workers (third priority)"
17+
}
18+
19+
variable "on_demand_percentage" {
20+
description = "Percentage of on-demand intances vs spot"
21+
default = 100
22+
}
23+
24+
variable "vpc_id" {
25+
description = "VPC ID to deploy the ECS cluster"
26+
}
27+
28+
variable "private_subnet_ids" {
29+
type = "list"
30+
description = "List of private subnet IDs for ECS instances"
31+
}
32+
33+
variable "public_subnet_ids" {
34+
type = "list"
35+
description = "List of public subnet IDs for ECS ALB"
36+
}
37+
38+
variable "secure_subnet_ids" {
39+
type = "list"
40+
description = "List of secure subnet IDs for EFS"
41+
}
42+
43+
variable "certificate_arn" {}
44+
45+
# == OPTIONAL VARS
46+
47+
variable "alb" {
48+
default = true
49+
description = "Whether to deploy an ALB or not with the cluster"
50+
}
51+
52+
variable "asg_min" {
53+
default = 1
54+
}
55+
56+
variable "asg_max" {
57+
default = 4
58+
}
59+
60+
variable "asg_memory_target" {
61+
default = 60
62+
}

alb.tf

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
resource "aws_lb" "ecs" {
2+
count = "${var.alb ? 1 : 0}"
3+
4+
load_balancer_type = "application"
5+
internal = false
6+
name = "ecs-${var.name}"
7+
subnets = ["${var.public_subnet_ids}"]
8+
9+
security_groups = [
10+
"${aws_security_group.alb.id}",
11+
]
12+
13+
idle_timeout = 400
14+
15+
tags = {
16+
Name = "ecs-${var.name}"
17+
}
18+
}
19+
20+
resource "aws_lb_listener" "ecs_https" {
21+
count = "${var.alb ? 1 : 0}"
22+
23+
load_balancer_arn = "${aws_lb.ecs.arn}"
24+
port = "443"
25+
protocol = "HTTPS"
26+
ssl_policy = "ELBSecurityPolicy-2016-08"
27+
certificate_arn = "${var.certificate_arn}"
28+
29+
default_action {
30+
type = "forward"
31+
target_group_arn = "${aws_lb_target_group.ecs_default_https.arn}"
32+
}
33+
}
34+
35+
resource "aws_lb_listener" "ecs_http_redirect" {
36+
count = "${var.alb ? 1 : 0}"
37+
38+
load_balancer_arn = "${aws_lb.ecs.arn}"
39+
port = "80"
40+
protocol = "HTTP"
41+
42+
default_action {
43+
type = "redirect"
44+
45+
redirect {
46+
port = "443"
47+
protocol = "HTTPS"
48+
status_code = "HTTP_301"
49+
}
50+
}
51+
}
52+
53+
resource "aws_lb_target_group" "ecs_default_http" {
54+
count = "${var.alb ? 1 : 0}"
55+
56+
name = "ecs-${var.name}-default-http"
57+
port = 80
58+
protocol = "HTTP"
59+
vpc_id = "${var.vpc_id}"
60+
}
61+
62+
resource "aws_lb_target_group" "ecs_default_https" {
63+
count = "${var.alb ? 1 : 0}"
64+
65+
name = "ecs-${var.name}-default-https"
66+
port = 80
67+
protocol = "HTTP"
68+
vpc_id = "${var.vpc_id}"
69+
}

asg.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
resource "aws_autoscaling_group" "ecs" {
2+
name = "ecs-${var.name}"
3+
4+
mixed_instances_policy {
5+
launch_template {
6+
launch_template_specification {
7+
launch_template_id = "${aws_launch_template.ecs.id}"
8+
version = "$$Latest"
9+
}
10+
11+
override {
12+
instance_type = "${var.instance_type_1}"
13+
}
14+
15+
override {
16+
instance_type = "${var.instance_type_2}"
17+
}
18+
19+
override {
20+
instance_type = "${var.instance_type_3}"
21+
}
22+
}
23+
24+
instances_distribution {
25+
spot_instance_pools = 3
26+
on_demand_base_capacity = 0
27+
on_demand_percentage_above_base_capacity = "${var.on_demand_percentage}"
28+
}
29+
}
30+
31+
vpc_zone_identifier = ["${var.private_subnet_ids}"]
32+
33+
min_size = "${var.asg_min}"
34+
max_size = "${var.asg_max}"
35+
36+
tags = [
37+
"${map("key", "Name", "value", "ecs-node-${var.name}", "propagate_at_launch", true)}",
38+
]
39+
40+
lifecycle {
41+
create_before_destroy = true
42+
}
43+
}
44+
45+
resource "aws_autoscaling_policy" "ecs_memory_tracking" {
46+
name = "ecs-${var.name}-memory"
47+
adjustment_type = "ChangeInCapacity"
48+
policy_type = "TargetTrackingScaling"
49+
autoscaling_group_name = "${aws_autoscaling_group.ecs.name}"
50+
estimated_instance_warmup = "180"
51+
52+
target_tracking_configuration {
53+
customized_metric_specification {
54+
metric_dimension {
55+
name = "ClusterName"
56+
value = "${aws_autoscaling_group.ecs.name}"
57+
}
58+
59+
metric_name = "MemoryReservation"
60+
namespace = "AWS/ECS"
61+
statistic = "Average"
62+
}
63+
64+
target_value = "${var.asg_memory_target}"
65+
}
66+
}

ec2-launch-template.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
data "template_file" "userdata" {
2+
template = "${file("${path.module}/userdata.tpl")}"
3+
4+
vars {
5+
tf_cluster_name = "${aws_ecs_cluster.ecs.name}"
6+
tf_efs_id = "${aws_efs_file_system.ecs.id}"
7+
}
8+
}
9+
10+
resource "aws_launch_template" "ecs" {
11+
name_prefix = "ecs-${var.name}-"
12+
image_id = "${data.aws_ami.amzn.image_id}"
13+
instance_type = "${var.instance_type_1}"
14+
15+
iam_instance_profile = {
16+
name = "${aws_iam_instance_profile.ecs.name}"
17+
}
18+
19+
vpc_security_group_ids = [
20+
"${aws_security_group.ecs_nodes.id}",
21+
]
22+
23+
user_data = "${base64encode(data.template_file.userdata.rendered)}"
24+
25+
lifecycle {
26+
create_before_destroy = true
27+
}
28+
}

ecs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "aws_ecs_cluster" "ecs" {
2+
name = "${var.name}"
3+
}

efs.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
resource "aws_efs_file_system" "ecs" {
2+
creation_token = "ecs-${var.name}"
3+
encrypted = true
4+
5+
tags {
6+
Name = "ecs-${var.name}"
7+
}
8+
9+
lifecycle {
10+
prevent_destroy = true
11+
}
12+
}
13+
14+
resource "aws_efs_mount_target" "ecs" {
15+
count = "${length(var.secure_subnet_ids)}"
16+
file_system_id = "${aws_efs_file_system.ecs.id}"
17+
subnet_id = "${var.secure_subnet_ids[count.index]}"
18+
19+
security_groups = [
20+
"${aws_security_group.efs.id}",
21+
]
22+
}
23+
24+
resource "aws_security_group" "efs" {
25+
name = "ecs-${var.name}-efs"
26+
description = "for EFS to talk to ECS cluster"
27+
vpc_id = "${var.vpc_id}"
28+
29+
tags = {
30+
Name = "ecs-efs-${var.name}"
31+
}
32+
}
33+
34+
resource "aws_security_group_rule" "nfs_from_ecs_to_efs" {
35+
description = "ECS to EFS"
36+
type = "ingress"
37+
from_port = 2049
38+
to_port = 2049
39+
protocol = "tcp"
40+
security_group_id = "${aws_security_group.efs.id}"
41+
source_security_group_id = "${aws_security_group.ecs_nodes.id}"
42+
}

0 commit comments

Comments
 (0)