ElasticSearch备份和恢复

7kqas0il  于 2023-02-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(199)

我尝试使用lambda函数对ElasticSearch域上的索引进行快照。

import boto3
from requests_aws4auth import AWS4Auth
import requests
from datetime import datetime
#from datetime import time
import datetime
now=datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
print(now)
#now2 = time.now2()
def register_repo(host,region,service):
  credentials = boto3.Session().get_credentials()
  awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
  # Register repository
  path = '_snapshot/backup_es' # the OpenSearch API endpoint
  url = host + path
  payload = {
    "type": "s3",
    "settings": {
      "bucket": "qa-domain-es-backup-s3-use1",
      "region": "us-east-1",
      "role_arn": "arn:aws:iam::133375953867:role/ArgusMemberPortal_LambdaRole"
    }
  }
  headers = {"Content-Type": "application/json"}
  r = requests.put(url, auth=awsauth, json=payload, headers=headers)
  print(r.status_code)
  print(r.text)
  # Take snapshot
  path = f'_snapshot/backup_es/{now}'
  url = host + path
  r = requests.put(url, auth=awsauth)
  print(r.text)

  #Restore snapshot
  # path = '_snapshot/my-snapshot-repo-name/my-snapshot/_restore'
  # url = host + path
  # payload = {"indices": "my-index"}
  # headers = {"Content-Type": "application/json"}
  # r = requests.post(url, auth=awsauth, json=payload, headers=headers)
  # print(r.text)

  

def lambda_handler(event, context):
  host = 'https://vpc-qa-domain-ess2-use1-hyrawwu4edkpqqcusxvyywq7vi.us-east-1.es.amazonaws.com/' # include https:// and trailing /
  region = 'us-east-1' # e.g. us-west-1
  service = 'es'
  register_repo(host,region,service)

在运行上面的代码时,我得到以下错误

2023-02-20-14-58-29
START RequestId: f71ec64a-a60a-4119-ba94-15f0a542393e Version: $LATEST
[ERROR] ConnectionError: HTTPSConnectionPool(host='vpc-qa-domain-ess2-use1-hyrawwu4edkpqqcusxvyywq7vi.us-east-1.es.amazonaws.com', port=443): Max retries exceeded with url: /_snapshot/backup_es (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f21b5747910>: Failed to establish a new connection: [Errno 110] Connection timed out'))
Traceback (most recent call last):
  File "/var/task/es-backup-restore.py", line 48, in lambda_handler
    register_repo(host,region,service)
  File "/var/task/es-backup-restore.py", line 25, in register_repo
    r = requests.put(url, auth=awsauth, json=payload, headers=headers)
  File "/opt/python/requests/api.py", line 132, in put
    return request('put', url, data=data, **kwargs)
  File "/opt/python/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/opt/python/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/opt/python/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/opt/python/requests/adapters.py", line 519, in send
    raise ConnectionError(e, request=request)END RequestId: f71ec64a-a60a-4119-ba94-15f0a542393e
REPORT RequestId: f71ec64a-a60a-4119-ba94-15f0a542393e  Duration: 130393.33 ms  Billed Duration: 130394 ms

我不知道为什么我得到的错误。有人可以请帮助我与相同的。
我尝试使用lambda函数对ElasticSearch域上的索引进行快照。

import boto3
from requests_aws4auth import AWS4Auth
import requests
from datetime import datetime
#from datetime import time
import datetime
now=datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
print(now)
#now2 = time.now2()
def register_repo(host,region,service):
  credentials = boto3.Session().get_credentials()
  awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
  # Register repository
  path = '_snapshot/backup_es' # the OpenSearch API endpoint
  url = host + path
  payload = {
    "type": "s3",
    "settings": {
      "bucket": "qa-domain-es-backup-s3-use1",
      "region": "us-east-1",
      "role_arn": "arn:aws:iam::133375953867:role/ArgusMemberPortal_LambdaRole"
    }
  }
  headers = {"Content-Type": "application/json"}
  r = requests.put(url, auth=awsauth, json=payload, headers=headers)
  print(r.status_code)
  print(r.text)
  # Take snapshot
  path = f'_snapshot/backup_es/{now}'
  url = host + path
  r = requests.put(url, auth=awsauth)
  print(r.text)

  #Restore snapshot
  # path = '_snapshot/my-snapshot-repo-name/my-snapshot/_restore'
  # url = host + path
  # payload = {"indices": "my-index"}
  # headers = {"Content-Type": "application/json"}
  # r = requests.post(url, auth=awsauth, json=payload, headers=headers)
  # print(r.text)

  

def lambda_handler(event, context):
  host = 'https://vpc-qa-domain-ess2-use1-hyrawwu4edkpqqcusxvyywq7vi.us-east-1.es.amazonaws.com/' # include https:// and trailing /
  region = 'us-east-1' # e.g. us-west-1
  service = 'es'
  register_repo(host,region,service)

在运行上面的代码时,我得到以下错误

2023-02-20-14-58-29
START RequestId: f71ec64a-a60a-4119-ba94-15f0a542393e Version: $LATEST
[ERROR] ConnectionError: HTTPSConnectionPool(host='vpc-qa-domain-ess2-use1-hyrawwu4edkpqqcusxvyywq7vi.us-east-1.es.amazonaws.com', port=443): Max retries exceeded with url: /_snapshot/backup_es (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f21b5747910>: Failed to establish a new connection: [Errno 110] Connection timed out'))
Traceback (most recent call last):
  File "/var/task/es-backup-restore.py", line 48, in lambda_handler
    register_repo(host,region,service)
  File "/var/task/es-backup-restore.py", line 25, in register_repo
    r = requests.put(url, auth=awsauth, json=payload, headers=headers)
  File "/opt/python/requests/api.py", line 132, in put
    return request('put', url, data=data, **kwargs)
  File "/opt/python/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/opt/python/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/opt/python/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/opt/python/requests/adapters.py", line 519, in send
    raise ConnectionError(e, request=request)END RequestId: f71ec64a-a60a-4119-ba94-15f0a542393e
REPORT RequestId: f71ec64a-a60a-4119-ba94-15f0a542393e  Duration: 130393.33 ms  Billed Duration: 130394 ms

我不知道为什么我得到的错误。有人可以请帮助我与相同的。

xbp102n0

xbp102n01#

通过查看错误,似乎Lambda和Elasticsearch之间在HTTPS端口上的连接未正确建立。您需要检查Lambda是否可以使用HTTPS连接到ES。

  • 安全组:检查ES和Lambda是否在同一安全组中
  • 入站规则:在安全组中创建规则以允许访问Lambda to ES。
  • 使用一些ES查询测试Lambda和ES之间的连通性。

相关问题