API 密钥示例 - AWS Serverless Application Model

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

API 密钥示例

您可以通过在 AWS SAM 模板中要求 API 密钥来控制对 API 的访问权限。为此,您需要使用 ApiAuth 数据类型。

以下是 API 密钥的示例 AWS SAM 模板部分:

Resources: MyApi: Type: AWS::Serverless::Api Properties: StageName: Prod Auth: ApiKeyRequired: true # sets for all methods MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: . Handler: index.handler Runtime: nodejs12.x Events: ApiKey: Type: Api Properties: RestApiId: !Ref MyApi Path: / Method: get Auth: ApiKeyRequired: true

有关 API 密钥的更多信息,请参阅《API Gateway 开发人员指南》中的创建和使用带 API 密钥的使用计划