python-3.x - 亚马逊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"]
任何人都知道可能出了什么问题或者我可能会在我的集群上查看日志吗?
解决方案
推荐阅读
- java - 如何让 Scanner 从用户输入中挑选出关键字?
- java - 如何使用 Project Reactor 使用动态值重复或重试?
- javascript - 如何有效地链接 RxJS Observables
- sql - 如何将层次结构中的底部值与其上方的每个级别配对?
- python - 如何从用户读取多行中的元组输入?
- javascript - react中的目标容器不是DOM元素
- flutter - 谷歌云 SQL 连接到颤振
- angularjs - 在使用 tooltip-is-open 时延迟后隐藏 uib-tooltip
- c# - 为什么这个 C# 方法不能生成正确的屏幕截图?
- java - 多对多 Spring MVC mappedBy 引用了一个未知的目标实体属性