Skip to content

Instantly share code, notes, and snippets.

@philschmid
Last active October 11, 2019 12:37
Show Gist options
  • Save philschmid/b34ba8f73065e26c77921305906810b7 to your computer and use it in GitHub Desktop.
Save philschmid/b34ba8f73065e26c77921305906810b7 to your computer and use it in GitHub Desktop.
Scale your aws Fargate task 0 to 100 and 100 to 0

Scaling up and down AWS Fargate with Lambda and SQS

Goal

Set up Lambda, which reads sqs-queue every minute and decides to start or stop aws-fargate tasks.

Literature

procedure

  • Use Serverless Framework for lambda function
  • use Cloudformation for ECR deploy and Fargate Definition -> show docs
  • script for docker build and deploy
import boto3
import os
client = boto3.client('ecs', region_name='eu-central-1')
def autoscaler(event,context):
message_number = count_sqs(queue_url)
desired_count = evaluate_scale(message_number)
response = client.update_service(
cluster=os.environ['test_ecs_cluster'],
service=os.environ['test_ecs_service'],
taskDefinition=os.environ['test_ecs_task'],
desiredCount=desired_count
)
return true
# Required env vars:
# $test_ecs_cluster: name of the ECS cluster
# $test_ecs_task: name and revision of the task definition (i.e. `mytask:1`)
# $test_ecs_service: name of the ECS service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment