Skip to content

Commit d290337

Browse files
committed
chore(files): Upload
0 parents  commit d290337

File tree

6 files changed

+229
-0
lines changed

6 files changed

+229
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Aplyca
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Terraform AWS ECS Deploy module
2+
===============================
3+
4+
Deploy al necessary resources for ECS apps

data.tf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
data "template_file" "this" {
2+
template = "${file(var.definition_file)}"
3+
vars = "${merge(var.definition_vars, zipmap(keys(var.repositories), aws_ecr_repository.this.*.repository_url), map("log_group", "${module.logs.name}"))}"
4+
}
5+
6+
7+
8+

main.tf

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
locals {
2+
id = "${replace(var.name, " ", "-")}"
3+
}
4+
5+
# --------------------------------------------------------
6+
# CREATE New Service
7+
# --------------------------------------------------------
8+
9+
resource "aws_ecr_repository" "this" {
10+
count = "${length(var.repositories)}"
11+
name = "${lower(element(values(var.repositories), count.index))}"
12+
}
13+
14+
resource "aws_ecs_task_definition" "this" {
15+
family = "${local.id}"
16+
container_definitions = "${data.template_file.this.rendered}"
17+
volume = "${var.volumes}"
18+
task_role_arn = "${aws_iam_role.this.arn}"
19+
requires_compatibilities = ["${var.compatibilities}"]
20+
}
21+
22+
resource "aws_alb_target_group" "this" {
23+
count = "${var.balancer["vpc_id"] != "" ? 1 : 0}"
24+
name = "${local.id}"
25+
port = 80
26+
protocol = "HTTP"
27+
vpc_id = "${var.balancer["vpc_id"]}"
28+
tags = "${merge(var.tags, map("Name", "${var.name}"))}"
29+
deregistration_delay = 3
30+
31+
stickiness {
32+
type = "lb_cookie"
33+
enabled = false
34+
}
35+
}
36+
37+
resource "aws_lb_listener_rule" "http" {
38+
count = "${lookup(var.balancer, "condition_values", "") != ""? length(split(",", var.balancer["condition_values"])) : 0}"
39+
listener_arn = "${var.balancer["listener_http"]}"
40+
priority = "${var.balancer["priority"] + count.index}"
41+
42+
action {
43+
type = "forward"
44+
target_group_arn = "${aws_alb_target_group.this.0.arn}"
45+
}
46+
47+
condition {
48+
field = "${var.balancer["condition_field"]}"
49+
values = ["${element(split(",", var.balancer["condition_values"]), count.index)}"]
50+
}
51+
}
52+
53+
resource "aws_lb_listener_rule" "https" {
54+
count = "${lookup(var.balancer, "condition_values", "") != ""? length(split(",", var.balancer["condition_values"])) : 0}"
55+
listener_arn = "${var.balancer["listener_https"]}"
56+
priority = "${var.balancer["priority"] + count.index}"
57+
58+
action {
59+
type = "forward"
60+
target_group_arn = "${aws_alb_target_group.this.0.arn}"
61+
}
62+
63+
condition {
64+
field = "${var.balancer["condition_field"]}"
65+
values = ["${element(split(",", var.balancer["condition_values"]), count.index)}"]
66+
}
67+
}
68+
69+
resource "aws_ecs_service" "this" {
70+
count = "${var.balancer["vpc_id"] != "" && var.cluster != "" ? 1 : 0}"
71+
name = "${local.id}"
72+
cluster = "${var.cluster}"
73+
task_definition = "${aws_ecs_task_definition.this.arn}"
74+
desired_count = "${var.desired}"
75+
iam_role = "arn:aws:iam::159895783284:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS"
76+
health_check_grace_period_seconds = 0
77+
78+
load_balancer {
79+
target_group_arn = "${aws_alb_target_group.this.0.arn}"
80+
container_name = "${var.balancer["container_name"]}"
81+
container_port = "${var.balancer["container_port"]}"
82+
}
83+
84+
ordered_placement_strategy {
85+
type = "spread"
86+
field = "host"
87+
}
88+
}
89+
90+
resource "aws_ecs_service" "no_balancer" {
91+
count = "${var.balancer["vpc_id"] != "" || var.cluster == "" ? 0 : 1}"
92+
name = "${local.id}"
93+
cluster = "${var.cluster}"
94+
task_definition = "${aws_ecs_task_definition.this.arn}"
95+
desired_count = "${var.desired}"
96+
health_check_grace_period_seconds = 0
97+
98+
ordered_placement_strategy {
99+
type = "spread"
100+
field = "host"
101+
}
102+
}
103+
104+
resource "aws_iam_role" "this" {
105+
name = "${local.id}"
106+
description = "${var.name} ECSTask"
107+
assume_role_policy = <<EOF
108+
{
109+
"Version": "2012-10-17",
110+
"Statement": [
111+
{
112+
"Effect": "Allow",
113+
"Principal": {
114+
"Service": "ecs-tasks.amazonaws.com"
115+
},
116+
"Action": "sts:AssumeRole"
117+
}
118+
]
119+
}
120+
EOF
121+
}
122+
123+
module "logs" {
124+
source = "Aplyca/cloudwatchlogs/aws"
125+
version = "0.1.0"
126+
127+
name = "${local.id}"
128+
role = "${aws_iam_role.this.name}"
129+
description = "${var.name} ECSTask CloudWatch Logs"
130+
tags = "${merge(var.tags, map("Name", "${var.name}"))}"
131+
}

outputs.tf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "repositories" {
2+
value = "${merge(var.definition_vars, zipmap(keys(var.repositories), aws_ecr_repository.this.*.repository_url))}"
3+
}
4+
5+
output "role" {
6+
value = "${aws_iam_role.this.name}"
7+
}
8+
9+
output "role_arn" {
10+
value = "${aws_iam_role.this.arn}"
11+
}

variables.tf

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
variable "name" {
2+
description = "Name prefix for all VPC resources."
3+
default = "App"
4+
}
5+
6+
variable "cluster" {
7+
description = "ECS cluster name"
8+
default = ""
9+
}
10+
11+
variable "tags" {
12+
description = "A mapping of tags to assign to the resource."
13+
default = {}
14+
}
15+
16+
variable "definition_file" {
17+
description = "Container definition JSON file"
18+
default = ""
19+
}
20+
21+
variable "definition_vars" {
22+
description = "Container definition vars"
23+
default = {}
24+
}
25+
26+
variable "balancer" {
27+
description = "Listener configurations for ALB"
28+
default = {
29+
vpc_id = ""
30+
condition_values = ""
31+
container_name = ""
32+
container_port = 0
33+
}
34+
}
35+
36+
variable "volumes" {
37+
description = "Volumes"
38+
default = []
39+
}
40+
41+
variable "desired" {
42+
description = "Desired count of tasks in service"
43+
default = 0
44+
}
45+
46+
variable "repositories" {
47+
description = "Images repositories"
48+
default = {}
49+
}
50+
51+
variable "compatibilities" {
52+
description = "Requires compatibilities"
53+
default = []
54+
}

0 commit comments

Comments
 (0)