在AWS API Gateway中,Lambda授权器的请求类型是将Lambda函数用作API Gateway的授权器。当使用Lambda授权器时,API Gateway会将请求中的信息传递给Lambda函数,并期望Lambda函数返回一个策略文档,指示是否允许访问API资源。
在Lambda授权器中,您可以通过事件对象访问请求的详细信息,包括HTTP方法(method),路径(path),标头(headers)等。然而,Lambda授权器事件对象并没有直接包含methodArn属性。
要访问请求的methodArn,您可以从事件对象中提取其它属性,并将它们组合成methodArn形式的字符串。以下是一个示例代码,演示如何从事件对象中构建methodArn:
import json
def lambda_handler(event, context):
# Extract request information from event object
httpMethod = event['httpMethod']
resourcePath = event['path']
accountId = event['requestContext']['accountId']
apiId = event['requestContext']['apiId']
stage = event['requestContext']['stage']
# Construct methodArn
methodArn = f"arn:aws:execute-api:{context.region}:{accountId}:{apiId}/{stage}/{httpMethod}{resourcePath}"
# Return policy document
policyDocument = {
'Version': '2012-10-17',
'Statement': [
{
'Action': 'execute-api:Invoke',
'Effect': 'Allow',
'Resource': methodArn
}
]
}
return {
'isAuthorized': True,
'policyDocument': policyDocument
}
在上面的示例中,我们从事件对象中提取httpMethod,resourcePath,accountId,apiId和stage属性,并使用它们构建methodArn字符串。然后,我们创建一个策略文档,将methodArn作为资源(Resource)指定。
最后,我们将isAuthorized设置为True,表示允许访问API资源,并返回包含策略文档的响应。
请注意,上面的示例是使用Python编写的Lambda函数代码,如果您使用的是其他语言,请进行相应的调整。此外,确保在API Gateway配置中正确设置了Lambda授权器。