

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

# 的 IAM 权限示例 AWS SAM
<a name="serverless-controlling-access-to-apis-permissions"></a>

您可以 APIs 通过在 AWS SAM 模板中定义 IAM 权限来控制对您的访问权限。为此，您需要使用 [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)。