diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0c40609..8677870 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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: @@ -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 diff --git a/README.md b/README.md index 29edf02..6f829ee 100644 --- a/README.md +++ b/README.md @@ -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" + } } ``` @@ -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 diff --git a/examples/complete-ecs/README.md b/examples/complete-ecs/README.md index c50888b..68e917f 100644 --- a/examples/complete-ecs/README.md +++ b/examples/complete-ecs/README.md @@ -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 diff --git a/examples/complete-ecs/main.tf b/examples/complete-ecs/main.tf index 3976be5..9c90dc8 100644 --- a/examples/complete-ecs/main.tf +++ b/examples/complete-ecs/main.tf @@ -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 @@ -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 } @@ -68,7 +93,7 @@ data "aws_ami" "amazon_linux_ecs" { } } -module "this" { +module "asg" { source = "terraform-aws-modules/autoscaling/aws" version = "~> 3.0" @@ -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 = [ @@ -113,3 +138,12 @@ data "template_file" "user_data" { cluster_name = local.name } } + +################### +# Disabled cluster +################### +module "disabled_ecs" { + source = "../../" + + create_ecs = false +} diff --git a/examples/complete-ecs/versions.tf b/examples/complete-ecs/versions.tf index 2016f1b..54f4347 100644 --- a/examples/complete-ecs/versions.tf +++ b/examples/complete-ecs/versions.tf @@ -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" } } diff --git a/main.tf b/main.tf index 2143b08..f1ab4f4 100644 --- a/main.tf +++ b/main.tf @@ -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" diff --git a/modules/ecs-instance-profile/versions.tf b/modules/ecs-instance-profile/versions.tf index 0e62042..d07d3aa 100644 --- a/modules/ecs-instance-profile/versions.tf +++ b/modules/ecs-instance-profile/versions.tf @@ -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" } } diff --git a/variables.tf b/variables.tf index ac7c658..e27133b 100644 --- a/variables.tf +++ b/variables.tf @@ -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 = {} } @@ -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 = {} +} diff --git a/versions.tf b/versions.tf index 0e62042..d07d3aa 100644 --- a/versions.tf +++ b/versions.tf @@ -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" } }