

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

# 的 IAM 許可範例 AWS SAM
<a name="serverless-controlling-access-to-apis-permissions"></a>

您可以在範本中定義 IAM 許可，以控制對 AWS SAM APIs存取。若要這樣做，請使用 [ApiAuth](sam-property-api-apiauth.md)資料類型。

以下是 用於 IAM 許可的範例 AWS SAM 範本：

```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Description: 'API with IAM authorization'
      Auth:
        DefaultAuthorizer: AWS_IAM #sets AWS_IAM auth for all methods in this API
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: python3.10
      Events:
        GetRoot:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /
            Method: get
      InlineCode: |
        def handler(event, context):
          return {'body': 'Hello World!', 'statusCode': 200}
```

如需 IAM 許可的詳細資訊，請參閱《 [API Gateway 開發人員指南》中的控制叫用 API 的存取](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html)。 **