这可能是因为您的 Lambda 函数在第二次运行时需要更多的内存来处理更多的数据。您可以尝试增加 Lambda 函数的内存限制以解决此问题。以下是一个示例 Lambda 函数代码,该函数从 SQS 队列中读取消息并将其打印到日志中:
import boto3
def lambda_handler(event, context):
# Get the SQS client
sqs = boto3.client('sqs')
# Get the URL for the SQS queue
queue_url = 'your_queue_url_here'
# Receive messages from the queue
response = sqs.receive_message(
QueueUrl=queue_url,
MaxNumberOfMessages=10,
VisibilityTimeout=30
)
# Print each message to the log
for message in response['Messages']:
print(message['Body'])
如果您的 Lambda 函数在第二次运行时失败,请将内存限制增加到 1 GB,并尝试再次运行它。要增加内存限制,请在 AWS Lambda 控制台中打开函数配置,并'内存(MB)”设置更改为 1024 MB 或更高。