

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# のリソースポリシーの例 AWS SAM
<a name="serverless-controlling-access-to-apis-resource-policies"></a>

API へのアクセスは、 AWS SAM テンプレート内でリソースポリシーをアタッチすることによって制御できます。これを実行するには、[ApiAuth](sam-property-api-apiauth.md) データ型を使用します。

プライベート 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: '*'
            - Effect: 'Deny'
              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 へのアクセスを制御する](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-resource-policies.html)」を参照してください。プライベート API の詳細情報については、「API Gateway デベロッパーガイド」の「[Amazon API Gateway でのプライベート API の作成](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-private-apis.html)」を参照してください。