Skip to content

Commit 6ac63e9

Browse files
authored
feat: Added capacity providers options to ECS cluster (#25)
1 parent 5ac7b15 commit 6ac63e9

File tree

9 files changed

+103
-28
lines changed

9 files changed

+103
-28
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
repos:
22
- repo: git://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.43.0
3+
rev: v1.44.0
44
hooks:
55
- id: terraform_fmt
6+
- id: terraform_validate
67
- id: terraform_docs
78
- id: terraform_tflint
89
args:
@@ -20,6 +21,6 @@ repos:
2021
- '--args=--only=terraform_standard_module_structure'
2122
- '--args=--only=terraform_workspace_remote'
2223
- repo: git://github.com/pre-commit/pre-commit-hooks
23-
rev: v3.2.0
24+
rev: v3.3.0
2425
hooks:
2526
- id: check-merge-conflict

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ module "ecs" {
2222
source = "terraform-aws-modules/ecs/aws"
2323
2424
name = "my-ecs"
25+
26+
container_insights = true
27+
28+
capacity_providers = ["FARGATE", "FARGATE_SPOT"]
29+
30+
default_capacity_provider_strategy = {
31+
capacity_provider = "FARGATE_SPOT"
32+
}
33+
34+
tags = {
35+
Environment = "Development"
36+
}
2537
}
2638
```
2739

@@ -49,22 +61,24 @@ module "ecs" {
4961

5062
| Name | Version |
5163
|------|---------|
52-
| terraform | >= 0.12.6, < 0.14 |
53-
| aws | >= 2.0, < 4.0 |
64+
| terraform | >= 0.12.6 |
65+
| aws | >= 2.48 |
5466

5567
## Providers
5668

5769
| Name | Version |
5870
|------|---------|
59-
| aws | >= 2.0, < 4.0 |
71+
| aws | >= 2.48 |
6072

6173
## Inputs
6274

6375
| Name | Description | Type | Default | Required |
6476
|------|-------------|------|---------|:--------:|
77+
| capacity\_providers | List of short names of one or more capacity providers to associate with the cluster. Valid values also include FARGATE and FARGATE\_SPOT. | `list(string)` | `[]` | no |
6578
| container\_insights | Controls if ECS Cluster has container insights enabled | `bool` | `false` | no |
6679
| create\_ecs | Controls if ECS should be created | `bool` | `true` | no |
67-
| name | Name to be used on all the resources as identifier, also the name of the ECS cluster | `string` | n/a | yes |
80+
| default\_capacity\_provider\_strategy | The capacity provider strategy to use by default for the cluster. Can be one or more. | `map(any)` | `{}` | no |
81+
| name | Name to be used on all the resources as identifier, also the name of the ECS cluster | `string` | `null` | no |
6882
| tags | A map of tags to add to ECS Cluster | `map(string)` | `{}` | no |
6983

7084
## Outputs

examples/complete-ecs/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ Current version creates an high-available VPC with instances that are attached t
4343

4444
| Name | Version |
4545
|------|---------|
46-
| terraform | >= 0.12.6, < 0.14 |
47-
| aws | >= 2.0, < 4.0 |
48-
| template | ~> 2.0 |
46+
| terraform | >= 0.12.6 |
47+
| aws | >= 2.48 |
48+
| template | >= 2.0 |
4949

5050
## Providers
5151

5252
| Name | Version |
5353
|------|---------|
54-
| aws | >= 2.0, < 4.0 |
55-
| template | ~> 2.0 |
54+
| aws | >= 2.48 |
55+
| template | >= 2.0 |
5656

5757
## Inputs
5858

examples/complete-ecs/main.tf

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module "vpc" {
2222
private_subnets = ["10.1.1.0/24", "10.1.2.0/24"]
2323
public_subnets = ["10.1.11.0/24", "10.1.12.0/24"]
2424

25-
enable_nat_gateway = true
25+
enable_nat_gateway = false # false is just faster
2626

2727
tags = {
2828
Environment = local.environment
@@ -32,20 +32,45 @@ module "vpc" {
3232

3333
#----- ECS --------
3434
module "ecs" {
35-
source = "../../"
35+
source = "../../"
36+
3637
name = local.name
3738
container_insights = true
39+
40+
capacity_providers = ["FARGATE", "FARGATE_SPOT", aws_ecs_capacity_provider.prov1.name]
41+
42+
default_capacity_provider_strategy = {
43+
capacity_provider = aws_ecs_capacity_provider.prov1.name # "FARGATE_SPOT"
44+
}
45+
46+
tags = {
47+
Environment = local.environment
48+
}
3849
}
3950

4051
module "ec2_profile" {
4152
source = "../../modules/ecs-instance-profile"
42-
name = local.name
53+
54+
name = local.name
55+
56+
tags = {
57+
Environment = local.environment
58+
}
4359
}
4460

45-
#----- ECS Services--------
61+
resource "aws_ecs_capacity_provider" "prov1" {
62+
name = "prov1"
63+
64+
auto_scaling_group_provider {
65+
auto_scaling_group_arn = module.asg.this_autoscaling_group_arn
66+
}
67+
68+
}
4669

70+
#----- ECS Services--------
4771
module "hello_world" {
48-
source = "./service-hello-world"
72+
source = "./service-hello-world"
73+
4974
cluster_id = module.ecs.this_ecs_cluster_id
5075
}
5176

@@ -68,7 +93,7 @@ data "aws_ami" "amazon_linux_ecs" {
6893
}
6994
}
7095

71-
module "this" {
96+
module "asg" {
7297
source = "terraform-aws-modules/autoscaling/aws"
7398
version = "~> 3.0"
7499

@@ -89,7 +114,7 @@ module "this" {
89114
health_check_type = "EC2"
90115
min_size = 0
91116
max_size = 2
92-
desired_capacity = 0
117+
desired_capacity = 0 # we don't need them for the example
93118
wait_for_capacity_timeout = 0
94119

95120
tags = [
@@ -113,3 +138,12 @@ data "template_file" "user_data" {
113138
cluster_name = local.name
114139
}
115140
}
141+
142+
###################
143+
# Disabled cluster
144+
###################
145+
module "disabled_ecs" {
146+
source = "../../"
147+
148+
create_ecs = false
149+
}

examples/complete-ecs/versions.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
terraform {
2-
required_version = ">= 0.12.6, < 0.14"
2+
required_version = ">= 0.12.6"
33

44
required_providers {
5-
aws = ">= 2.0, < 4.0"
6-
template = "~> 2.0"
5+
aws = ">= 2.48"
6+
template = ">= 2.0"
77
}
88
}

main.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ resource "aws_ecs_cluster" "this" {
33

44
name = var.name
55

6+
capacity_providers = var.capacity_providers
7+
8+
dynamic "default_capacity_provider_strategy" {
9+
for_each = length(keys(var.default_capacity_provider_strategy)) == 0 ? [] : [var.default_capacity_provider_strategy]
10+
iterator = strategy
11+
12+
content {
13+
capacity_provider = strategy.value["capacity_provider"]
14+
weight = lookup(strategy.value, "weight", null)
15+
base = lookup(strategy.value, "base", null)
16+
}
17+
}
18+
619
setting {
720
name = "containerInsights"
821
value = var.container_insights ? "enabled" : "disabled"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
2-
required_version = ">= 0.12.6, < 0.14"
2+
required_version = ">= 0.12.6"
33

44
required_providers {
5-
aws = ">= 2.0, < 4.0"
5+
aws = ">= 2.48"
66
}
77
}

variables.tf

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ variable "create_ecs" {
77
variable "name" {
88
description = "Name to be used on all the resources as identifier, also the name of the ECS cluster"
99
type = string
10+
default = null
1011
}
1112

12-
variable "tags" {
13-
description = "A map of tags to add to ECS Cluster"
14-
type = map(string)
13+
variable "capacity_providers" {
14+
description = "List of short names of one or more capacity providers to associate with the cluster. Valid values also include FARGATE and FARGATE_SPOT."
15+
type = list(string)
16+
default = []
17+
}
18+
19+
variable "default_capacity_provider_strategy" {
20+
description = "The capacity provider strategy to use by default for the cluster. Can be one or more."
21+
type = map(any)
1522
default = {}
1623
}
1724

@@ -20,3 +27,9 @@ variable "container_insights" {
2027
type = bool
2128
default = false
2229
}
30+
31+
variable "tags" {
32+
description = "A map of tags to add to ECS Cluster"
33+
type = map(string)
34+
default = {}
35+
}

versions.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
2-
required_version = ">= 0.12.6, < 0.14"
2+
required_version = ">= 0.12.6"
33

44
required_providers {
5-
aws = ">= 2.0, < 4.0"
5+
aws = ">= 2.48"
66
}
77
}

0 commit comments

Comments
 (0)