Skip to content

Instantly share code, notes, and snippets.

@okram999
Created April 15, 2019 18:46
Show Gist options
  • Save okram999/a0066347682fc0c34f5d841e4f549ce3 to your computer and use it in GitHub Desktop.
Save okram999/a0066347682fc0c34f5d841e4f549ce3 to your computer and use it in GitHub Desktop.
# original source: https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/curator.html
import boto3
from requests_aws4auth import AWS4Auth
from elasticsearch import Elasticsearch, RequestsHttpConnection
import curator
host = 'vpc-ava-uat2-es-6fv4pfs3r6vvhdf2gadimydigi.us-east-1.es.amazonaws.com'
region = 'us-east-1'
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
def publishMsg(data, es_env, count, topic_name):
''' Sents notification post the document rotation '''
client = boto3.client('sns')
print("Sending email notification......")
response = client.publish(
TopicArn=topic_name,
Message=f"Deleted {data} Elastic Search indices from {es_env} older then {count} days",
Subject=f"Automatic rotation of Elastic Search indices for {es_env}",
MessageStructure='string')
print(response)
# Lambda execution starts here.
def lambda_handler():
# Build the Elasticsearch client.
es = Elasticsearch(
hosts = [{'host': host, 'port': 443}],
http_auth = awsauth,
use_ssl = True,
verify_certs = True,
connection_class = RequestsHttpConnection
)
index_list = curator.IndexList(es)
# Filters by age, anything with a time stamp older than 30 days in the index name.
index_list.filter_by_age(source='name', direction='older', timestring='%Y.%m.%d', unit='days', unit_count=30)
# Filters by naming prefix.
# index_list.filter_by_regex(kind='prefix', value='my-logs-2017')
# Filters by age, anything created more than one month ago.
# index_list.filter_by_age(source='creation_date', direction='older', unit='months', unit_count=1)
print("Found %s indices to delete" % len(index_list.indices))
indices_to_delete = len(index_list.indices)
# # # # If our filtered list contains any indices, delete them.
# if index_list.indices:
# curator.DeleteIndices(index_list).do_action()
publishMsg(indices_to_delete, 'uat', 30, 'arn:aws:sns:us-east-1:577595225843:AVA-UAT-ALERTS')
lambda_handler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment