Skip to content

Instantly share code, notes, and snippets.

View sschrijver's full-sized avatar

Stephan Schrijver sschrijver

  • Pon Datalab
  • Zwolle, The Netherlands
View GitHub Profile
resource "aws_cloudwatch_metric_alarm" "sqs_scale_in" {
alarm_name = "SQS-ScaleIn-${var.service_name}"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "ApproximateNumberOfMessagesVisible"
namespace = "AWS/SQS"
period = "60"
threshold = "1"
statistic = "Sum"
alarm_description = "SQS-ScaleIn-${var.service_name}"
resource "aws_cloudwatch_metric_alarm" "sqs_scale_out" {
alarm_name = "SQS-ScaleOut-${var.service_name}"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "ApproximateNumberOfMessagesVisible"
namespace = "AWS/SQS"
period = "60"
threshold = "1"
statistic = "Sum"
resource "aws_appautoscaling_policy" "scale_down_fargate" {
policy_type = "StepScaling"
name = "sqs-scaling-down-${var.service_name}"
resource_id = aws_appautoscaling_target.ecs_target.resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target.service_namespace
step_scaling_policy_configuration {
adjustment_type = "ExactCapacity"
metric_aggregation_type = "Average"
resource "aws_appautoscaling_policy" "scale_up_fargate" {
policy_type = "StepScaling"
name = "sqs-scaling-up-${var.service_name}"
resource_id = aws_appautoscaling_target.ecs_target.resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target.service_namespace
step_scaling_policy_configuration {
adjustment_type = "ExactCapacity"
metric_aggregation_type = "Average"
resource "aws_appautoscaling_target" "ecs_target" {
max_capacity = 3
min_capacity = 0
resource_id = "service/${data.aws_ecs_cluster.ecs_cluster.cluster_name}/${var.service_name}"
role_arn = data.aws_iam_role.ecs_autoscaling_role.arn
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
@sschrijver
sschrijver / main.tf
Created August 14, 2020 11:32
Scaling fargate based on SQS queue items
provider "aws" {
profile = var.profile_name
region = var.region
}
data "aws_ecs_cluster" "ecs_cluster" {
cluster_name = var.ecs_cluster_name
}
### Keybase proof
I hereby claim:
* I am sschrijver on github.
* I am stephanschrijver (https://keybase.io/stephanschrijver) on keybase.
* I have a public key ASA75ijohpUWqZh4q8WF8D4oVwqmwX9VXIpRf9CYqFpmwwo
To claim this, I am signing this object: