Skip to content

Instantly share code, notes, and snippets.

@rifki192
Created November 9, 2016 04:29
Show Gist options
  • Save rifki192/e04791ffc1a77d8d946cd51178b1bffe to your computer and use it in GitHub Desktop.
Save rifki192/e04791ffc1a77d8d946cd51178b1bffe to your computer and use it in GitHub Desktop.
Serverless curator elasticsearch ver 3.5.1 (AWS ES Supported)
certifi==2016.8.8
elasticsearch-curator==3.5.1
PyYAML==3.11
# Run Elasticsearch Curator from AWS Lambda.
#
# Edit serverless-curator.yaml to define which indices should be purged.
from __future__ import print_function
import certifi
import curator
import yaml
# from curator.exceptions import NoIndices
from elasticsearch import Elasticsearch
# This is the entry point where Lambda will start execution.
def handler(event, context):
# For this function, we don't care about 'event' and 'context',
# but they need to be in the function signature anyway.
with open('serverless-curator.yaml') as config_file:
config = yaml.load(config_file)
# Create a place to track any indices that are deleted.
deleted_indices = {}
# We can define multiple Elasticsearch clusters to manage, so we'll have
# an outer loop for working through them.
for cluster_config in config:
cluster_name = cluster_config['name']
deleted_indices[cluster_name] = []
# Create a collection to the cluster. We're using mangaged clusters in
# Elastic Cloud for this example, so we can enable SSL security.
es = Elasticsearch(cluster_config['endpoint'], use_ssl=True,
verify_certs=True, ca_certs=certifi.where())
# Now we'll work through each set of time-series indices defined in
# our config for this cluster.
for index in cluster_config['indices']:
prefix = index['prefix']
print('Checking "%s" indices on %s cluster.' %
(prefix, cluster_name))
working_list = []
# Fetch all the index names.
indices = curator.get_indices(es)
try:
filter_list = []
# Reduce the list to those that match the prefix.
filter_list.append(curator.build_filter(kindOf='prefix', value=prefix))
# Reduce again, by age.
filter_list.append(
curator.build_filter(
kindOf='older_than', value=index['days'], time_unit='days',
timestring='%Y.%m.%d'
)
)
for filter in filter_list:
working_list = curator.apply_filter(indices, **filter)
if working_list:
curator.delete(es, working_list)
# If nothing is left in the list, we'll get a NoIndices exception.
# That's OK.
except Exception:
pass
# Record the names of any indices we removed.
deleted_indices[cluster_name].extend(working_list)
lambda_response = {'deleted': deleted_indices}
print(lambda_response)
return lambda_response
---
# Define Elasticsearch Clusters and indices here, to have them periodically
# pruned by Curator.
- name: example logging cluster
endpoint: https://curator:[email protected]:9243/
indices:
- prefix: logstash-
days: 365
- name: example metrics cluster
endpoint: https://curator:[email protected]:9243
indices:
- prefix: metricbeat-
days: 14
- prefix: packetbeat-
days: 14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment