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 金鑰」的使用計劃