我的函数可以从本地 Jupyter 笔记本将文档批量索引到我的 AWS Elasticsearch,但是当我部署到 Lambda 时,它不断返回此错误:
"errorMessage": "AuthorizationException(403, 'security_exception', 'no permissions for
[indices:data/write/bulk] and User [name=arn:aws:iam::xxxxxxxxxxxx:role/MyLambdaRole,
backend_roles=[arn:aws:iam::xxxxxxxxxxxx:role/MyLambdaRole], requestedTenant=null]')"
我的 Elasticsearch 域(v7.7)配置如下:
Fine-grained access control: Enabled
Master user type: Internal user database
SAML authentication: Disabled
Amazon Cognito for authentication: Disabled
Require HTTPS: Enabled
Encryption at rest: Enabled
KMS master keyarn:aws:kms:us-east-1:xxxxxxxxxxxxx:key/<aws/es key>
Node-to-node encryption: Enabled
域的访问策略包含:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "es:*",
"Resource": "*"
}
]
}
MyLambdaRole 的 IAM 策略包含:
...
{
"Action": [
"es:*"
],
"Resource": [
"*"
],
"Effect": "Allow"
}
]
在 Kibana 中,我已在 Security -> Role Mappings -> all_access 下映射了我的 AWS 管理员 IAM 用户和 MyLambdaRole。我尝试了不同的组合,将它们添加到后端角色并将它们添加到 security_manager。
Lambda 使用 AWS Signature v4 身份验证,elasticsearch 客户端版本为 7.7.0:
import boto3
from elasticsearch import Elasticsearch, RequestsHttpConnection, helpers
from requests_aws4auth import AWS4Auth
session = boto3.Session()
credentials = session.get_credentials().get_frozen_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, \
session.region_name, 'es', session_token=credentials.token)
host = 'search-es-domain.us-east-1.es.amazonaws.com'
es = Elasticsearch(
hosts = [{'host': host, 'port': 443}],
http_auth = awsauth
use_ssl = True,
verify_certs = True,
connection_class = RequestsHttpConnection
)
# Single indexing call
document = { my data }
es.index(index="my_index", doc_type="_doc", id=doc_id, body=document)
# Bulk indexing call
k = ({ my data })
helpers.bulk(es, k)
如果我用我的 Kibana 凭据替换
http_auth = awsauth
http_auth = (kibana_username, kibana_password)
,它会返回状态 200,但索引中不会创建新文档,这很奇怪。
我想知道我可能缺少什么或者我的配置可能在哪里被关闭。
这是因为启用了细粒度访问控制。我遇到了完全相同的问题,细粒度的访问控制会导致问题。您的笔记本可能正在使用您指定的主 ARN,该主 ARN 具有访问权限并且基本上始终被允许。
我重新创建了我的 ES 实例并禁用了细粒度访问控制,并且仅使用域策略,因为它适合我们的设置。
在此处阅读更多内容,并注意突出显示的部分重新用户/IAM 混合且无法正常工作。
https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/fgac.html
我正在使用 Opensearch。我在插入文档时遇到了同样的问题。这个博客帮助我。他们建议添加一个 lambda 角色 ARN,用于访问角色 -> 映射用户 -> 管理映射 -> 后端角色中的打开搜索仪表板/Kibana。对我来说它有效。详细步骤见以下网址
https://aws.amazon.com/premiumsupport/knowledge-center/opensearch-troubleshoot-cloudwatch-logs/ 在此处输入链接描述
"type":"security_exception","reason":"no permissions for [indices:data/write/bulk]