

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

# Amazon Lex V2 基于资源的策略示例
<a name="security_iam_resource-based-policy-examples"></a>

*基于资源的策略*将附加到诸如机器人或机器人别名等资源上。通过使用基于资源的策略，您可以指定有权访问资源的用户，以及该等用户可以对资源执行的操作。例如，您可以添加基于资源的策略，以允许用户修改特定的机器人，或者允许用户对特定的机器人别名使用运行时操作。

当您使用基于资源的策略时，您可以允许其他 AWS 服务访问您账户中的资源。例如，您可以允许 Amazon Connect 访问亚马逊 Lex V2 机器人。

要了解如何创建机器人或机器人别名，请参阅 [使用 Amazon Lex V2 机器人](building-bots.md)。

**Topics**
+ [使用控制台来指定基于资源的策略](#security_iam_resource-based-policy-examples-console)
+ [使用 API 来指定基于资源的策略](#security_iam_resource-based-policy-examples-api)
+ [允许 IAM 角色更新机器人并列出机器人别名](#security_iam_resource-based-policy-examples-allow-lex-models)
+ [允许用户与机器人进行对话](#security_iam_resource-based-policy-examples-allow-lex-runtime)
+ [允许 AWS 服务使用特定的 Amazon Lex V2 机器人](#security_iam_resource-based-policy-examples-allow-lex-connect)

## 使用控制台来指定基于资源的策略
<a name="security_iam_resource-based-policy-examples-console"></a>

您可以使用 Amazon Lex V2 控制台来管理您的机器人和机器人别名的基于资源的策略。您输入策略的 JSON 结构，控制台会将其与资源关联。如果已有策略与资源关联，则可以使用控制台来查看和修改该策略。

使用策略编辑器保存策略时，控制台会检查策略的句法。如果策略中存在错误，例如用户不存在或存在资源不支持的操作，则会返回错误且不会保存该策略。

下图是针对控制台中机器人的基于资源的策略编辑器。机器人别名的策略编辑器与此类似。

![\[\]](http://docs.aws.amazon.com/zh_cn/lexv2/latest/dg/images/resource-policy-editor.png)


**要打开机器人的策略编辑器，请执行以下操作：**

1. 登录 AWS 管理控制台并通过以下网址打开 Amazon Lex 控制台[https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/)。

1. 从**机器人**列表中，选择要编辑其策略的机器人。

1. 在**基于资源的策略**部分中，选择**编辑**。

**要打开机器人别名的策略编辑器，请执行以下操作：**

1. 登录 AWS 管理控制台并通过以下网址打开 Amazon Lex 控制台[https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/)。

1. 从**机器人**列表中，选择包含要编辑别名的机器人。

1. 在左侧菜单中选择**别名**，然后选择要编辑的别名。

1. 在**基于资源的策略**部分中，选择**编辑**。

## 使用 API 来指定基于资源的策略
<a name="security_iam_resource-based-policy-examples-api"></a>

您可以使用 API 操作来针对机器人和机器人别名管理基于资源的策略。这些操作包括创建、更新和删除策略的操作。

[CreateResourcePolicy](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateResourcePolicy.html)  
将带有指定策略语句的新资源策略添加到机器人或机器人别名中。

[CreateResourcePolicyStatement](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateResourcePolicyStatement.html)  
将新资源策略语句添加到机器人或机器人别名中。

[DeleteResourcePolicy](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_DeleteResourcePolicy.html)  
从机器人或机器人别名中删除资源策略。

[DeleteResourcePolicyStatement](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_DeleteResourcePolicyStatement.html)  
从机器人或机器人别名中删除资源策略语句。

[DescribeResourcePolicy](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_DescribeResourcePolicy.html)  
获取资源策略和策略修订。

[UpdateResourcePolicy](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_UpdateResourcePolicy.html)  
将机器人或机器人别名的现有资源策略替换为新的资源策略。

### 示例
<a name="collapsible-api-example"></a>

------
#### [ Java ]

以下示例说明如何使用基于资源的策略操作来管理基于资源的策略。

```
        /*
         * Create a new policy for the specified bot alias 
         * that allows a role to invoke lex:UpdateBotAlias on it.
         * The created policy will have revision id 1.
         */
        
        CreateResourcePolicyRequest createPolicyRequest =
                CreateResourcePolicyRequest.builder()
                        .resourceArn("arn:aws:lex:Region:123456789012:bot-alias/MYBOTALIAS/TSTALIASID")
                        .policy("{\"Version\": \"2012-10-17\",\"Statement\": [{\"Sid\": \"BotAliasEditor\",\"Effect\": \"Allow\",\"Principal\": {\"AWS\": \"arn:aws:iam::123456789012:role/BotAliasEditor\"},\"Action\": [\"lex:UpdateBotAlias\"],\"Resource\":[\"arn:aws:lex:Region:123456789012:bot-alias/MYBOTALIAS/TSTALIASID\"]]}")

        lexmodelsv2Client.createResourcePolicy(createPolicyRequest);
        
        /*
         * Overwrite the policy for the specified bot alias with a new policy.
         * Since no expectedRevisionId is provided, this request overwrites the current revision.
         * After this update, the revision id for the policy is 2.
         */
        UpdateResourcePolicyRequest updatePolicyRequest =
        UpdateResourcePolicyRequest.builder()
                        .resourceArn("arn:aws:lex:Region:123456789012:bot-alias/MYBOTALIAS/TSTALIASID")
                        .policy("{\"Version\": \"2012-10-17\",\"Statement\": [{\"Sid\": \"BotAliasEditor\",\"Effect\": \"Deny\",\"Principal\": {\"AWS\": \"arn:aws:iam::123456789012:role/BotAliasEditor\"},\"Action\": [\"lex:UpdateBotAlias\"],\"Resource\":[\"arn:aws:lex:Region:123456789012:bot-alias/MYBOTALIAS/TSTALIASID\"]]}")

        lexmodelsv2Client.updateResourcePolicy(updatePolicyRequest);
        
        /*
         * Creates a statement in an existing policy for the specified bot alias 
         * that allows a role to invoke lex:RecognizeText on it.
         * This request expects to update revision 2 of the policy. The request will fail
         * if the current revision of the policy is no longer revision 2.
         * After this request, the revision id for this policy will be 3.
         */
        
        CreateResourcePolicyStatementRequest createStatementRequest =
                CreateResourcePolicyStatementRequest.builder()
                        .resourceArn("arn:aws:lex:Region:123456789012:bot-alias/MYBOTALIAS/TSTALIASID")
                        .effect("Allow")
                        .principal(Principal.builder().arn("arn:aws:iam::123456789012:role/BotRunner").build())
                        .action("lex:RecognizeText")
                        .statementId("BotRunnerStatement")
                        .expectedRevisionId(2)
                        .build();

        lexmodelsv2Client.createResourcePolicyStatement(createStatementRequest);

        /*
         * Deletes a statement from an existing policy for the specified bot alias by statementId.
         * Since no expectedRevisionId is supplied, the request will remove the statement from
         * the current revision of the policy for the bot alias. 
         * After this request, the revision id for this policy will be 4.
         */
        DeleteResourcePolicyRequest deleteStatementRequest =
                DeleteResourcePolicyRequest.builder()
                        .resourceArn("arn:aws:lex:Region:123456789012:bot-alias/MYBOTALIAS/TSTALIASID")
                        .statementId("BotRunnerStatement")                        
                        .build();

        lexmodelsv2Client.deleteResourcePolicy(deleteStatementRequest);
        
        /*
         * Describe the current policy for the specified bot alias
         * It always returns the current revision.
         */
        DescribeResourcePolicyRequest describePolicyRequest =
                DescribeResourcePolicyRequest.builder()
                        .resourceArn("arn:aws:lex:Region:123456789012:bot-alias/MYBOTALIAS/TSTALIASID")
                        .build();

        lexmodelsv2Client.describeResourcePolicy(describePolicyRequest);
        
        
        /*
         * Delete the current policy for the specified bot alias
         * This request expects to delete revision 3 of the policy. Since the revision id for 
         * this policy is already at 4, this request will fail.
         */
        DeleteResourcePolicyRequest deletePolicyRequest =
                DeleteResourcePolicyRequest.builder()
                        .resourceArn("arn:aws:lex:Region:123456789012:bot-alias/MYBOTALIAS/TSTALIASID")
                        .expectedRevisionId(3);
                        .build();

        lexmodelsv2Client.deleteResourcePolicy(deletePolicyRequest);
```

------

## 允许 IAM 角色更新机器人并列出机器人别名
<a name="security_iam_resource-based-policy-examples-allow-lex-models"></a>

以下示例授予特定 IAM 角色调用 Amazon Lex V2 模型构建 API 操作以修改现有机器人的权限。用户可以列出机器人的别名并更新机器人，但无法删除机器人或机器人别名。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "botBuilders",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:role/BotBuilder"
            },
            "Action": [
                "lex:ListBotAliases",
                "lex:UpdateBot"
            ],
            "Resource": [
                "arn:aws:lex:us-east-1:123456789012:bot/MYBOT"
            ]
        }
    ]
}
```

------

## 允许用户与机器人进行对话
<a name="security_iam_resource-based-policy-examples-allow-lex-runtime"></a>

以下示例授予特定用户在机器人的单个别名上调用 Amazon Lex V2 运行时 API 操作的权限。

该用户更新或删除机器人别名的权限被明确拒绝。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "botRunners",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:user/botRunner"
            },
            "Action": [
                "lex:RecognizeText",
                "lex:RecognizeUtterance",
                "lex:StartConversation",
                "lex:DeleteSession",
                "lex:GetSession",
                "lex:PutSession"
            ],
            "Resource": [
                "arn:aws:lex:us-east-1:123456789012:bot-alias/MYBOT/MYBOTALIAS"
            ]
        },
        {
            "Sid": "botRunners",
            "Effect": "Deny",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:user/botRunner"
            },
            "Action": [
                "lex:UpdateBotAlias",
                "lex:DeleteBotAlias"
            ],
            "Resource": [
                "arn:aws:lex:us-east-1:123456789012:bot-alias/MYBOT/MYBOTALIAS"
            ]
        }
    ]
}
```

------

## 允许 AWS 服务使用特定的 Amazon Lex V2 机器人
<a name="security_iam_resource-based-policy-examples-allow-lex-connect"></a>

以下示例授予 AWS Lambda 和 Amazon Connect 调用 Amazon Lex V2 运行时 API 操作的权限。

条件块是服务主体所必需的，并且必须使用全局上下文键 `AWS:SourceAccount` 和 `AWS:SourceArn`。

`AWS:SourceAccount` 是调用 Amazon Lex V2 机器人的账户编号。

`AWS:SourceArn` 是引发对 Amazon Lex V2 机器人别名的调用的 Amazon Connect 服务实例或 Lambda 函数的资源 ARN。