DEV Community

Cover image for How to register a S3 repository used to store Elasticsearch Snapshots.
Thodoris Velmachos
Thodoris Velmachos

Posted on • Updated on

How to register a S3 repository used to store Elasticsearch Snapshots.

Hello, I would like to share with a small python function I have designed to register a AWS S3 bucket as a snapshot repository for AWS Opensearch.

import boto3
import requests
from requests_aws4auth import AWS4Auth

region = 'eu-central-1'
service = 'es'
role_arn = "arn:aws:iam::<accountid>:role/EsSnapshotRole"
host = '<elasticsearch Endpoint>' 
path = '/_snapshot/moo-es-snapshots'
url = host + path

credentials = boto3.Session(profile_name="moo-stag-es").get_credentials()

awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)

#Register repository

payload = {
  "type": "s3",
  "settings": {
    "bucket": "<bucket-name>",
    "region": region,
    "role_arn": ""
  }
}

headers = {"Content-Type": "application/json"}
try:
   r = requests.put(url, auth=awsauth, json=payload,        headers=headers)

   print(r.status_code)
   print(r.text)

except Exception as e:
        print(e) 
Enter fullscreen mode Exit fullscreen mode

I hope you like the tutorial, if you do give a thumps up! and follow me in Twitter, also you can subscribe to my Newsletter in order to avoid missing any of the upcoming tutorials.

Media Attribution

I would like to thank Clark Tibbs for designing the awesome [photo

Top comments (0)