Last active
December 6, 2018 09:54
-
-
Save jangaraj/22b71027192d2e3334854f1acc8c23e6 to your computer and use it in GitHub Desktop.
AWS Lambda Function to backup Grafana 5+ dashboards into S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# multiprocess Lambda function to backup all Grafana dashboards to S3 bucket | |
import datetime, boto3 | |
from botocore.vendored import requests | |
from multiprocessing import Process, Pipe | |
config = { | |
'domain.com': { | |
'url': 'https://domain.com', | |
'key': '=grafana-api-key-with-read-permissions-for-all-dashboards=', | |
}, | |
} | |
def backup_dashboard(d, path, conf, headers, s3): | |
if 'folderTitle' in d: | |
path = path + d['folderTitle'].replace(' ', '-').replace(':', '').replace('/', '-').replace('\'', '') + '/' | |
path = path + d['uid'] + '___' + d['title'].replace(' ', '-').replace(':', '').replace('/', '-').replace('\'', '') + '.json' | |
print path | |
r = requests.get('%s/api/dashboards/uid/%s' % (conf['url'], d['uid']), headers=headers) | |
object = s3.Object('s3-backup', 'backup-folder/' + path) | |
object.put(Body=r.text.encode('utf-8')) | |
def lambda_handler(event, context): | |
global config | |
conf = config[event['config']] | |
dt = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + '/' | |
headers = {'Authorization': 'Bearer %s' % (conf['key'])} | |
r = requests.get('%s/api/search?limit=100000&type=dash-db&query=&' % (conf['url']), headers=headers) | |
s3 = boto3.resource('s3') | |
parent_connections = [] | |
processes = [] | |
for d in r.json(): | |
parent_conn, child_conn = Pipe() | |
parent_connections.append(parent_conn) | |
process = Process(target=backup_dashboard, args=(d, event['config'] + '/' + dt, conf, headers, s3,)) | |
processes.append(process) | |
for process in processes: | |
process.start() | |
for process in processes: | |
process.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment