AWSECSFargate中的任务或容器缩减保护。
创始人
2024-09-25 04:31:55
0

可以通过Lambda函数和CloudWatch Events实现任务或容器缩减保护,具体步骤如下:

1.创建Lambda函数 在AWS Lambda中创建一个函数,用于监视ECS服务和任务的状态。如果检测到任务或服务即将被缩减,则将其标记为不可变动。 以下是一个示例函数:

import boto3 import os

def lambda_handler(event, context):

ecs_client = boto3.client('ecs')

cluster = os.environ['ECS_CLUSTER_NAME']

# get all running tasks in the cluster 
tasks = ecs_client.list_tasks(cluster=cluster, desiredStatus='RUNNING')

# loop through tasks
for task in tasks['taskArns']:
    # check task stop times
    task_details = ecs_client.describe_tasks(cluster=cluster, tasks=[task])
    for container in task_details['tasks'][0]['containers']:
        # if task or container is protected, skip it
        if container.get('taskDefinitionArn', '').endswith(':protected') or task.get('taskDefinitionArn', '').endswith(':protected'):
            continue
        
        # check if task or container is draining
        # if it is, mark it protected to prevent scaling in
        if container['lastStatus'] == 'DRAINING' or task['lastStatus'] == 'DRAINING':
            container_arn = container.get('containerArn', '')
            task_arn = task.get('taskArn', '')
            ecs_client.tag_resource(resourceArn=container_arn, tags={'protected': 'true'})
            ecs_client.tag_resource(resourceArn=task_arn, tags={'protected': 'true'})

2.创建CloudWatch Events规则 在AWS CloudWatch中创建一个事件规则,以周期性地调用Lambda函数。以下是一个示例规则,它将每分钟自动调用Lambda函数:

{ "source": [ "aws.ecs" ], "detail-type": [ "ECS Task State Change" ], "detail": { "lastStatus": [ "STOPPED" ] }, "targets": [ {

相关内容

热门资讯

关于!微信微乐自建房辅助工具,... 关于!微信微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)方法1)微乐家乡麻将自建房辅...
大家学习交流!微信卡五星辅助器... 大家学习交流!微信卡五星辅助器,微乐小程序免费黑科技(开挂自建房)方法1、很好的工具软件,可以解锁游...
三分钟了解!微乐微信自建房辅助... 三分钟了解!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、三分钟了解...
一分钟教会你!微乐家乡麻将自建... 一分钟教会你!微乐家乡麻将自建房辅助app,微乐自建房脚本免费入口(开挂自建房)方法1、用户打开应用...
分享实测!微信微乐自建房辅助工... 分享实测!微信微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)器1、起透看视 微信微乐自建房...
重大通报!微乐自建房辅助工具脚... 重大通报!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)app微乐自建房辅助工具脚本破解...
如何分辨真伪!微乐微信自建房辅... 如何分辨真伪!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)技巧1、微乐微信自建房辅...
我来教教你!微乐自建房辅助工具... 我来教教你!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)工具1、玩家可以在微乐家...
重大来袭!微乐自建房辅助工具神... 重大来袭!微乐自建房辅助工具神器,微乐小程序免费黑科技(开挂自建房)器1、游戏颠覆性的策略玩法,独创...
带你了解!微信卡五星辅助器,微... 带你了解!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法1、带你了解!微信卡五星辅助器,...