是的,AWS API网关可以调用多个Lambda函数。您可以使用Lambda集成来实现此目的。下面是一个示例代码,演示了如何在AWS API网关中调用多个Lambda函数。
import boto3
import json
def lambda_handler(event, context):
# 解析API网关的请求
httpMethod = event['httpMethod']
resource = event['resource']
# 检查请求的资源路径,并调用相应的Lambda函数
if httpMethod == 'GET' and resource == '/function1':
response = function1_handler(event)
elif httpMethod == 'POST' and resource == '/function2':
response = function2_handler(event)
else:
response = {
'statusCode': 404,
'body': 'Resource not found'
}
return response
def function1_handler(event):
# 处理function1的逻辑
response = {
'statusCode': 200,
'body': 'Hello from function1'
}
return response
def function2_handler(event):
# 处理function2的逻辑
response = {
'statusCode': 200,
'body': 'Hello from function2'
}
return response
在上面的示例中,我们首先解析API网关的请求,检查HTTP方法和资源路径。根据请求的路径,我们调用相应的Lambda函数来处理逻辑。最后,我们返回每个函数的响应。
请确保将上述代码部署到AWS Lambda,并将API网关配置为使用Lambda集成。这样,当API网关接收到请求时,它将触发Lambda函数,并将其响应返回给调用方。