

# 针对调用 API 的访问控制
<a name="api-gateway-control-access-using-iam-policies-to-invoke-api"></a>

在此部分中，您将了解使用 IAM 权限控制对 API 的访问的权限模型。启用 IAM 授权后，客户端必须使用签名版本 4a（SigV4a）和签名版本 4（SigV4），才能使用 AWS 凭证签署其请求。有关更多信息，请参阅 [AWS 签名版本 4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html)。

在本节中，我们介绍模板 IAM 策略语句和策略语句参考。策略语句参考包括与 API 执行服务相关的 `Action` 和 `Resource` 字段的格式。使用这些参考可创建您的 IAM 策略语句。在创建 IAM 策略语句时，您可能需要考虑 API Gateway 资源策略会给授权工作流程带来哪些影响。有关更多信息，请参阅 [API Gateway 资源策略如何影响授权工作流程](apigateway-authorization-flow.md)。

对于私有 API，您应使用 API Gateway 资源策略和 VPC 终端节点策略的组合。有关更多信息，请参阅以下主题：
+ [使用 API Gateway 资源策略控制对 API 的访问](apigateway-resource-policies.md)
+ [在 API Gateway 中为私有 API 使用 VPC 端点策略](apigateway-vpc-endpoint-policies.md)

## 控制谁可以使用 IAM 策略调用 API Gateway API 方法
<a name="api-gateway-who-can-invoke-an-api-method-using-iam-policies"></a>

 要控制谁可以或不可以使用 IAM 权限调用已部署的 API，请创建一个包含必要权限的 IAM 策略文档。此类策略文档的模板如下所示。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Permission",
      "Action": [
        "execute-api:Execution-operation"           
      ],
      "Resource": [
        "arn:aws:execute-api:region:123456789012:api-id/stage/METHOD_HTTP_VERB/Resource-path"
      ]
    }
  ]
}
```

------

 此处，`Permission` 将被替换为 `Allow` 或 `Deny`，具体取决于您是要授予或撤消相应权限。`Execution-operation` 将被替换为受 API 执行服务支持的操作。`METHOD_HTTP_VERB` 表示受指定资源支持的 HTTP 动词。`Resource-path` 是已部署的 API `[Resource](https://docs.aws.amazon.com/apigateway/latest/api/API_Resource.html)` 实例的 URL 路径占位符，该实例可支持上述的 `METHOD_HTTP_VERB`。有关更多信息，请参阅 [在 API Gateway 中执行 API 的 IAM 策略的语句参考](#api-gateway-calling-api-permissions)。

**注意**  
要使 IAM 策略生效，您必须已通过将该方法的 `AWS_IAM` 属性设置为 `[authorizationType](https://docs.aws.amazon.com/apigateway/latest/api/API_Method.html#authorizationType)` 对 API 方法启用 IAM 身份验证。否则，这些 API 方法会从外部进行访问。

 例如，要授予某个用户查看特定 API 提供的宠物列表的权限，同时拒绝该用户向列表添加宠物的权限，您可以在 IAM 策略中包含以下语句：

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "execute-api:Invoke"           
      ],
      "Resource": [
        "arn:aws:execute-api:us-east-1:111111111111:api-id/*/GET/pets"
      ]
    },
    {
      "Effect": "Deny",
      "Action": [
        "execute-api:Invoke"           
      ],
      "Resource": [
        "arn:aws:execute-api:us-east-1:111111111111:api-id/*/POST/pets"
      ]
    }
  ]
}
```

------

要授予某个用户查看配置为 `GET /pets/{petId}` 的 API 提供的某个特定宠物的权限，您可以在 IAM 策略中包含以下语句：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "execute-api:Invoke"
            ],
            "Resource": [
                "arn:aws:execute-api:us-east-1:111122223333:api-id/*/GET/pets/a1b2"
            ]
        }
    ]
}
```

------

## 在 API Gateway 中执行 API 的 IAM 策略的语句参考
<a name="api-gateway-calling-api-permissions"></a>

以下信息将介绍用于执行 API 的访问权限的 IAM 策略语句的 Action 和 Resource 格式。

### 在 API Gateway 中执行 API 的权限的 Action 格式
<a name="api-gateway-iam-policy-action-format-for-executing-api"></a>

API 执行的 `Action` 表达式有以下一般格式：

```
execute-api:action
```

其中 *action* 是一项可用的 API 执行操作：
+ **\$1**，代表以下所有操作。
+ **Invoke (调用)**，用于根据客户端请求调用 API。
+ **InvalidateCache**，用于根据客户端请求使 API 缓存失效。

### 在 API Gateway 中执行 API 的权限的 Resource 格式
<a name="api-gateway-iam-policy-resource-format-for-executing-api"></a>

API 执行的 `Resource` 表达式有以下一般格式：

```
arn:aws:execute-api:region:account-id:api-id/stage-name/HTTP-VERB/resource-path-specifier
```

其中：
+ *region* 为 AWS 区域（例如，适用于所有 **us-east-1** 区域的 **\$1** 或 AWS），与该方法已部署的 API 相对应。
+ *account-id* 是 REST API 所有者的 12 位 AWS 账户 ID。
+ *api-id* 是 API Gateway 为该方法分配给 API 的标识符。
+ *stage-name* 是与方法关联的阶段的名称。
+ *HTTP-VERB* 是该方法的 HTTP 动词。它可以是以下任一动词：GET、POST、PUT、DELETE、PATCH。
+ *resource-path-specifier* 是前往预期方法的路径。

**注意**  
如果您指定了通配符 (`*`)，则 `Resource` 表达式会将通配符应用于表达式的其余部分。

一些示例资源表达式包括：
+ **arn:aws:execute-api:\$1:\$1:\$1**，适用于任何 AWS 区域内任何 API 的任何阶段中的任何资源路径。
+ **arn:aws:execute-api:us-east-1:\$1:\$1**，适用于 AWS 的任何 `us-east-1` 区域内任何 API 的任何阶段中的任何资源路径。
+ **arn:aws:execute-api:us-east-1:\$1:*api-id*/\$1**，适用于AWS区域 us-east-1 内带 *api-id* 标识符的 API 中任何阶段的任何资源路径。
+ **arn:aws:execute-api:us-east-1:\$1:*api-id*/`test`/\$1**，适用于 AWS 区域 us-east-1 内带 *api-id* 标识符的 API 中 `test` 阶段的任何资源路径。

要了解更多信息，请参阅“[API Gateway Amazon 资源名称 (ARN) 参考](arn-format-reference.md)”。