首页 > 解决方案 > 亚马逊AWS | 从 lambda 调用时 ECS 任务不运行

问题描述

我正在使用 Amazon Web Services 并尝试在从 Lambda 触发的集群上运行 ECS 任务定义。

任务定义运行一个容器,该容器应该调整图像大小,通过 lambda 接收,然后上传相同图像的调整大小版本。

这是 lambda 定义,它由 S3 存储桶上的上传触发,我在运行任务方法中从此处发送 python 命令:

import json
import boto3

def run_fargate_task(img_name, img_resized_name):
    client = boto3.client('ecs', region_name='us-east-1')
    response = client.run_task(
        cluster='cloud-p2-cluster',
        launchType = 'FARGATE',
        taskDefinition='cloud-p2-task-definition',
        count = 1,
        platformVersion='LATEST',
        networkConfiguration={
            'awsvpcConfiguration': {
                'subnets': [
                    'subnet-1824f955',
                    'subnet-889d93d4',
                    'subnet-c40508a3',
                    'subnet-6a8a8244',
                    'subnet-71dd8c4f',
                    'subnet-3f07c031',
                ],
                'assignPublicIp': 'ENABLED'
            }
        },
        overrides={
            'containerOverrides': [
                {
                    'name': 'cloud-p2',
                    'command': [
                            'python',
                            'app.py',
                            img_name,
                            img_resized_name
                        ]
                },
            ],
        },
    )

    return str(response)

def lambda_handler(event, context):
    print(event)
    s3 = boto3.resource('s3')
    for record in event['Records']:   #Eventos de s3
        bucket = record['s3']['bucket']['name']
        file = record['s3']['object']['key']

        file_aux = file.split('.')
        img_resized_name = file_aux[0] + '-small.' + file_aux[1]

        resp = run_fargate_task(file, img_resized_name)

        print("File to process:" + file)
        print("Run task response: " + resp)
    return {
        'statusCode': 200,
        'body': resp
    }


这是我的容器图像背后的代码:

from PIL import Image
from resizeimage import resizeimage
import sys
import boto3
import json

#Bucket setup
s3 = boto3.client('s3') #.client('s3')
#download file
ext = sys_argv[1].split('.')
temp_file_name = ext[0] + '-tmp.' + ext[1] 
s3.download_file('cloud-p2-cubeta, sys_argv[1], temp_file_name)

with open(temp_file_name,'r+b') as f:
    with Image.open(f) as image:
        cover = resizeimage.resize_cover(image,[150,150])
        cover.save(sys.argv[2],image.format)
    s3.upload_fileobj(f, 'cloud-p2-cubeta', cover)



如果有帮助,这是来自 Dockerfile 的代码,resize.sh 文件包含执行 app.py 的 python 命令,但它不应该是相关的,因为我从 lambda 发送了一个覆盖命令。

FROM python:3.6

RUN pip3 install boto3
RUN pip3 install python-resize-image

WORKDIR /app
COPY app.py /app/
COPY resize.sh /app/

VOLUME ["/app/images"]

任何人都知道可能出了什么问题或者我可能会在我的集群上查看日志吗?

标签: python-3.xamazon-web-servicesaws-lambdaamazon-ecs

解决方案


推荐阅读