避免在ECS集群仍在引用时过期任何ECR镜像
创始人
2024-12-17 04:30:09
0

可以编写lambda函数定期检查ECS集群是否有任何正在使用的ECR镜像,如果有,则更新这些镜像的过期时间,以避免它们在使用期间过期。以下是Python代码示例:

import boto3
import datetime

def update_ecr_image_expire_date(cluster_name, repository_name, image_tag, expire_days):
    client = boto3.client('ecr')

    # calculate new expire date
    new_expire_date = datetime.datetime.now() + datetime.timedelta(days=expire_days)

    # update image expire date
    client.batch_set_image_expiry(
        repositoryName=repository_name,
        imageIds=[
            {
                'imageTag': image_tag
            },
        ],
        lifecyclePolicyPreviewFilter={
            'tagStatus': 'ANY',
            'countType': 'sinceImagePushed',
            'countUnit': 'days',
            'countNumber': expire_days
        },
        expireAfter=new_expire_date
    )

def lambda_handler(event, context):
    # get ECR image detail
    client = boto3.client('ecr')
    result = client.describe_images(
        registryId=event['registry_id'],
        repositoryName=event['repository_name']
    )

    # check if any ECS cluster is still using the image
    for image in result['imageDetails']:
        image_tag = image['imageTags'][0]
        expire_date = image['imagePushedAt'] + datetime.timedelta(days=image['imageManifest']['imageExpirationTimeout'])

        for cluster_arn in image['imageManifest']['imageUses']:
            ecs = boto3.client('ecs')
            cluster_name = cluster_arn.split('/')[1]
            response = ecs.list_container_instances(
                cluster=cluster_name,
                status='ACTIVE'
            )
            if response['containerInstanceArns']:
                # ECS cluster is still using the image, update the image expire date
                update_ecr_image_expire_date(cluster_name, event['repository_name'], image_tag, (expire_date - datetime.datetime.now()).days)

相关内容

热门资讯

五分钟了解!aapoke辅助工... 五分钟了解!aapoke辅助工具存在(黑科技)原来真的有挂(详细智能ai代打教程);最新版2024是...
德州之星辅助挂!wpk透视辅助... 德州之星辅助挂!wpk透视辅助哪里下载(软件透明该)的确是真的有挂(详细智能ai代打教程);玩家必备...
8分钟了解!微扑克ai辅助(透... WePoke高级策略深度解析‌;8分钟了解!微扑克ai辅助(透明挂)其实真的有挂(详细辅助软件教程)...
德州之星插件!poker外挂(... 德州之星插件!poker外挂(辅助挂)确实是真的有挂(详细辅助教程)1、这是跨平台的黑科技,在线的操...
8分钟了解!wepoke外 挂... 8分钟了解!wepoke外 挂(透明挂)确实是真的有挂(详细智能ai代打教程)准备好在wepoke外...
wepoke辅助技巧!aapo... wepoke辅助技巧!aapoker德州线上扑克辅助工具(辅助挂)果真真的有挂(详细辅助脚本教程);...
3分钟了解!wpk有外 挂(透... 1、3分钟了解!wpk有外 挂(透视)其实真的有挂(详细ai辅助教程)。2、wpk有外 挂透视辅助简...
aa扑克辅助!德州ai能盈利(... aa扑克辅助!德州ai能盈利(透视)确实是真的有挂(详细代打ai辅助教程);(需添加指定薇75744...
八分钟了解!wepokewop... 八分钟了解!wepokewopoker透明挂(透明挂)确实真的有挂(详细辅助插件教程)准备好在wep...
wepoke辅助插件!wpk微... wepoke辅助插件!wpk微扑克真的有辅助插件(透视辅助)原来真的有挂(详细辅助工具教程)1、完成...