本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
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 机器人的账户编号。
AWS:SourceArn
是引发对 Amazon Lex V2 机器人别名的调用的 Amazon Connect 服务实例或 Lambda 函数的资源 ARN。
- 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
"
}
}
}
]
}