的资源策略示例 AWS SAM - AWS Serverless Application Model

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

的资源策略示例 AWS SAM

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

以下是私有 AWS SAM 版的示例模板API。私有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网API关开发者指南》中的API使用 Gateway 资源策略控制对的访问权限。有关私有化的更多信息APIs,请参阅API网关开发者指南API中的在 Amazon Gatew API ay 中创建私有网络