Technical Article

Python script - detect DB of any AWS Service

,

In your dev desktop run the following command :

aws configure
  • It will ask for your aws account's access key id and secret key.
  • Make two files ~/.aws/credentials manually. Template of these files should be as below.
  • If you are setting up a DJS job then provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables.

~/.aws/credentials :

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY
#script 1
import subprocess
import sys

serviceToOdinMap = { 'rds' : { 'odinSetMaterial' : 'com.amazon.associates.rds_automation' } }

for i in range(1, len(sys.argv)):
if sys.argv[i] == 'rds':
subprocess.Popen(['python', 'script2.py', sys.argv[i],
serviceToOdinMap[sys.argv[i]]['odinSetMaterial']])
else:
print("Wrong input provided.")


#script 2
import boto3
import socket
import os
import sys
#import python connector to MWS 
from bender.mws import MWS
SERVICE_NAME = sys.argv[1]
ODIN_SET = sys.argv[2]
regions = ['us-east-2']
MARKER = 'Marker'
dataSet = 'Prod'
serviceName = 'test' + SERVICE_NAME.upper()
metric = 'NumberOfPublic' + SERVICE_NAME.upper() + 'Snapshots'
methodName = 'ALL'

def main() :
generate_data()

def get_response(boto3_client, marker_for_next_page_retrieval) :
public_db_snapshots = {}
if(SERVICE_NAME == 'rds'):
if(marker_for_next_page_retrieval == None) :
public_db_snapshots = boto3_client.describe_db_snapshots(SnapshotType = 'public')
else :
public_db_snapshots = boto3_client.describe_db_snapshots(Marker = marker_for_next_page_retrieval, SnapshotType = 'public') 
return public_db_snapshots

def generate_data() :
for region in regions :
success, number_of_snapshots = get_number_of_snapshots(region)
if success :
upload_metrics(metric  + 'In' + region.upper(), number_of_snapshots, region)

def upload_metrics(metricName, numericVal, region):
mws = MWS(odin_set = ODIN_SET)
marketplace = region[:2]
response = mws.put(DataSet = dataSet,
ServiceName = serviceName,
Metric = metricName,
MethodName = methodName,
Marketplace = marketplace,
NumericValue = numericVal)


def get_number_of_snapshots(region) :
boto3_client = boto3.client(SERVICE_NAME, region)
number_of_snapshots = 0
marker_for_next_page_retrieval = None
success = False

while(success == False) :
public_db_snapshots = {}
public_db_snapshots = get_response(boto3_client, marker_for_next_page_retrieval)
number_of_snapshots += len(public_db_snapshots['DBSnapshots'])
if MARKER in public_db_snapshots :
marker_for_next_page_retrieval = public_db_snapshots[MARKER]
else :
marker_for_next_page_retrieval = None
if marker_for_next_page_retrieval == None :
# If no 'Marker' in response we have scanned every database snapshots.
success = True
return success, number_of_snapshots

main()

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating