在AWS S3托管网站部署LexBot时遇资源不存在错误求助
排查LexBot部署到S3网站时的"The specified resource 'xxxx' does not exist"错误
可能的原因及解决步骤
1. 验证Lex Bot别名的可用性与权限
代码中使用$LATEST作为Bot别名,需确认:
- 该Bot的
$LATEST版本已发布,未发布的版本无法被外部调用 - Cognito身份池对应的IAM角色拥有
lex:PostText权限,资源路径需精确匹配(格式:arn:aws:lex:eu-west-1:你的AWS账号ID:bot/MoodleUlsterBotHelp:alias/$LATEST)
2. 确认区域一致性
代码中设置区域为eu-west-1,需同步验证:
- Lex Bot确实部署在
eu-west-1区域,若Bot在其他区域,需修改代码中的region参数 - Cognito身份池也处于同一区域,跨区域调用会触发资源不存在错误
3. 检查Bot名称的大小写与拼写
Lex Bot名称大小写敏感,代码中botName: 'MoodleUlsterBotHelp'必须与AWS控制台中Bot的名称完全一致,包括大小写和特殊字符
4. 校验IAM角色的信任关系与权限
Cognito身份池的未认证/认证角色需满足:
- 信任策略允许
cognito-identity.amazonaws.com扮演该角色 - 权限策略明确允许访问目标Lex Bot,示例策略:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "lex:PostText", "Resource": "arn:aws:lex:eu-west-1:你的AWS账号ID:bot/MoodleUlsterBotHelp:alias/$LATEST" } ] }
5. 检查S3网站的CORS配置
S3托管网站需配置CORS规则,允许调用Lex和Cognito API,示例配置:
<CORSConfiguration> <CORSRule> <AllowedOrigin>*</AllowedOrigin> <!-- 生产环境建议限制为你的域名 --> <AllowedMethod>POST</AllowedMethod> <AllowedMethod>GET</AllowedMethod> <AllowedHeader>*</AllowedHeader> </CORSRule> </CORSConfiguration>
6. 确认Lex运行时客户端初始化
确保AWS.LexRuntime客户端的区域与Bot一致,代码中已设置region: "eu-west-1",需检查AWS SDK是否正确加载,无区域覆盖情况
你的完整代码参考
//set the focus to the input box document.getElementById("wisdom").focus(); //初始化Amazon Cognito凭证提供程序 AWS.config.update({ region: "eu-west-1", credentials: new AWS.CognitoIdentityCredentials({ IdentityPoolId: "eu-west-1:e67246ad-9f60-45fd-b8bd-6cdab757f4af" }) }); var lexruntime = new AWS.LexRuntime(); var lexUserId = 'chatbot-demo' + Date.now(); var sessionAttributes = {}; function pushChat() { //若存在需要发送的文本... var wisdomText = document.getElementById('wisdom'); if (wisdomText && wisdomText.value && wisdomText.value.trim().length > 0) { //禁用输入框以显示正在发送 var wisdom = wisdomText.value.trim(); wisdomText.value = '...'; wisdomText.locked = true; //将文本发送至Lex运行时 var params = { botAlias: '$LATEST', botName: 'MoodleUlsterBotHelp', inputText: wisdom, userId: lexUserId, sessionAttributes: sessionAttributes }; showRequest(wisdom); lexruntime.postText(params, function(err, data) { if (err) { console.log(err, err.stack); showError('Error: ' + err.message + ' (see console for details)') } if (data) { //捕获sessionAttributes用于下一交互周期 sessionAttributes = data.sessionAttributes; //显示响应及/或错误/对话状态 showResponse(data); } //重新启用输入框 wisdomText.value = ''; wisdomText.locked = false; }); } //始终取消表单提交 return false; } function showRequest(daText) { var conversationDiv = document.getElementById('conversation'); var requestPara = document.createElement("P"); requestPara.className = 'userRequest'; requestPara.appendChild(document.createTextNode(daText)); conversationDiv.appendChild(requestPara); conversationDiv.scrollTop = conversationDiv.scrollHeight; } function showError(daText) { var conversationDiv = document.getElementById('conversation'); var errorPara = document.createElement("P"); errorPara.className = 'lexError'; errorPara.appendChild(document.createTextNode(daText)); conversationDiv.appendChild(errorPara); conversationDiv.scrollTop = conversationDiv.scrollHeight; } function showResponse(lexResponse) { var conversationDiv = document.getElementById('conversation'); var responsePara = document.createElement("P"); responsePara.className = 'lexResponse'; if (lexResponse.message) { responsePara.appendChild(document.createTextNode(lexResponse.message)); responsePara.appendChild(document.createElement('br')); } if (lexResponse.dialogState === 'ReadyForFulfillment') { responsePara.appendChild(document.createTextNode( 'Ready for fulfillment')); // TODO: show slot values } else { responsePara.appendChild(document.createTextNode( '(' + lexResponse.dialogState + ')')); } conversationDiv.appendChild(responsePara); conversationDiv.scrollTop = conversationDiv.scrollHeight; }
内容的提问来源于stack exchange,提问作者Hugo BLOIS
相关产品推荐
相关产品推荐

