

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

# Amazon Lex V2 的資源型政策範例
<a name="security_iam_resource-based-policy-examples"></a>

以*資源為基礎的政策*會連接到資源，例如機器人或機器人別名。透過以資源為基礎的政策，您可以指定誰可以存取資源，以及他們可以對其執行的動作。例如，您可以新增資源型政策，讓使用者修改特定機器人，或允許使用者在特定機器人別名上使用執行期操作。

當您使用以資源為基礎的政策時，您可以允許其他 AWS 服務存取您帳戶中的資源。例如，您可以允許 Amazon Connect 存取 Amazon 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_tw/lexv2/latest/dg/images/resource-policy-editor.png)


**開啟機器人的政策編輯器**

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/) 開啟 Amazon Lex 主控台。

1. 從**機器人**清單中，選擇您要編輯其政策的機器人。

1. 在**資源型政策**區段中，選擇**編輯**。

**開啟機器人別名的政策編輯器**

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/lex/](https://console.aws.amazon.com/lex/) 開啟 Amazon 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 機器人的帳戶 ID。

`AWS:SourceArn` 是對 Amazon Lex V2 機器人別名發出的 Amazon Connect 服務執行個體或 Lambda 函數的資源 ARN。 Amazon Lex V2 