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

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

资源策略示例

您可以通过在 Amazon SAM 模板中附加资源策略来控制对 API 的访问。为此,您需要使用 ApiAuth 数据类型。

以下是私有 API 的示例 Amazon SAM 模板。私有 API 必须有资源策略才能部署。

AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Resources: MyPrivateApi: Type: AWS::Serverless::Api Properties: StageName: Prod EndpointConfiguration: PRIVATE # Creates a private API. Resource policies are required for all private APIs. Auth: ResourcePolicy: CustomStatements: { Effect: 'Allow', Action: 'execute-api:Invoke', Resource: ['execute-api:/*/*/*'], Principal: '*' } MyFunction: Type: 'AWS::Serverless::Function' Properties: InlineCode: | def handler(event, context): return {'body': 'Hello World!', 'statusCode': 200} Handler: index.handler Runtime: python3.10 Events: AddItem: Type: Api Properties: RestApiId: Ref: MyPrivateApi Path: / Method: get

有关资源策略的更多信息,请参阅《API Gateway 开发人员指南》中的使用 API Gateway 资源策略控制 API 的访问权限。有关私有 API 的更多信息,请参阅 API Gatew ay 开发者指南中的在 Amazon API Gateway 中创建私有 API