Python 在AWS中尝试作为lambda函数运行时,代码超时

Python 在AWS中尝试作为lambda函数运行时,代码超时,python,amazon-web-services,aws-lambda,Python,Amazon Web Services,Aws Lambda,下面是我的代码,我希望有人能帮助我清理代码,使之更有效。基本上,代码应该遍历AWS帐户中的所有卷,然后列出所有未标记的卷,然后发送电子邮件。然而,当在AWS中作为lambda函数运行它时,它会超时,但如果在本地运行它,它将需要30分钟才能完成(不管它是如何完成的)。我敢肯定,它是在重复它不需要的东西 另外,如果我打印ec2_实例列表,我可以看到重复的值,因此我只希望有唯一的值,这样就不会对每个ec2实例重复脚本 import logging import boto3 from smtplib i

下面是我的代码,我希望有人能帮助我清理代码,使之更有效。基本上,代码应该遍历AWS帐户中的所有卷,然后列出所有未标记的卷,然后发送电子邮件。然而,当在AWS中作为lambda函数运行它时,它会超时,但如果在本地运行它,它将需要30分钟才能完成(不管它是如何完成的)。我敢肯定,它是在重复它不需要的东西

另外,如果我打印ec2_实例列表,我可以看到重复的值,因此我只希望有唯一的值,这样就不会对每个ec2实例重复脚本

import logging
import boto3
from smtplib import SMTP, SMTPException
from email.mime.text import MIMEText

logger = logging.getLogger()
logger.setLevel(logging.INFO)

session = boto3.Session(profile_name="prod")
client = session.client('ec2')

untagged_volumes = []
detached_volumes = []
ec2_instances = []

response = client.describe_volumes()

for volume in response['Volumes']:
    if 'Tags' in str(volume):
        continue
    else:
        if 'available' in str(volume):
            detached_volumes.append(volume['VolumeId'])
        else:
            untagged_volumes.append(volume['VolumeId'])
            untagged_volumes.append(volume['Attachments'][0]['InstanceId'])
            ec2_instances.append(volume['Attachments'][0]['InstanceId'])

unique_instances = list(set(ec2_instances))

# Create the msg body.
msg_body_list = []
for instance in unique_instances:
    desc_instance = client.describe_instances()

    # append to the msg_body_list the lines that we would like to show on the email
    msg_body_list.append("VolumeID: {}".format(desc_instance['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']))
    msg_body_list.append("Attached Instance: {}".format(desc_instance['Reservations'][0]['Instances'][0]['InstanceId']))

    # if there are tags, we will append it as singles lines as far we have tags
    if 'Tags' in desc_instance['Reservations'][0]['Instances'][0]:
        msg_body_list.append("Tags:")
        for tag in desc_instance['Reservations'][0]['Instances'][0]['Tags']:
            msg_body_list.append("    Key: {} | Value: {}".format(tag['Key'], tag['Value']))
    # in case we don't have tags, just append no tags.
    else:
        msg_body_list.append("Tags: no tags")
        msg_body_list.append("--------------------")

# send email
mail_from = "xxx@xxx.com"
mail_to = 'xxx@xxx.com'

msg = MIMEText("\n".join(msg_body_list))
msg["Subject"] = "EBS Tagged Instance Report for"
msg["From"] = mail_from
msg["To"] = mail_to

try:
    server = SMTP('xxx.xxx.xxx.xxx', 'xx')
    server.sendmail(mail_from, mail_to.split(','), msg.as_string())
    server.quit()
    print('Email sent')
except SMTPException:
    print('ERROR! Unable to send mail')

Lambda函数的时间限制为15分钟。这就是超时的原因-如果需要运行更长时间的脚本,请查找。

Lambda函数的时间限制为15分钟。这就是超时的原因-如果您需要运行更长时间的脚本,请查找。

Fargate目前不是我们的选项。我知道Lambda的超时时间是15分钟,但我认为如果我的脚本超过这个时间,可能会有问题,这就是为什么我请求帮助Fargate目前不是我们的选择。我知道Lambda的超时时间为15分钟,但我认为如果我的脚本超过15分钟,可能会有问题,这就是我寻求帮助的原因。首先看,我认为你可以开始使用boto3资源,而不是像文档中建议的那样使用
客户机
。那里的功能更快,更适合开箱即用。从重构开始,即用7次投票检查评论:“唯一列表”和“实例”是什么?您迭代“unique_list”,并将每个项指定为“instance”,但不要使用它们。unique_list是我用来测试的。谢谢你指出这一点。我已经编辑了代码。有人能帮忙吗?我已经设法创建了一个新的列表,其中只包含唯一的instanceid(更新代码以反映这一点),但代码运行仍然需要很长时间(有人告诉我,这可能是因为Descripte_实例),最终完成时,它多次向我发送一封包含同一实例信息的电子邮件乍一看,我认为你可以开始使用bot3资源,而不是像文档建议的那样使用
client
。那里的功能更快,更适合开箱即用。从重构开始,即用7次投票检查评论:“唯一列表”和“实例”是什么?您迭代“unique_list”,并将每个项指定为“instance”,但不要使用它们。unique_list是我用来测试的。谢谢你指出这一点。我已经编辑了代码。有人能帮忙吗?我已经设法创建了一个新的列表,其中只包含唯一的instanceid(更新代码以反映这一点),但代码运行仍然需要很长时间(有人告诉我,这可能是因为Descripte_实例),当它最终完成时,它会多次向我发送一封包含同一实例信息的电子邮件