Skip to content

Commit fdc82f7

Browse files
initial changes
1 parent bfbd38f commit fdc82f7

File tree

8 files changed

+122
-77
lines changed

8 files changed

+122
-77
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License
2+
3+
Copyright (c) 2010-2019 Google, Inc. http://angularjs.org
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
13+
all 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
21+
THE SOFTWARE.
22+

README.md

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,42 @@
1-
# terraform-aws-ecs-cluster for terraform >= 0.12
21

3-
This module configures ECS-cluster consisting of number of ec2 instances. It supports spot ec2 instances, which might be
4-
cost effective for a development environment that allows some downtime.
2+
# aws-ecs-cluster
3+
A modified version of https://github.com/springload/terraform-aws-ecs-cluster
54

6-
## Required variables
5+
## Usage
76

8-
Only `cluster_name` is required to be set.
7+
```hcl
8+
module "ecs-cluster" {
9+
source = "github.com/globeandmail/aws-ecs-cluster?ref=1.0"
10+
11+
cluster_name = my-ecs-cluster
12+
instance_type = var.instance_type
13+
subnet_ids = var.subnet_ids
14+
security_group_ids = var.security_group_ids
15+
vpc_id = var.vpc_id
16+
}
17+
```
18+
19+
## Inputs
20+
21+
| Name | Description | Type | Default | Required |
22+
|------|-------------|:----:|:-----:|:-----:|
23+
| cluster\_name | Name of the ECS cluster | string | n/a | yes |
24+
| ami | Name of the AMI image to use | string | `"amzn2-ami-ecs-hvm-*-x86_64-ebs"` | no |
25+
| cpu\_unlimited | Whether or not enable t2/t3 cpu unlimited \(if true, might incur additional charges\) | string | `"false"` | no |
26+
| ec2\_key\_name | EC2 key name to attach to newly created EC2 instances | string | `""` | no |
27+
| instance\_type | EC2 instance type | string | `"t3.micro"` | no |
28+
| instances\_desired | Number of EC2 instances desired | string | `"1"` | no |
29+
| security\_group\_ids | list of security group IDs | list | `[]` | no |
30+
| spot | Whether or not use Spot instances. Warning: most likely not suitable for production! | string | `"false"` | no |
31+
| subnet\_ids | list of subnet IDs | list | `[]` | no |
32+
| tags | AWS tags | map | `{}` | no |
33+
| vpc\_id | VPC id to use | string | `""` | no |
34+
35+
## Outputs
36+
37+
| Name | Description |
38+
|------|-------------|
39+
| ecs\_cluster\_arn | The ARN of this cluster |
40+
| ecs\_cluster\_id | The ARN of this cluster |
41+
| ecs\_nodes\_role\_arn | The ARN of the ec2 role for the cluster nodes |
42+
| ecs\_nodes\_role\_id | The ID of the ec2 role for the cluster nodes |

cloud-init.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ data "template_cloudinit_config" "config" {
77

88
content = <<-EOT
99
#!/bin/bash
10-
echo "ECS_CLUSTER=${aws_ecs_cluster.main.name}" >> /etc/ecs/ecs.config
10+
echo "ECS_CLUSTER=${aws_ecs_cluster.cluster.name}" >> /etc/ecs/ecs.config
1111
1212
EOT
1313
}

data.tf

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,8 @@
1-
data "aws_vpc" "main" {
2-
default = var.vpc_name == "" ? true : false
3-
4-
tags = var.vpc_name != "" ? {
5-
Name = "${var.vpc_name}"
6-
} : {}
7-
}
8-
91
data "aws_ami" "ami" {
102
most_recent = true
11-
123
filter {
13-
name = "name"
14-
4+
name = "name"
155
values = [var.ami]
166
}
17-
187
owners = ["amazon"]
198
}
20-
21-
data "aws_security_group" "group" {
22-
filter {
23-
name = "group-name"
24-
values = var.security_groups
25-
}
26-
27-
filter {
28-
name = "vpc-id"
29-
values = [data.aws_vpc.main.id]
30-
}
31-
}
32-
33-
data "aws_subnet_ids" "subnets" {
34-
vpc_id = data.aws_vpc.main.id
35-
}
36-

iam.tf

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ locals {
2626
}
2727

2828
resource "aws_iam_role_policy_attachment" "ec2-role-attach" {
29-
role = aws_iam_role.ec2-role.name
30-
31-
count = length(local.managed_roles)
29+
role = aws_iam_role.ec2-role.name
30+
count = length(local.managed_roles)
3231
policy_arn = element(local.managed_roles, count.index)
3332
}
3433

3534
resource "aws_iam_instance_profile" "ec2-instance-role" {
3635
name = var.cluster_name
3736
role = aws_iam_role.ec2-role.name
3837
}
39-

main.tf

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "aws_launch_template" "LT" {
2-
name = "${var.cluster_name}-spot"
2+
name = var.cluster_name
33

44
dynamic instance_market_options {
55
for_each = var.spot ? [1] : []
@@ -15,7 +15,7 @@ resource "aws_launch_template" "LT" {
1515

1616
image_id = data.aws_ami.ami.id
1717
instance_type = var.instance_type
18-
vpc_security_group_ids = [data.aws_security_group.group.id]
18+
vpc_security_group_ids = var.security_group_ids
1919

2020
iam_instance_profile {
2121
arn = aws_iam_instance_profile.ec2-instance-role.arn
@@ -24,41 +24,40 @@ resource "aws_launch_template" "LT" {
2424
key_name = var.ec2_key_name
2525
user_data = data.template_cloudinit_config.config.rendered
2626
depends_on = [aws_iam_instance_profile.ec2-instance-role]
27-
}
28-
29-
resource "aws_autoscaling_group" "ASG" {
30-
name = var.cluster_name
31-
max_size = var.instances_desired
32-
min_size = var.instances_desired
3327

34-
desired_capacity = var.instances_desired
28+
tags = var.tags
29+
}
3530

36-
force_delete = true
31+
resource "aws_autoscaling_group" "asg" {
32+
name = var.cluster_name
33+
max_size = var.instances_desired
34+
min_size = var.instances_desired
35+
desired_capacity = var.instances_desired
36+
force_delete = true
37+
vpc_zone_identifier = var.subnet_ids
38+
termination_policies = ["OldestInstance"]
3739

3840
launch_template {
3941
id = aws_launch_template.LT.id
4042
version = "$Latest"
4143
}
4244

43-
vpc_zone_identifier = coalescelist(var.subnet_ids, list(data.aws_subnet_ids.subnets.ids))
44-
termination_policies = ["OldestInstance"]
45-
46-
tag {
47-
key = "Name"
48-
value = "${var.cluster_name}-ecs"
49-
propagate_at_launch = true
50-
}
51-
52-
tag {
53-
key = "ecs-cluster"
54-
value = var.cluster_name
55-
propagate_at_launch = true
56-
}
45+
tags = [
46+
{
47+
key = "Name"
48+
value = "${var.cluster_name}-ecs"
49+
propagate_at_launch = true
50+
},
51+
{
52+
key = "ecs-cluster"
53+
value = var.cluster_name
54+
propagate_at_launch = true
55+
},
56+
]
5757

5858
depends_on = [aws_iam_instance_profile.ec2-instance-role]
5959
}
6060

61-
resource "aws_ecs_cluster" "main" {
61+
resource "aws_ecs_cluster" "cluster" {
6262
name = var.cluster_name
6363
}
64-

outputs.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
output "ecs_cluster_arn" {
2+
value = aws_ecs_cluster.cluster.arn
3+
description = "The ARN of this cluster"
4+
}
5+
6+
output "ecs_cluster_id" {
7+
value = aws_ecs_cluster.cluster.id
8+
description = "The ARN of this cluster"
9+
}
10+
11+
output "ecs_nodes_role_arn" {
12+
value = aws_iam_role.ec2-role.arn
13+
description = "The ARN of the ec2 role for the cluster nodes"
14+
}
15+
16+
output "ecs_nodes_role_id" {
17+
value = aws_iam_role.ec2-role.id
18+
description = "The ID of the ec2 role for the cluster nodes"
19+
}

variables.tf

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
# required variables
2-
#
31
variable "cluster_name" {
42
description = "Name of the ECS cluster"
53
}
64

7-
# optional variables
8-
#
9-
105
variable "ami" {
116
description = "Name of the AMI image to use"
127
default = "amzn2-ami-ecs-hvm-*-x86_64-ebs"
@@ -32,15 +27,15 @@ variable "instances_desired" {
3227
default = 1
3328
}
3429

35-
variable "security_groups" {
36-
description = "list of security group names"
37-
type = "list"
30+
variable "security_group_ids" {
31+
description = "list of security group IDs"
32+
type = list
3833
default = []
3934
}
4035

4136
variable "subnet_ids" {
42-
description = "list of subnet ids. By default takes all subnets from the VPC"
43-
type = "list"
37+
description = "list of subnet IDs"
38+
type = list
4439
default = []
4540
}
4641

@@ -49,7 +44,13 @@ variable "spot" {
4944
default = false
5045
}
5146

52-
variable "vpc_name" {
53-
description = "VPC name. If not set, will default to \"default\""
47+
variable "vpc_id" {
48+
description = "VPC id to use"
5449
default = ""
5550
}
51+
52+
variable "tags" {
53+
type = map
54+
description = "AWS tags"
55+
default = {}
56+
}

0 commit comments

Comments
 (0)