資源政策範例 - AWS Serverless Application Model

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

資源政策範例

您可以在 AWS SAM 範本中附加資源策略來控制 API 的存取。要做到這一點,你使用的ApiAuth數據類型。

以下是私有 API 的 AWS 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 Gateway 開發人員指南中的在 Amazon API G ateway 道中建立私有 API