Skip to content

Commit

Permalink
feat: Added capacity providers options to ECS cluster (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbabenko authored Nov 9, 2020
1 parent 5ac7b15 commit 6ac63e9
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 28 deletions.
5 changes: 3 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
repos:
- repo: git://github.com/antonbabenko/pre-commit-terraform
rev: v1.43.0
rev: v1.44.0
hooks:
- id: terraform_fmt
- id: terraform_validate
- id: terraform_docs
- id: terraform_tflint
args:
Expand All @@ -20,6 +21,6 @@ repos:
- '--args=--only=terraform_standard_module_structure'
- '--args=--only=terraform_workspace_remote'
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
rev: v3.3.0
hooks:
- id: check-merge-conflict
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ module "ecs" {
source = "terraform-aws-modules/ecs/aws"
name = "my-ecs"
container_insights = true
capacity_providers = ["FARGATE", "FARGATE_SPOT"]
default_capacity_provider_strategy = {
capacity_provider = "FARGATE_SPOT"
}
tags = {
Environment = "Development"
}
}
```

Expand Down Expand Up @@ -49,22 +61,24 @@ module "ecs" {

| Name | Version |
|------|---------|
| terraform | >= 0.12.6, < 0.14 |
| aws | >= 2.0, < 4.0 |
| terraform | >= 0.12.6 |
| aws | >= 2.48 |

## Providers

| Name | Version |
|------|---------|
| aws | >= 2.0, < 4.0 |
| aws | >= 2.48 |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| 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 |
| container\_insights | Controls if ECS Cluster has container insights enabled | `bool` | `false` | no |
| create\_ecs | Controls if ECS should be created | `bool` | `true` | no |
| name | Name to be used on all the resources as identifier, also the name of the ECS cluster | `string` | n/a | yes |
| default\_capacity\_provider\_strategy | The capacity provider strategy to use by default for the cluster. Can be one or more. | `map(any)` | `{}` | no |
| name | Name to be used on all the resources as identifier, also the name of the ECS cluster | `string` | `null` | no |
| tags | A map of tags to add to ECS Cluster | `map(string)` | `{}` | no |

## Outputs
Expand Down
10 changes: 5 additions & 5 deletions examples/complete-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ Current version creates an high-available VPC with instances that are attached t

| Name | Version |
|------|---------|
| terraform | >= 0.12.6, < 0.14 |
| aws | >= 2.0, < 4.0 |
| template | ~> 2.0 |
| terraform | >= 0.12.6 |
| aws | >= 2.48 |
| template | >= 2.0 |

## Providers

| Name | Version |
|------|---------|
| aws | >= 2.0, < 4.0 |
| template | ~> 2.0 |
| aws | >= 2.48 |
| template | >= 2.0 |

## Inputs

Expand Down
48 changes: 41 additions & 7 deletions examples/complete-ecs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module "vpc" {
private_subnets = ["10.1.1.0/24", "10.1.2.0/24"]
public_subnets = ["10.1.11.0/24", "10.1.12.0/24"]

enable_nat_gateway = true
enable_nat_gateway = false # false is just faster

tags = {
Environment = local.environment
Expand All @@ -32,20 +32,45 @@ module "vpc" {

#----- ECS --------
module "ecs" {
source = "../../"
source = "../../"

name = local.name
container_insights = true

capacity_providers = ["FARGATE", "FARGATE_SPOT", aws_ecs_capacity_provider.prov1.name]

default_capacity_provider_strategy = {
capacity_provider = aws_ecs_capacity_provider.prov1.name # "FARGATE_SPOT"
}

tags = {
Environment = local.environment
}
}

module "ec2_profile" {
source = "../../modules/ecs-instance-profile"
name = local.name

name = local.name

tags = {
Environment = local.environment
}
}

#----- ECS Services--------
resource "aws_ecs_capacity_provider" "prov1" {
name = "prov1"

auto_scaling_group_provider {
auto_scaling_group_arn = module.asg.this_autoscaling_group_arn
}

}

#----- ECS Services--------
module "hello_world" {
source = "./service-hello-world"
source = "./service-hello-world"

cluster_id = module.ecs.this_ecs_cluster_id
}

Expand All @@ -68,7 +93,7 @@ data "aws_ami" "amazon_linux_ecs" {
}
}

module "this" {
module "asg" {
source = "terraform-aws-modules/autoscaling/aws"
version = "~> 3.0"

Expand All @@ -89,7 +114,7 @@ module "this" {
health_check_type = "EC2"
min_size = 0
max_size = 2
desired_capacity = 0
desired_capacity = 0 # we don't need them for the example
wait_for_capacity_timeout = 0

tags = [
Expand All @@ -113,3 +138,12 @@ data "template_file" "user_data" {
cluster_name = local.name
}
}

###################
# Disabled cluster
###################
module "disabled_ecs" {
source = "../../"

create_ecs = false
}
6 changes: 3 additions & 3 deletions examples/complete-ecs/versions.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
terraform {
required_version = ">= 0.12.6, < 0.14"
required_version = ">= 0.12.6"

required_providers {
aws = ">= 2.0, < 4.0"
template = "~> 2.0"
aws = ">= 2.48"
template = ">= 2.0"
}
}
13 changes: 13 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ resource "aws_ecs_cluster" "this" {

name = var.name

capacity_providers = var.capacity_providers

dynamic "default_capacity_provider_strategy" {
for_each = length(keys(var.default_capacity_provider_strategy)) == 0 ? [] : [var.default_capacity_provider_strategy]
iterator = strategy

content {
capacity_provider = strategy.value["capacity_provider"]
weight = lookup(strategy.value, "weight", null)
base = lookup(strategy.value, "base", null)
}
}

setting {
name = "containerInsights"
value = var.container_insights ? "enabled" : "disabled"
Expand Down
4 changes: 2 additions & 2 deletions modules/ecs-instance-profile/versions.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_version = ">= 0.12.6, < 0.14"
required_version = ">= 0.12.6"

required_providers {
aws = ">= 2.0, < 4.0"
aws = ">= 2.48"
}
}
19 changes: 16 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ variable "create_ecs" {
variable "name" {
description = "Name to be used on all the resources as identifier, also the name of the ECS cluster"
type = string
default = null
}

variable "tags" {
description = "A map of tags to add to ECS Cluster"
type = map(string)
variable "capacity_providers" {
description = "List of short names of one or more capacity providers to associate with the cluster. Valid values also include FARGATE and FARGATE_SPOT."
type = list(string)
default = []
}

variable "default_capacity_provider_strategy" {
description = "The capacity provider strategy to use by default for the cluster. Can be one or more."
type = map(any)
default = {}
}

Expand All @@ -20,3 +27,9 @@ variable "container_insights" {
type = bool
default = false
}

variable "tags" {
description = "A map of tags to add to ECS Cluster"
type = map(string)
default = {}
}
4 changes: 2 additions & 2 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_version = ">= 0.12.6, < 0.14"
required_version = ">= 0.12.6"

required_providers {
aws = ">= 2.0, < 4.0"
aws = ">= 2.48"
}
}

0 comments on commit 6ac63e9

Please sign in to comment.