From 29c8ec19c5d63307a56814ff5e9debc30aa2ee35 Mon Sep 17 00:00:00 2001 From: lucasmacedot Date: Sun, 25 Aug 2024 21:07:19 -0400 Subject: [PATCH 01/10] Add timezone var --- _variables.tf | 8 ++++++++ appautoscaling.tf | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/_variables.tf b/_variables.tf index 09b1488..958b8e6 100644 --- a/_variables.tf +++ b/_variables.tf @@ -502,6 +502,10 @@ variable "enable_schedule" { description = "Enables schedule to shut down and start up instances outside business hours." } +variable "schedules_stop" { + +} + variable "schedule_cron_start" { type = string default = "" @@ -530,3 +534,7 @@ variable "task_role_policies" { description = "Custom policies to be added on the task role." } +variable "timezone" { + type = string + default = "UTC" +} diff --git a/appautoscaling.tf b/appautoscaling.tf index 15a630d..05ee817 100644 --- a/appautoscaling.tf +++ b/appautoscaling.tf @@ -84,7 +84,7 @@ resource "aws_appautoscaling_scheduled_action" "scale_service_out" { resource_id = aws_appautoscaling_target.ecs[0].resource_id scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension schedule = var.schedule_cron_stop - timezone = "UTC" + timezone = var.timezone scalable_target_action { min_capacity = 0 @@ -99,7 +99,7 @@ resource "aws_appautoscaling_scheduled_action" "scale_service_in" { resource_id = aws_appautoscaling_target.ecs[0].resource_id scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension schedule = var.schedule_cron_start - timezone = "UTC" + timezone = var.timezone scalable_target_action { min_capacity = var.autoscaling_min From 9bf47f886bb5b8e149a9a95d5dab9bd49dd2df6b Mon Sep 17 00:00:00 2001 From: lucasmacedot Date: Sun, 25 Aug 2024 20:47:08 -0400 Subject: [PATCH 02/10] Add multiple crons to start and stop app --- _variables.tf | 20 ++++++++++++++++++-- appautoscaling.tf | 3 ++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/_variables.tf b/_variables.tf index 958b8e6..7aef9ad 100644 --- a/_variables.tf +++ b/_variables.tf @@ -1,29 +1,35 @@ variable "name" { + type = string description = "Name of your ECS service" } variable "container_port" { default = 8080 + type = string description = "Port your container listens (used in the placeholder task definition)" } variable "port" { default = 80 + type = number description = "Port for target group to listen" } variable "protocol" { default = "HTTP" + type = string description = "Protocol to use (HTTP or HTTPS)" } variable "memory" { default = 512 + type = number description = "Hard memory of the container" } variable "cpu" { default = 0 + type = number description = "Hard limit for CPU for the container" } @@ -34,32 +40,38 @@ variable "paths" { } variable "hosted_zone_is_internal" { - default = "false" + default = false + type = bool description = "Set true in case the hosted zone is in an internal VPC, otherwise false" } variable "hosted_zone" { default = "" + type = string description = "Hosted Zone to create DNS record for this app" } variable "hosted_zone_id" { default = "" + type = string description = "Hosted Zone ID to create DNS record for this app (use this to avoid data lookup when using `hosted_zone`)" } variable "hostname_create" { - default = "false" + default = false + type = bool description = "Optional parameter to create or not a Route53 record" } variable "hostnames" { default = [] + type = list(string) description = "List of hostnames to create listerner rule and optionally, DNS records for this app" } variable "source_ips" { default = [] + type = list(string) description = "List of source ip to use on listerner rule" } @@ -72,11 +84,13 @@ variable "http_header" { variable "hostname_redirects" { description = "List of hostnames to redirect to the main one, comma-separated" default = "" + type = string } variable "healthcheck_path" { default = "/" + type = string } variable "healthcheck_interval" { @@ -526,11 +540,13 @@ variable "command" { variable "task_role_policies_managed" { default = [] + type = list(string) description = "AWS Managed policies to be added on the task role." } variable "task_role_policies" { default = [] + type = list(string) description = "Custom policies to be added on the task role." } diff --git a/appautoscaling.tf b/appautoscaling.tf index 05ee817..15fe80e 100644 --- a/appautoscaling.tf +++ b/appautoscaling.tf @@ -105,4 +105,5 @@ resource "aws_appautoscaling_scheduled_action" "scale_service_in" { min_capacity = var.autoscaling_min max_capacity = var.autoscaling_max } -} \ No newline at end of file +} + From 952c2f080c3ee648e1bc4102e9c04dc571e4dbee Mon Sep 17 00:00:00 2001 From: lucasmacedot Date: Mon, 26 Aug 2024 01:10:53 +0000 Subject: [PATCH 03/10] terraform-docs: automated update action --- README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 944155d..354d778 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ In addition you have the option to create or not : | codedeploy\_wait\_time\_for\_termination | Time in minutes to terminate the new deployment | `number` | `0` | no | | command | Command to run on container | `list(string)` | `null` | no | | compat\_keep\_target\_group\_naming | Keeps old naming convention for target groups to avoid recreation of resource in production environments | `bool` | `false` | no | -| container\_port | Port your container listens (used in the placeholder task definition) | `number` | `8080` | no | +| container\_port | Port your container listens (used in the placeholder task definition) | `string` | `8080` | no | | cpu | Hard limit for CPU for the container | `number` | `0` | no | | create\_iam\_codedeployrole | Create Codedeploy IAM Role for ECS or not. | `bool` | `true` | no | | deployment\_controller | Type of deployment controller. Valid values: CODE\_DEPLOY, ECS, EXTERNAL. | `string` | `"CODE_DEPLOY"` | no | @@ -116,10 +116,10 @@ In addition you have the option to create or not : | healthy\_threshold | The number of consecutive health checks successes required before considering an unhealthy target healthy | `number` | `3` | no | | hosted\_zone | Hosted Zone to create DNS record for this app | `string` | `""` | no | | hosted\_zone\_id | Hosted Zone ID to create DNS record for this app (use this to avoid data lookup when using `hosted_zone`) | `string` | `""` | no | -| hosted\_zone\_is\_internal | Set true in case the hosted zone is in an internal VPC, otherwise false | `string` | `"false"` | no | -| hostname\_create | Optional parameter to create or not a Route53 record | `string` | `"false"` | no | +| hosted\_zone\_is\_internal | Set true in case the hosted zone is in an internal VPC, otherwise false | `bool` | `false` | no | +| hostname\_create | Optional parameter to create or not a Route53 record | `bool` | `false` | no | | hostname\_redirects | List of hostnames to redirect to the main one, comma-separated | `string` | `""` | no | -| hostnames | List of hostnames to create listerner rule and optionally, DNS records for this app | `list` | `[]` | no | +| hostnames | List of hostnames to create listerner rule and optionally, DNS records for this app | `list(string)` | `[]` | no | | http\_header | Header to use on listerner rule with name e values | `list(any)` | `[]` | no | | image | Docker image to deploy (can be a placeholder) | `string` | `""` | no | | launch\_type | The launch type on which to run your service. The valid values are EC2 and FARGATE. Defaults to EC2. | `string` | `"EC2"` | no | @@ -128,7 +128,7 @@ In addition you have the option to create or not : | log\_subscription\_filter\_filter\_pattern | n/a | `string` | `""` | no | | log\_subscription\_filter\_role\_arn | n/a | `string` | `""` | no | | memory | Hard memory of the container | `number` | `512` | no | -| name | Name of your ECS service | `any` | n/a | yes | +| name | Name of your ECS service | `string` | n/a | yes | | network\_mode | The Docker networking mode to use for the containers in the task. The valid values are none, bridge, awsvpc, and host. (REQUIRED IF 'LAUCH\_TYPE' IS FARGATE) | `any` | `null` | no | | ordered\_placement\_strategy | Service level strategy rules that are taken into consideration during task placement. List from top to bottom in order of precedence. The maximum number of ordered\_placement\_strategy blocks is 5. |
list(object({
field = string
type = string
}))
| `[]` | no | | paths | List of paths to use on listener rule (example: ['/\*']) | `list(string)` | `[]` | no | @@ -139,22 +139,24 @@ In addition you have the option to create or not : | redirects | Map of path redirects to add to the listener | `map` | `{}` | no | | schedule\_cron\_start | Cron expression to define when to trigger a start of the auto-scaling group. E.g. 'cron(00 21 ? \* SUN-THU \*)' to start at 8am UTC time. | `string` | `""` | no | | schedule\_cron\_stop | Cron expression to define when to trigger a stop of the auto-scaling group. E.g. 'cron(00 09 ? \* MON-FRI \*)' to start at 8am UTC time | `string` | `""` | no | +| schedules\_stop | n/a | `any` | n/a | yes | | security\_groups | The security groups associated with the task or service | `any` | `null` | no | | service\_deployment\_maximum\_percent | Maximum percentage of tasks to run during deployments | `number` | `200` | no | | service\_deployment\_minimum\_healthy\_percent | Minimum healthy percentage during deployments | `number` | `100` | no | | service\_desired\_count | Desired count for this service (for use when auto scaling is disabled) | `number` | `1` | no | | service\_health\_check\_grace\_period\_seconds | Time until your container starts serving requests | `number` | `0` | no | | service\_role\_arn | Existing service role ARN created by ECS cluster module | `any` | `null` | no | -| source\_ips | List of source ip to use on listerner rule | `list` | `[]` | no | +| source\_ips | List of source ip to use on listerner rule | `list(string)` | `[]` | no | | ssm\_variables | Map of variables and SSM locations to add to the task definition | `map(string)` | `{}` | no | | static\_variables | Map of variables and static values to add to the task definition | `map(string)` | `{}` | no | | subnets | The subnets associated with the task or service. (REQUIRED IF 'LAUCH\_TYPE' IS FARGATE) | `any` | `null` | no | | tags | Map of tags that will be added to created resources. By default resources will be tagged with terraform=true. | `map(string)` | `{}` | no | | task\_definition\_arn | Task definition to use for this service (optional) | `string` | `""` | no | | task\_role\_arn | Existing task role ARN created by ECS cluster module | `any` | `null` | no | -| task\_role\_policies | Custom policies to be added on the task role. | `list` | `[]` | no | -| task\_role\_policies\_managed | AWS Managed policies to be added on the task role. | `list` | `[]` | no | +| task\_role\_policies | Custom policies to be added on the task role. | `list(string)` | `[]` | no | +| task\_role\_policies\_managed | AWS Managed policies to be added on the task role. | `list(string)` | `[]` | no | | test\_traffic\_route\_listener\_arn | ALB HTTPS Listener for Test Traffic created by ECS cluster module | `any` | n/a | yes | +| timezone | n/a | `string` | `"UTC"` | no | | ulimits | Container ulimit settings. This is a list of maps, where each map should contain "name", "hardLimit" and "softLimit" |
list(object({
name = string
hardLimit = number
softLimit = number
}))
| `null` | no | | unhealthy\_threshold | The number of consecutive health check failures required before considering the target unhealthy | `number` | `3` | no | | vpc\_id | VPC ID to deploy this app to | `any` | n/a | yes | From 19a02e5ab3d59d410e562f1921ef3c57d9a62b38 Mon Sep 17 00:00:00 2001 From: lucasmacedot Date: Sun, 25 Aug 2024 21:15:11 -0400 Subject: [PATCH 04/10] Add `scale_in_schedules` var --- _variables.tf | 9 +++++++-- appautoscaling.tf | 37 +++++++++++++++++++------------------ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/_variables.tf b/_variables.tf index 7aef9ad..99567a4 100644 --- a/_variables.tf +++ b/_variables.tf @@ -513,11 +513,16 @@ variable "alarm_ecs_running_tasks_threshold" { variable "enable_schedule" { default = false + type = bool description = "Enables schedule to shut down and start up instances outside business hours." } -variable "schedules_stop" { - +variable "scale_in_schedules" { + type = list(object({ + cron = string + name = "" + })) + default = null } variable "schedule_cron_start" { diff --git a/appautoscaling.tf b/appautoscaling.tf index 15fe80e..f119de8 100644 --- a/appautoscaling.tf +++ b/appautoscaling.tf @@ -77,13 +77,14 @@ resource "aws_appautoscaling_policy" "scale_custom" { } } -resource "aws_appautoscaling_scheduled_action" "scale_service_out" { +resource "aws_appautoscaling_scheduled_action" "scale_in_schedules" { + for_each = var.scale_in_schedules count = var.enable_schedule ? 1 : 0 - name = "${var.name}-scale-out" + name = "${var.name}-${each.value}-scale-in" service_namespace = aws_appautoscaling_target.ecs[0].service_namespace resource_id = aws_appautoscaling_target.ecs[0].resource_id scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension - schedule = var.schedule_cron_stop + schedule = each.value.cron timezone = var.timezone scalable_target_action { @@ -92,18 +93,18 @@ resource "aws_appautoscaling_scheduled_action" "scale_service_out" { } } -resource "aws_appautoscaling_scheduled_action" "scale_service_in" { - count = var.enable_schedule ? 1 : 0 - name = "${var.name}-scale-in" - service_namespace = aws_appautoscaling_target.ecs[0].service_namespace - resource_id = aws_appautoscaling_target.ecs[0].resource_id - scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension - schedule = var.schedule_cron_start - timezone = var.timezone - - scalable_target_action { - min_capacity = var.autoscaling_min - max_capacity = var.autoscaling_max - } -} - +#resource "aws_appautoscaling_scheduled_action" "scale_service_in" { +# count = var.enable_schedule ? 1 : 0 +# name = "${var.name}-scale-in" +# service_namespace = aws_appautoscaling_target.ecs[0].service_namespace +# resource_id = aws_appautoscaling_target.ecs[0].resource_id +# scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension +# schedule = var.schedule_cron_start +# timezone = var.timezone +# +# scalable_target_action { +# min_capacity = var.autoscaling_min +# max_capacity = var.autoscaling_max +# } +#} +# From 2ec80bea4452610a0b29b0a0917de89f136c673d Mon Sep 17 00:00:00 2001 From: lucasmacedot Date: Sun, 25 Aug 2024 21:17:12 -0400 Subject: [PATCH 05/10] Fix timezone --- _variables.tf | 2 +- appautoscaling.tf | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/_variables.tf b/_variables.tf index 99567a4..d285401 100644 --- a/_variables.tf +++ b/_variables.tf @@ -555,7 +555,7 @@ variable "task_role_policies" { description = "Custom policies to be added on the task role." } -variable "timezone" { +variable "schedule_timezone" { type = string default = "UTC" } diff --git a/appautoscaling.tf b/appautoscaling.tf index f119de8..adef81d 100644 --- a/appautoscaling.tf +++ b/appautoscaling.tf @@ -85,8 +85,7 @@ resource "aws_appautoscaling_scheduled_action" "scale_in_schedules" { resource_id = aws_appautoscaling_target.ecs[0].resource_id scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension schedule = each.value.cron - timezone = var.timezone - + timezone = var.schedule_timezone scalable_target_action { min_capacity = 0 max_capacity = 0 From 2bbeec4b530969520890b8c12aa53bc14e31ab66 Mon Sep 17 00:00:00 2001 From: lucasmacedot Date: Sun, 25 Aug 2024 21:19:07 -0400 Subject: [PATCH 06/10] Fix for_each --- _variables.tf | 2 +- appautoscaling.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/_variables.tf b/_variables.tf index d285401..4681796 100644 --- a/_variables.tf +++ b/_variables.tf @@ -520,7 +520,7 @@ variable "enable_schedule" { variable "scale_in_schedules" { type = list(object({ cron = string - name = "" + name = string })) default = null } diff --git a/appautoscaling.tf b/appautoscaling.tf index adef81d..4dcd0ff 100644 --- a/appautoscaling.tf +++ b/appautoscaling.tf @@ -78,7 +78,7 @@ resource "aws_appautoscaling_policy" "scale_custom" { } resource "aws_appautoscaling_scheduled_action" "scale_in_schedules" { - for_each = var.scale_in_schedules + for_each = { for schedule in var.scale_in_schedules : schedule.name => schedule } count = var.enable_schedule ? 1 : 0 name = "${var.name}-${each.value}-scale-in" service_namespace = aws_appautoscaling_target.ecs[0].service_namespace From aabe93a4aa86d141e2da4527ad8d17805d360579 Mon Sep 17 00:00:00 2001 From: lucasmacedot Date: Sun, 25 Aug 2024 21:20:25 -0400 Subject: [PATCH 07/10] Remove count --- appautoscaling.tf | 1 - 1 file changed, 1 deletion(-) diff --git a/appautoscaling.tf b/appautoscaling.tf index 4dcd0ff..60d890c 100644 --- a/appautoscaling.tf +++ b/appautoscaling.tf @@ -79,7 +79,6 @@ resource "aws_appautoscaling_policy" "scale_custom" { resource "aws_appautoscaling_scheduled_action" "scale_in_schedules" { for_each = { for schedule in var.scale_in_schedules : schedule.name => schedule } - count = var.enable_schedule ? 1 : 0 name = "${var.name}-${each.value}-scale-in" service_namespace = aws_appautoscaling_target.ecs[0].service_namespace resource_id = aws_appautoscaling_target.ecs[0].resource_id From 78248e103d93c36e2b51f500d5ddead47cb90761 Mon Sep 17 00:00:00 2001 From: lucasmacedot Date: Sun, 25 Aug 2024 21:24:37 -0400 Subject: [PATCH 08/10] Fix type of `container_port` --- _variables.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_variables.tf b/_variables.tf index 4681796..28235ea 100644 --- a/_variables.tf +++ b/_variables.tf @@ -5,7 +5,7 @@ variable "name" { variable "container_port" { default = 8080 - type = string + type = number description = "Port your container listens (used in the placeholder task definition)" } From 3d50ca77a913a7d4fd66b0c895f669945a1624bf Mon Sep 17 00:00:00 2001 From: lucasmacedot Date: Sun, 25 Aug 2024 21:29:11 -0400 Subject: [PATCH 09/10] Update name --- appautoscaling.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appautoscaling.tf b/appautoscaling.tf index 60d890c..9a0bab4 100644 --- a/appautoscaling.tf +++ b/appautoscaling.tf @@ -79,7 +79,7 @@ resource "aws_appautoscaling_policy" "scale_custom" { resource "aws_appautoscaling_scheduled_action" "scale_in_schedules" { for_each = { for schedule in var.scale_in_schedules : schedule.name => schedule } - name = "${var.name}-${each.value}-scale-in" + name = "${var.name}-${each.key}-scale-in" service_namespace = aws_appautoscaling_target.ecs[0].service_namespace resource_id = aws_appautoscaling_target.ecs[0].resource_id scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension From e2fe6961c2258b69e0ea68f46767ba90cc4ce7b8 Mon Sep 17 00:00:00 2001 From: lucasmacedot Date: Sun, 25 Aug 2024 21:37:44 -0400 Subject: [PATCH 10/10] Add scale out schedules --- _variables.tf | 24 ++++++++---------------- appautoscaling.tf | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/_variables.tf b/_variables.tf index 28235ea..4348cb9 100644 --- a/_variables.tf +++ b/_variables.tf @@ -511,32 +511,24 @@ variable "alarm_ecs_running_tasks_threshold" { description = "Alarm when the number of ecs service running tasks is lower than a certain value. CloudWatch Container Insights must be enabled for the cluster." } -variable "enable_schedule" { - default = false - type = bool - description = "Enables schedule to shut down and start up instances outside business hours." -} - variable "scale_in_schedules" { type = list(object({ cron = string name = string })) - default = null + default = null + description = "Cron expression to define when to trigger a stop of the auto-scaling group. E.g. 'cron(00 21 ? * SUN-THU *)' to start at 8am UTC time." } -variable "schedule_cron_start" { - type = string - default = "" +variable "scale_out_schedules" { + type = list(object({ + cron = string + name = string + })) + default = null description = "Cron expression to define when to trigger a start of the auto-scaling group. E.g. 'cron(00 21 ? * SUN-THU *)' to start at 8am UTC time." } -variable "schedule_cron_stop" { - type = string - default = "" - description = "Cron expression to define when to trigger a stop of the auto-scaling group. E.g. 'cron(00 09 ? * MON-FRI *)' to start at 8am UTC time" -} - variable "command" { type = list(string) default = null diff --git a/appautoscaling.tf b/appautoscaling.tf index 9a0bab4..6c564c0 100644 --- a/appautoscaling.tf +++ b/appautoscaling.tf @@ -91,6 +91,20 @@ resource "aws_appautoscaling_scheduled_action" "scale_in_schedules" { } } +resource "aws_appautoscaling_scheduled_action" "scale_out_schedules" { + for_each = { for schedule in var.scale_out_schedules : schedule.name => schedule } + name = "${var.name}-${each.key}-scale-out" + service_namespace = aws_appautoscaling_target.ecs[0].service_namespace + resource_id = aws_appautoscaling_target.ecs[0].resource_id + scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension + schedule = each.value.cron + timezone = var.schedule_timezone + scalable_target_action { + min_capacity = var.autoscaling_min + max_capacity = var.autoscaling_max + } +} + #resource "aws_appautoscaling_scheduled_action" "scale_service_in" { # count = var.enable_schedule ? 1 : 0 # name = "${var.name}-scale-in"