本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Amazon Lex V2 的資源型政策範例
以資源為基礎的政策會連接到資源,例如機器人或機器人別名。透過以資源為基礎的政策,您可以指定誰可以存取資源,以及他們可以對其執行的動作。例如,您可以新增資源型政策,讓使用者修改特定機器人,或允許使用者在特定機器人別名上使用執行期操作。
當您使用以資源為基礎的政策時,您可以允許其他服務 AWS 存取您帳戶中的資源。例如,您可以允許 Amazon Connect 存取機器人。
若要了解如何建立機器人或機器人別名,請參閱 使用 Amazon Lex V2 機器人。
使用 主控台指定以資源為基礎的政策
您可以使用 主控台來管理機器人和機器人別名的資源型政策。您可以輸入政策的 JSON 結構,並且主控台會將其與資源建立關聯。如果已有與資源相關聯的政策,您可以使用 主控台來檢視和修改政策。
當您使用政策編輯器儲存政策時,主控台會檢查政策的語法。如果政策包含錯誤,例如不存在的使用者或資源不支援的動作,則會傳回錯誤,且不會儲存政策。
以下顯示 主控台中機器人的資源型政策編輯器。機器人別名的政策編輯器類似。
使用 API 指定以資源為基礎的政策
您可以使用 API 操作來管理機器人和機器人別名的資源型政策。有操作可以建立、更新和刪除政策。
- 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 操作來修改現有機器人。使用者可以列出機器人的別名並更新機器人,但無法刪除機器人或機器人別名。
- 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
"
]
}
]
}
允許使用者與機器人進行對話
下列範例授予特定使用者在機器人的單一別名上呼叫 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 機器人
下列範例會授予 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
- JSON
-
-
{
"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:us-east-1
:123456789012
:bot-alias/MYBOT
/MYBOTALIAS
"
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "123456789012
"
},
"ArnEquals": {
"AWS:SourceArn": "arn:aws:connect:us-east-1
:123456789012
:instance/instance-id
"
}
}
},
{
"Sid": "lambda-function
",
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": [
"lex:RecognizeText",
"lex:StartConversation"
],
"Resource": [
"arn:aws:lex:us-east-1
:123456789012
:bot-alias/MYBOT
/MYBOTALIAS
"
],
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "123456789012
"
},
"ArnEquals": {
"AWS:SourceArn": "arn:aws:lambda:Region
:123456789012
:function/function-name
"
}
}
}
]
}