【问题标题】:How to issue POST requests to opensearch cluster in VPC?如何向 VPC 中的 opensearch 集群发出 POST 请求?
【发布时间】:2022-10-22 22:38:29
【问题描述】:

我已将 opensearch 集群部署到 VPC 中。我有一个 VPC 终端节点,可以成功地向“VPC 终端节点 URL”/_cluster/settings 发出 GET 请求以获取集群配置,但向同一 URL 发出 POST 请求不起作用。是否应该向“VPC 端点 URL”/_cluster/settings 发出 POST 请求?

查看文档 - https://docs.aws.amazon.com/opensearch-service/latest/developerguide/configuration-api.html#configuration-api-actions-describedomainconfig - 它说 -

POST https://es.us-east-1.amazonaws.com/2021-01-01/opensearch/domain/domain-name/config
"SnapshotOptions": {
    "AutomatedSnapshotStartHour": 3
  }

由于这是一个公共 URL,因此我无法对我的集群执行此操作。我还尝试了对“VPC 端点 URL”/config 的 POST 请求,但没有任何运气。

我的工作 GET 请求 -

def lambda_handler(event, context):
    x = requests.get('https://vpc-<private endpoint>.us-east-1.es.amazonaws.com/_cluster/settings')

GET 请求的成功输出 -

Function Logs
:"5s","max_index_buffer_size":"-1","shard_inactive_time":"5m",...

我失败的 POST 请求 -

def lambda_handler(event, context):
    url = 'https://vpc-<private endpoint>.us-east-1.es.amazonaws.com/_cluster/settings'
    myobj = {"SnapshotOptions": {
    "AutomatedSnapshotStartHour": 3
  } }
    x = requests.post(url, json = myobj)

错误信息 -

Function Logs
START RequestId: b483f2ca-0051-468a-81cf-8a771a667bd2 Version: $LATEST
{"Message":"Your request: '/_cluster/settings' is not allowed for verb: POST"}

【问题讨论】:

  • 您的域 IAM 政策是什么?

标签: python amazon-web-services aws-lambda opensearch amazon-opensearch


【解决方案1】:

您必须从官方文档中检查该 API 路径支持的方法。

目前,它仅支持:

GET _cluster/settings
PUT _cluster/settings

参考:

【讨论】:

    【解决方案2】:

    我发现的解决方案是签署请求。 - https://docs.aws.amazon.com/opensearch-service/latest/developerguide/request-signing.html

    前任。 -

    from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth
    import boto3
    
    host = '' # cluster endpoint, for example: my-test-domain.us-east-1.es.amazonaws.com
    region = '' # e.g. us-west-1
    
    credentials = boto3.Session().get_credentials()
    auth = AWSV4SignerAuth(credentials, region)
    index_name = 'movies'
    
    client = OpenSearch(
        hosts = [{'host': host, 'port': 443}],
        http_auth = auth,
        use_ssl = True,
        verify_certs = True,
        connection_class = RequestsHttpConnection
    )
    
    q = 'miller'
    query = {
      'size': 5,
      'query': {
        'multi_match': {
          'query': q,
          'fields': ['title^2', 'director']
        }
      }
    }
    
    response = client.search(
        body = query,
        index = index_name
    )
    
    print('
    Search results:')
    print(response)
    

    【讨论】:

      猜你喜欢
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-24
      相关资源
      最近更新 更多