HttpApiFunctionAuth - Amazon Serverless Application Model
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

HttpApiFunctionAuth

在事件级别配置授权。

为特定 API+路径+方法配置身份验证

语法

要在您的 Amazon Serverless Application Model (Amazon SAM) 模板中声明此实体,请使用以下语法。

YAML

AuthorizationScopes: List Authorizer: String

属性

AuthorizationScopes

适用于此 API、路径和方法的授权范围。

此处列出的范围将覆盖DefaultAuthorizer应用的任何范围(如果存在)。

类型:列表

必需:否

Amazon CloudFormation 兼容性:此属性为 Amazon SAM 独有,没有 Amazon CloudFormation 等效属性。

Authorizer

特定函数的Authorizer。要使用 IAM 授权,请在模板的 Globals 部分指定 AWS_IAM,并为 EnableIamAuthorizer 指定 true

如果您已在 API 中指定了全局授权方并想公开特定函数,请通过将 Authorizer 设置为 NONE 来覆盖。

类型:字符串

必需:否

Amazon CloudFormation 兼容性:此属性为 Amazon SAM 独有,没有 Amazon CloudFormation 等效属性。

示例

函数身份验证

在函数级别指定授权

YAML

Auth: Authorizer: OpenIdAuth AuthorizationScopes: - scope1 - scope2

IAM 授权

在事件级别指定 IAM 授权。要在事件级别使用 AWS_IAM 授权,还必须在模板的 Globals 部分为 EnableIamAuthorizer 指定 true。有关更多信息,请参阅 Amazon SAM 模板的全局变量部分

YAML

Globals: HttpApi: Auth: EnableIamAuthorizer: true Resources: HttpApiFunctionWithIamAuth: Type: AWS::Serverless::Function Properties: Events: ApiEvent: Type: HttpApi Properties: Path: /iam-auth Method: GET Auth: Authorizer: AWS_IAM Handler: index.handler InlineCode: | def handler(event, context): return {'body': 'HttpApiFunctionWithIamAuth', 'statusCode': 200} Runtime: python3.9