使用Python和AWS Lambda SDK来处理SQS消息。
代码示例:
import json
import boto3
sqs = boto3.resource('sqs', region_name='us-east-1')
queue = sqs.get_queue_by_name(QueueName='my-sqs-queue-name')
def lambda_handler(event, context):
for record in event['Records']:
message = json.loads(record['body'])
# Do something with the message
print(message)
return 'Successfully processed {} records.'.format(len(event['Records']))
此示例假设您已经在AWS控制台中创建了一个SQS队列,并将其命名为“my-sqs-queue-name”。代码使用AWS SDK for Python(Boto3)中的“get_queue_by_name”函数访问该队列,并读取队列中的所有消息。然后,可以使用mid-level API处理消息(如将其发送到其他服务),并使用 Lambda 函数返回成功处理的记录数。
确保配置正确后,您可以将该Lambda函数与SQS绑定,以在每次有新的消息进入队列时自动触发该函数。
上一篇:AWSLambda函数使用AWSGolangSecretsManager缓存客户端的正确资源权限是什么?
下一篇:AWSLambda函数使用CognitoIdentityServiceProvider的adminCreateUser方法突然失败了。