forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ecs-service-extensions): Auto scaling for Queue Extension (aws#1…
…7430) ---- This PR adds target tracking auto scaling policy for the the SQS Queues provided to and created by the `QueueExtension` (in the `useService()` hook). The auto scaling is based on `backlogPerTask` custom metric which is emitted by an AWS Lambda Function. The PR also contains this Lambda Function and its tests. *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
18 changed files
with
1,456 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 0 additions & 212 deletions
212
packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/queue.ts
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/queue/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './queue'; |
15 changes: 15 additions & 0 deletions
15
packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/queue/lambda/index.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import os | ||
import boto3 | ||
from queue_backlog_calculator import QueueHandler | ||
|
||
def queue_handler(event, context): | ||
""" | ||
Handler for the lambda trigger | ||
""" | ||
|
||
ecs = boto3.client('ecs') | ||
sqs = boto3.client('sqs') | ||
|
||
queue_handler = QueueHandler(ecs_client=ecs, sqs_client=sqs, environ=os.environ) | ||
|
||
return queue_handler.emit() |
71 changes: 71 additions & 0 deletions
71
...containers/ecs-service-extensions/lib/extensions/queue/lambda/queue_backlog_calculator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from math import ceil | ||
import time | ||
import json | ||
|
||
class QueueHandler: | ||
def __init__(self, ecs_client, sqs_client, environ): | ||
self.ecs = ecs_client | ||
self.sqs = sqs_client | ||
self.cluster_name = environ['CLUSTER_NAME'] | ||
self.service_name = environ['SERVICE_NAME'] | ||
self.namespace = environ['NAMESPACE'] | ||
self.queue_names = environ['QUEUE_NAMES'].split(',') | ||
|
||
def emit(self): | ||
try: | ||
running_count = self.get_running_task_count() | ||
backlogs = [self.get_queue_backlog(queue_name, running_count) for queue_name in self.queue_names] | ||
self.timestamp = int(time.time() * 1000) | ||
for backlog in backlogs: | ||
self.emit_backlog_per_task_metric(backlog['queueName'], backlog['backlogPerTask']) | ||
except Exception as e: | ||
Exception('Exception: {}'.format(e)) | ||
|
||
""" | ||
Write the backlogPerTask metric to the stdout according to the Cloudwatch embedded metric format. | ||
""" | ||
def emit_backlog_per_task_metric(self, queue_name, backlog_per_task): | ||
print(json.dumps({ | ||
"_aws": { | ||
"Timestamp": self.timestamp, | ||
"CloudWatchMetrics": [{ | ||
"Namespace": self.namespace, | ||
"Dimensions": [["QueueName"]], | ||
"Metrics": [{"Name":"BacklogPerTask", "Unit": "Count"}] | ||
}], | ||
}, | ||
"QueueName": queue_name, | ||
"BacklogPerTask": backlog_per_task, | ||
})) | ||
|
||
""" | ||
Get the number of tasks in the 'RUNNING' state for the service 'service_name'. | ||
""" | ||
def get_running_task_count(self): | ||
service_desc = self.ecs.describe_services( | ||
cluster=self.cluster_name, | ||
services=[self.service_name], | ||
) | ||
if len(service_desc['services']) == 0: | ||
raise Exception('There are no services with name {} in cluster: {}'.format(self.service_name, self.cluster_name)) | ||
return service_desc['services'][0].get('runningCount', 0) | ||
|
||
""" | ||
This method calculates and returns the backlogPerTask metric for the given queue. | ||
""" | ||
def get_queue_backlog(self, queue_name, count): | ||
queue_url = self.sqs.get_queue_url(QueueName=queue_name) | ||
running_count = 1 if count == 0 else count | ||
|
||
def get_backlog_per_task(): | ||
queue_attributes = self.sqs.get_queue_attributes( | ||
QueueUrl=queue_url['QueueUrl'], | ||
AttributeNames=['ApproximateNumberOfMessages'] | ||
) | ||
num_of_msgs = int(queue_attributes['Attributes'].get('ApproximateNumberOfMessages', 0)) | ||
return ceil(num_of_msgs/running_count) | ||
|
||
return { | ||
'queueName': queue_name, | ||
'backlogPerTask': get_backlog_per_task() | ||
} |
Oops, something went wrong.