Skip to content

Instantly share code, notes, and snippets.

@brevityinmotion
brevityinmotion / brevity-aws-dashboard.py
Created March 22, 2022 04:29
Generate IPinfo html dashboard
import pandas as pd
import boto3
import requests
import io
from requests.structures import CaseInsensitiveDict
from boto3.dynamodb.conditions import Key
def lambda_handler(event, context):
def _getParameters(paramName):
@brevityinmotion
brevityinmotion / brevity-aws-ipinfo.py
Last active March 22, 2022 04:20
Process, enrich, and store Route 53 DNS query logs, enrich with IPInfo, and store in DynamoDB table.
import json, boto3, os, re
import gzip
import base64
import ipinfo
from io import BytesIO
from botocore.exceptions import ClientError
def lambda_handler(event, context):
dynamodbclient = boto3.client('dynamodb')
@brevityinmotion
brevityinmotion / brevity-aws-services-list.py
Created October 19, 2021 14:13
Python script to enumerate the short codes and long names for AWS services
# Description: Enumerates short codes and long names for AWS services
# Code modified from https://www.sentiatechblog.com/retrieving-all-region-codes-and-names-with-boto3
class Services:
@classmethod
def get_services(cls):
short_codes = cls._get_service_short_codes()
services = [{
'name': cls._get_service_long_name(sc),
'code': sc
@brevityinmotion
brevityinmotion / axiom-ec2-install.py
Created August 18, 2021 11:56
Automated install for Axiom in AWS EC2
import io, json
import brevitycore.core
def generateInstallScriptAxiom(inputBucketName):
# Load AWS access keys for s3 synchronization
secretName = 'brevity-aws-recon'
regionName = 'us-east-1'
secretRetrieved = brevitycore.core.get_secret(secretName,regionName)
secretjson = json.loads(secretRetrieved)
awsAccessKeyId = secretjson['AWS_ACCESS_KEY_ID']
@brevityinmotion
brevityinmotion / brevity-parameterstore-load.py
Created August 3, 2021 05:27
Example function to load static variables into AWS parameter store
def generateParameterStore(dataBucketName,graphBucketName,inputBucketName,presentationBucketName,ATHENA_BUCKET,ATHENA_DB,ATHENA_TABLE):
graphBucketPath = 's3://' + dataBucketName + '/graph/'
rawBucketPath = 's3://' + dataBucketName + '/raw/'
refinedBucketPath = 's3://' + dataBucketName + '/refined/'
inputBucketPath = 's3://' + inputBucketName + '/'
programInputBucketPath = 's3://' + inputBucketName + '/programs/'
presentationBucketPath = 's3://' + presentationBucketName + '/presentation/'
variableStatus = 'Variables successfully assigned.'
@brevityinmotion
brevityinmotion / brevity-glue-startcrawler.py
Created August 3, 2021 04:30
Lambda example to start a Glue crawler to index new data in preparation for analysis.
import json, boto3, os
def lambda_handler(event, context):
if event['program'] is None:
return {"isBase64Encoded":False,"statusCode":400,"body":json.dumps({"error":"Missing program name."})}
if event['operation'] is None:
return {"isBase64Encoded":False,"statusCode":400,"body":json.dumps({"error":"Missing operation name."})}
else:
operationName = str(event['operation'])
@brevityinmotion
brevityinmotion / brevity-networkx-example.py
Last active August 3, 2021 04:13
Example NetworkX with Bokeh plot displaying a single program
# NetworkX with Bokeh example
import networkx as nx
import pandas as pd
from bokeh.io import output_notebook, show, save
from bokeh.models import Range1d, Circle, ColumnDataSource, MultiLine
from bokeh.plotting import figure
from bokeh.plotting import from_networkx
# https://docs.bokeh.org/en/latest/docs/reference/palettes.html
@brevityinmotion
brevityinmotion / brevity-process-httpx.py
Created August 3, 2021 02:48
Function to add program and baseurl fields to raw httpx json output
def processHttpx(programName, refinedBucketPath, inputBucketPath, presentationBucketPath, operationName, programInputBucketPath):
fileName = programName + '-httpx-' + operationName + '.json'
presentationFilePath = presentationBucketPath + 'httpx-json/' + fileName
df = pd.read_json(presentationFilePath, lines=True)
df['program'] = programName
if (operationName == 'initial'):
storePathUrl = programInputBucketPath + programName + '/' + programName + '-httpx.csv'
df.to_csv(storePathUrl, header=False, index=False, sep='\n')
@brevityinmotion
brevityinmotion / brevity-parameters-example.py
Created July 29, 2021 03:08
Retrieves values from AWS Parameter Store
def _getParameters(paramName):
client = boto3.client('ssm')
response = client.get_parameter(
Name=paramName
)
return response['Parameter']['Value']
rawBucketPath = _getParameters('rawBucketPath')
refinedBucketPath = _getParameters('refinedBucketPath')
inputBucketPath = _getParameters('inputBucketPath')
@brevityinmotion
brevityinmotion / brevity-secrets-code.py
Created July 29, 2021 02:52
Code samples for utilizing AWS Secrets Manager
import boto3
import logging
import ClientError
import json
from botocore.exceptions import ClientError
# update a secret value within AWS Secrets Manager
def put_secret(secretName,secretValue,regionName):
# Create a Secrets Manager client
session = boto3.session.Session()