Amazon Lex V2 的資源型政策範例 - Amazon Lex

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

Amazon Lex V2 的資源型政策範例

以資源為基礎的政策會附加至資源,例如機器人或機器人別名。使用以資源為基礎的策略,您可以指定誰可以存取資源,以及他們可以對其執行的動作。例如,您可以新增以資源為基礎的政策,讓使用者能夠修改特定機器人,或允許使用者對特定機器人別名使用執行階段作業。

當您使用以資源為基礎的策略時,您可以允許其他 AWS 服務存取您帳戶中的資源。例如,您可以允許 Amazon Connect 訪問 Amazon Lex 機器人。

若要瞭解如何建立機器人或機器人別名,請參閱建築機器人

使用主控台指定以資源為基礎的策略

您可以使用 Amazon Lex 主控台管理機器人和機器人別名的資源型政策。您可以輸入策略的 JSON 結構,控制台將其與資源相關聯。如果已經存在與資源相關聯的策略,您可以使用主控台來檢視和修改策略。

當您使用原則編輯器儲存原則時,主控台會檢查原則的語法。如果原則包含錯誤 (例如不存在的使用者或資源不支援的動作),則會傳回錯誤且不儲存原則。

以下顯示主控台中機器人的資源型政策編輯器。機器人別名的原則編輯器類似。

Amazon Lex 主控台以資源為基礎的政策編輯器。
若要開啟機器人的政策編輯器
  1. 登錄到 AWS Management Console 並打開 Amazon Lex 控制台 https://console.aws.amazon.com/lex/.

  2. 從「機器人」清單中,選擇您要編輯其政策的機器人。

  3. 以資源為基礎的政策區段中,選擇編輯

若要開啟機器人別名的原則編輯器
  1. 登錄到 AWS Management Console 並打開 Amazon Lex 控制台 https://console.aws.amazon.com/lex/.

  2. 從「機器人」清單中,選擇包含您要編輯之別名的機器人。

  3. 從左側選單中選擇別名,然後選擇要編輯的別名。

  4. 以資源為基礎的政策區段中,選擇編輯

使用 API 指定以資源為基礎的政策

您可以使用 API 操作來管理機器人和機器人別名的資源型政策。有建立、更新和刪除策略的作業。

CreateResource政策

將具有指定政策陳述式的新資源策略新增至機器人或機器人別名。

CreateResourcePolicyStatement

將新的資源政策陳述式新增至機器人或機器人別名。

DeleteResource政策

從機器人或機器人別名中移除資源策略。

DeleteResourcePolicyStatement

從機器人或機器人別名移除資源政策陳述式。

DescribeResource政策

取得資源策略和策略修訂版本。

UpdateResource政策

將機器人或機器人別名的現有資源策略取代為新的別名。

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 角色更新機器人並列出機器人別名

下列範例授予特定 IAM 角色的許可,以呼叫 Amazon Lex V2 模型建置 API 操作以修改現有機器人。使用者可以列出機器人的別名並更新機器人,但無法刪除機器人或機器人別名。

{ "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:Region:123456789012:bot/MYBOT" ] } ] }

允許使用者與機器人進行對話

下列範例授予特定使用者在機器人的單一別名上呼叫 Amazon Lex V2 執行階段 API 操作的權限。

特別拒絕使用者更新或刪除機器人別名的權限。

{ "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:Region: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:Region:123456789012:bot-alias/MYBOT/MYBOTALIAS" ] } ] }

允許 AWS 服務使用特定的 Amazon Lex V2 機器人

下列範例授予 AWS Lambda 和 Amazon Connect 的權限,以呼叫 Amazon Lex V2 執行階段 API 作業。

服務主體需要條件區塊,且必須使用全域前後關聯索引鍵AWS:SourceAccountAWS:SourceArn

AWS:SourceAccount是呼叫 Amazon Lex V2 機器人的帳戶識別碼。

AWS:SourceArn是 Amazon Connect 服務執行個體或 Lambda 函數的資源 ARN,呼叫 Amazon Lex V2 機器人別名的來源。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "connect-bot-alias", "Effect": "Allow", "Principal": { "Service": [ "connect.amazonaws.com" ] }, "Action": [ "lex:RecognizeText", "lex:StartConversation" ], "Resource": [ "arn:aws:lex:Region:123456789012:bot-alias/MYBOT/MYBOTALIAS" ], "Condition": { "StringEquals": { "AWS:SourceAccount": "123456789012" }, "ArnEquals": { "AWS:SourceArn": "arn:aws:connect:Region:123456789012:instance/instance-id" } } }, { "Sid": "lambda-function", "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" ] }, "Action": [ "lex:RecognizeText", "lex:StartConversation" ], "Resource": [ "arn:aws:lex:Region:123456789012:bot-alias/MYBOT/MYBOTALIAS" ], "Condition": { "StringEquals": { "AWS:SourceAccount": "123456789012" }, "ArnEquals": { "AWS:SourceArn": "arn:aws:lambda:Region:123456789012:function/function-name" } } } ] }