本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Lambda 授权方
这些区域有:AWS::Serverless::Api资源类型支持两种类型的 Lambda 授权方:TOKEN和授权方REQUEST授权方。这些区域有:AWS::Serverless::HttpApi仅支持资源类型REQUEST授权方。以下是每种类型的示例。
LambdaTOKEN授权方AWS::Serverless::Api)
您可以通过定义 Lambda 来控制对 API 的访问。TOKEN您的授权方Amazon SAMTemplate 要执行此操作,请使用ApiAuth数据类型。
以下是示例:Amazon SAMLambda 的模板部分TOKEN授权方
Resources: MyApi: Type: AWS::Serverless::Api Properties: StageName: Prod Auth: DefaultAuthorizer: MyLambdaTokenAuthorizer Authorizers: MyLambdaTokenAuthorizer: FunctionArn: !GetAtt MyAuthFunction.Arn MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: index.handler Runtime: nodejs12.x Events: GetRoot: Type: Api Properties: RestApiId: !Ref MyApi Path: / Method: get MyAuthFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: authorizer.handler Runtime: nodejs12.x
有关 Lambda 授权方的更多信息,请参阅。使用 API Gateway Lambda 授权方中的API Gateway 开发人员指南.
LambdaREQUEST授权方AWS::Serverless::Api)
您可以通过定义 Lambda 来控制对 API 的访问。REQUEST您的授权方Amazon SAMTemplate 要执行此操作,请使用ApiAuth数据类型。
以下是示例:Amazon SAMLambda 的模板部分REQUEST授权方
Resources: MyApi: Type: AWS::Serverless::Api Properties: StageName: Prod Auth: DefaultAuthorizer: MyLambdaRequestAuthorizer Authorizers: MyLambdaRequestAuthorizer: FunctionPayloadType: REQUEST FunctionArn: !GetAtt MyAuthFunction.Arn Identity: QueryStrings: - auth MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: index.handler Runtime: nodejs12.x Events: GetRoot: Type: Api Properties: RestApiId: !Ref MyApi Path: / Method: get MyAuthFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: authorizer.handler Runtime: nodejs12.x
有关 Lambda 授权方的更多信息,请参阅。使用 API Gateway Lambda 授权方中的API Gateway 开发人员指南.
Lambda 授权方示例 (AWS::Serverless::HttpApi)
您可以 Lambda 过在Amazon SAMTemplate 要执行此操作,请使用HttpApiAuth数据类型。
以下是示例:Amazon SAMLambda 授权者的模板部分:
Resources: MyApi: Type: AWS::Serverless::HttpApi Properties: StageName: Prod Auth: DefaultAuthorizer: MyLambdaRequestAuthorizer Authorizers: MyLambdaRequestAuthorizer: FunctionArn: !GetAtt MyAuthFunction.Arn FunctionInvokeRole: !GetAtt MyAuthFunctionRole.Arn Identity: Headers: - Authorization AuthorizerPayloadFormatVersion: 2.0 EnableSimpleResponses: true MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: index.handler Runtime: nodejs12.x Events: GetRoot: Type: HttpApi Properties: ApiId: !Ref MyApi Path: / Method: get PayloadFormatVersion: "2.0" MyAuthFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: authorizer.handler Runtime: nodejs12.x