可以使用 AWS CloudWatch 事件规则来触发 Lambda 函数,该方法不会造成并发阻塞。代码示例:
{
"source": [
"aws.events"
],
"detail-type": [
"Scheduled Event"
],
"detail": {
"source": [
"aws.events"
],
"detail-type": [
"Scheduled Event"
]
}
}
import json
import boto3
def lambda_handler(event, context):
# 处理 CloudWatch 事件
if 'source' in event and event['source'] == 'aws.events':
print('Received CloudWatch event')
else:
print('Processing Lambda function')
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
注意:在此解决方案中,Lambda 函数将同时处理 CloudWatch 事件和其它请求。在处理 CloudWatch 事件时,可以添加适当的逻辑来避免并发阻塞。