Last active
August 16, 2019 10:55
-
-
Save brafales/900e06b94c08349e28d4c57808f0e7db to your computer and use it in GitHub Desktop.
Serverless Framework example to restart fargate services based on memory usage
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
service: restartFargateServices | |
custom: | |
# SNS topic where the CloudWatch alarms will send messages when triggered | |
# We need the full Arn for the CloudFormation snippet that creates the alarms | |
snsTopic: "${self:service}-fargate-memory-alarms" | |
snsTopicArn: { "Fn::Join" : ["", ["arn:aws:sns:${self:provider.region}:", { "Ref" : "AWS::AccountId" }, ":${self:custom.snsTopic}" ] ] } | |
# Cluster and Service Fargate names that we want to monitor and restart | |
cluster_name: "my-cluster" | |
service_name: "my-service" | |
provider: | |
name: aws | |
runtime: ruby2.5 | |
stage: dev | |
region: eu-west-1 | |
# We need our lambda to have permissions to update ECS services to restart them | |
iamRoleStatements: | |
- Effect: Allow | |
Action: | |
- ecs:UpdateService | |
Resource: '*' | |
functions: | |
restartFargateServices: | |
handler: handler.restart_service | |
# The lambda function will get invoked when a message is sent to the SNS topic | |
events: | |
- sns: ${self:custom.snsTopic} | |
resources: | |
Resources: | |
# Cloudformation template for the CloudWatch alarms | |
MemoryUsageAlarm: | |
Type: AWS::CloudWatch::Alarm | |
Properties: | |
# This will send a message to the SNS topic when the alarm triggers | |
ActionsEnabled: true | |
AlarmActions: | |
- ${self:custom.snsTopicArn} | |
AlarmName: ${self:service}:MemoryUsageAlarm | |
ComparisonOperator: GreaterThanOrEqualToThreshold | |
# Get the cluster and service name dimensions from the YML configuration on lines 8 and 9 | |
Dimensions: | |
- Name: ClusterName | |
Value: ${self:custom.cluster_name} | |
- Name: ServiceName | |
Value: ${self:custom.service_name} | |
MetricName: MemoryUtilization | |
Namespace: AWS/ECS | |
# Trigger the alarm when memory usage goes over 85% for 2 periods of 120 seconds | |
EvaluationPeriods: 2 | |
Period: 120 | |
Statistic: Maximum | |
Threshold: 85.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment