我需要在启用细粒度访问控制的新 OpenSearch 实例中索引一些数据。
我创建了一个运行以下 Python 代码的 Lambda 函数,并将以下策略添加到 Lambda 执行角色:
AmazonESFullAccess
AmazonOpenSearchServiceFullAccess
我的代码如下所示:
import boto3
import re
import requests
from requests_aws4auth import AWS4Auth
from data_to_index import bulk_data_to_index
region = 'ap-south-1' # e.g. us-west-1
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
host = 'my_domain' # the OpenSearch Service domain, e.g. https://search-mydomain.us-west-1.es.amazonaws.com
index = 'my_index'
datatype = '_doc'
url = host + '/' + index + '/' + datatype
headers = { "Content-Type": "application/json" }
# Lambda execution starts here
def lambda_handler(event, context):
for data in bulk_data_to_index:
document = {
"key1": data[1],
"key2": data[2],
"data": ', '.join(data)
}
r = requests.post(url, auth=awsauth, json=document, headers=headers)
# Print response content and status code
print("Response Content:", r.content)
print("Status Code:", r.status_code)
# Check if the request was successful
if r.status_code == 201:
print("Document added successfully to OpenSearch!")
else:
print("Failed to add document to OpenSearch. Status code:", r.status_code)
print("Error Response:", r.text)
运行 Lambda 时,我收到错误:
{
"error": {
"root_cause": [
{
"type": "security_exception",
"reason": "no permissions for [indices:data/write/index] and User [name=my_lambda_role, backend_roles=[my_lambda_role], requestedTenant=null]"
}
],
"type": "security_exception",
"reason": "no permissions for [indices:data/write/index] and User [name=my_lambda_role, backend_roles=[my_lambda_role], requestedTenant=null]"
},
"status": 403
}
为什么
AmazonOpenSearchServiceFullAccess
不给我所需的所有访问权限?
如何解决这个问题?
为什么
不给我所需的所有访问权限?AmazonOpenSearchServiceFullAccess
AmazonOpenSearchServiceFullAccess
提供配置 OpenSearch 服务的完全访问权限。它允许任何资源对任何资源执行任何 es:*
操作,例如es:DescribeDomain
获取 OpenSearch 域的详细信息,或 es:DeleteDomain
删除 OpenSearch 域。
它指定 IAM 委托人在 OpenSearch 域的外部拥有的权限。
它没有指定 IAM 主体的权限,例如您的 Lambda 函数执行角色在 OpenSearch 域本身内。
您已授予 Lambda 函数完全访问权限来配置域、列出域、删除域、更新域配置、升级域等,但在域本身内无访问权限(例如创建索引)。
如何解决这个问题?您需要执行
角色映射以授予您的 Lambda 执行角色域内权限。
将 Lambda 角色 ARN 作为后端角色映射到您创建的自定义角色,或将授予您创建索引的访问权限的预定义 OpenSearch 角色之一,即 all_access
。您可以通过
OpenSearch API 或通过 docs 后面的控制台来执行此操作: