Skip to content

Commit

Permalink
Allow configuring the scaling metric. Also run TF fmt.
Browse files Browse the repository at this point in the history
  • Loading branch information
dipack95 committed Feb 8, 2022
1 parent 1d1f913 commit 905c2ac
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 54 deletions.
26 changes: 13 additions & 13 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ resource "aws_ecs_service" "service" {
dynamic "capacity_provider_strategy" {
for_each = var.capacity_provider_strategies
content {
base = capacity_provider_strategy.value.base
base = capacity_provider_strategy.value.base
capacity_provider = capacity_provider_strategy.value.capacity_provider
weight = capacity_provider_strategy.value.weight
weight = capacity_provider_strategy.value.weight
}
}

Expand Down Expand Up @@ -81,7 +81,7 @@ resource "aws_ecs_task_definition" "task" {
for_each = var.volumes

content {
name = volume.value["name"]
name = volume.value["name"]
host_path = lookup(volume.value, "host_path", null)
}
}
Expand Down Expand Up @@ -127,28 +127,28 @@ resource "aws_security_group" "service_security_group" {
resource "aws_appautoscaling_target" "ecs_service_autoscaling_target" {
for_each = var.use_autoscaling ? [1] : []

min_capacity = var.min_desired_count
max_capacity = var.max_desired_count
min_capacity = var.min_desired_count
max_capacity = var.max_desired_count
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
resource_id = "service/${data.aws_ecs_cluster.cluster.cluster_name}/${aws_ecs_service.service.name}"
service_namespace = "ecs"
resource_id = "service/${data.aws_ecs_cluster.cluster.cluster_name}/${aws_ecs_service.service.name}"
}

resource "aws_appautoscaling_policy" "ecs_service_autoscaling_policy" {
name = "ecs-fargate-service-autoscaling-policy"
service_namespace = aws_appautoscaling_target.ecs_service_autoscaling_target.service_namespace
name = "ecs-fargate-service-autoscaling-policy"
service_namespace = aws_appautoscaling_target.ecs_service_autoscaling_target.service_namespace
scalable_dimension = aws_appautoscaling_target.ecs_service_autoscaling_target.scalable_dimension
resource_id = aws_appautoscaling_target.ecs_service_autoscaling_target.resource_id
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs_service_autoscaling_target.resource_id
policy_type = "TargetTrackingScaling"

target_tracking_scaling_policy_configuration {
# Seconds
scale_in_cooldown = 120
# Seconds
scale_out_cooldown = 30
target_value = var.scaling_target_value
target_value = var.scaling_target_value
predefined_metric_specification {
predefined_metric_type = "ALBRequestCountPerTarget"
predefined_metric_type = var.scaling_metric
}
}
}
84 changes: 45 additions & 39 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
variable name {
variable "name" {
description = "The name of the service. Used as a prefix for other resource names"
}

variable cluster_id {
variable "cluster_id" {
description = <<EOF
The id of the ECS cluster this service belongs to.
Expand All @@ -11,46 +11,52 @@ variable cluster_id {
EOF
}

variable desired_count {
variable "desired_count" {
type = number
description = "If not using Application Auto-scaling, the number of tasks to keep alive at all times"
default = null
default = null
}

variable use_autoscaling {
type = bool
variable "use_autoscaling" {
type = bool
description = "Use Application Auto-scaling to scale service"
default = false
default = false
}

variable min_desired_count {
type = number
variable "min_desired_count" {
type = number
description = "If using Application auto-scaling, minimum number of tasks to keep alive at all times"
default = null
default = null
}

variable max_desired_count {
type = number
variable "max_desired_count" {
type = number
description = "If using Application auto-scaling, maximum number of tasks to keep alive at all times"
default = null
default = null
}

variable "scaling_target_value" {
type = number
type = number
description = "If using Application auto-scaling, the target value to hit for the Auto-scaling policy"
default = null
default = null
}

variable "scaling_metric" {
type = string
description = "If using Application auto-scaling, the pre-defined AWS metric to use for the Auto-scaling policy"
default = "ALBRequestCountPerTarget"
}

variable vpc_id {
variable "vpc_id" {
description = "ID of the VPC the alb is in"
}

variable subnet_ids {
variable "subnet_ids" {
type = list(string)
description = "List of subnets tasks can be run in."
}

variable load_balancers {
variable "load_balancers" {
type = list(object({
target_group_arn = string
container_name = string
Expand All @@ -69,91 +75,91 @@ variable load_balancers {
EOF
}

variable service_registries {
variable "service_registries" {
description = "Allows you to register this service to a Cloud Map registry"
type = list(map(string))
default = []
}

variable tags {
variable "tags" {
type = map(string)
description = "Tags to set on all resources that support them"
}

variable cpu {
variable "cpu" {
default = 512
description = "How much CPU should be allocated to each app instance?"
}

variable memory {
variable "memory" {
default = 1024
description = "How much memory should be allocated to each app instance?"
}

variable container_definitions {
variable "container_definitions" {
type = string
description = "JSON encoded list of container definitions"
}

variable additional_task_policy_arns {
variable "additional_task_policy_arns" {
type = list(string)
description = "IAM Policy arns to be added to the tasks"
default = []
}

variable additional_task_policy_arns_count {
variable "additional_task_policy_arns_count" {
type = number
description = "The number of items in var.additional_task_policy_arns. Terraform is not quite smart enough to figure this out on its own."
default = 0
}

variable health_check_grace_period_seconds {
variable "health_check_grace_period_seconds" {
type = number
default = 60
description = "Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown, up to 2147483647. Only valid for services configured to use load balancers."
}

variable alb_security_group_ids {
variable "alb_security_group_ids" {
type = list(string)
description = "The ids of all security groups set on the ALB. We require that the tasks can only talk to the ALB"
}

variable execution_role_arn {
variable "execution_role_arn" {
type = string
description = "If present, this is the execution role that will be used for the ECS Tasks. If not present, a role will be created"
default = ""
}

variable security_group_id {
variable "security_group_id" {
type = string
description = "If present, this is the security group to apply to the ECS task. If not present, a security group will be created"
default = ""
}

variable volumes {
variable "volumes" {
type = list(map(string))
description = "List of volumes to make available to containers in this task."
default = []
}

variable deploy_env {
variable "deploy_env" {
type = string
description = "The environment resources are to be created in. Usually dev, staging, or prod"
}

variable aws_region {
variable "aws_region" {
type = string
description = "The AWS region to create resources in."
default = "eu-west-1"
}

variable capacity_provider_strategies {
variable "capacity_provider_strategies" {
type = list(object({
base = optional(number)
base = optional(number)
capacity_provider = string
weight = number
weight = number
}))
default = []
default = []
description = <<EOF
Capacity provider strategy to use for the service
Expand All @@ -163,8 +169,8 @@ variable capacity_provider_strategies {
EOF
}

variable ephemeral_storage_gib {
variable "ephemeral_storage_gib" {
description = "The total amount, in GiB, of ephemeral storage to set for the task. The minimum supported value is 20 GiB and the maximum supported value is 200 GiB."
type = number
default = 20
type = number
default = 20
}
4 changes: 2 additions & 2 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
source = "hashicorp/aws"
version = ">= 3.46.0"
}
}
required_version = ">= 0.13"
experiments = [module_variable_optional_attrs]
experiments = [module_variable_optional_attrs]
}

0 comments on commit 905c2ac

Please sign in to comment.