Last active
November 26, 2018 20:17
-
-
Save adam-phillipps/d11f57973d6de41d37a90cf5327e2c39 to your computer and use it in GitHub Desktop.
pseudo-code for pushing csv to rds
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
import boto3 | |
import csv | |
import json | |
import os | |
import request | |
def handler(event, context): | |
cerebro_api_url = os.getenv('CEREBRO_API_URL') | |
s3 = boto3.client('s3') | |
bucket = event['Records'][0]['s3']['bucket']['name'] | |
file_name = event['Records'][0]['s3']['object']['key'] | |
action = event['httpMethod'] # post, get, etc | |
raw_file = s3.get_object(Bucket=bucket, Object=file_name) | |
for row_as_array in csv.reader(raw_file, delimiter=','): | |
table = event['pathParts'].split('/')[-1] # get the last piece of the URL | |
table_url = '/'.join(cerebro_api_url, table) # join the base url and the table-name for the URL for the api call | |
new_row = build_record_dictionary(row_as_array, table, action) | |
# This is where the magic happens, you are sending the new record you created to the database via the cerebro api | |
new_request = request.getattr(action.downcase) # returns `request.post`, `request.get`, etc based on the action name | |
new_request(table_url, data=new_row) # invokes the method we just got by using the params we need to send for the api | |
def build_record_dictionary(row_arr): | |
"""Create dictionary out of CSV row with custom columns that depend on the resource type""" | |
if 'EC2 instance' in row_arr[0]: | |
record = { | |
'httpMethod': action, | |
'body': { | |
'tags': row[-1], # set tags to last column in this row | |
'instance_id': row[2] # set resource id to column 3, as in the CSV | |
} | |
} | |
elif 'EC2 Network Interface' in row_arr[0]: | |
record = {...} | |
elif 'VPC Subnet' in row_arr[0]: | |
record = { | |
'tags': row[-1], # set tags to last column in this row | |
'subnet_id': row[2], # set resource id to column 3, as in the CSV | |
... | |
} | |
else record = {} | |
return record |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment