Skip to content

Instantly share code, notes, and snippets.

View nqbao's full-sized avatar
Make Stuff Works

Bao Nguyen nqbao

Make Stuff Works
View GitHub Profile
@nqbao
nqbao / ssm_parameter_store.py
Last active March 21, 2024 00:15
Python class to provide a dictionary-like interface to access AWS SSM Parameter Store easily
# Copyright (c) 2018 Bao Nguyen <b@nqbao.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
@nqbao
nqbao / clean.py
Last active April 24, 2023 05:34
my snippets
# remove outliner
upper = df.quantile(0.99)
cols = set(list(df)) - {"ts", "tss"}
df_clean = df
for col in cols:
df_clean = df_clean[(df_clean[col] < upper[col])]
streamlit
openai <= 1
@nqbao
nqbao / gpu_allocation.py
Created August 27, 2017 17:58
gpu_allocation.py
import os
import logging
import gpustat
logger = logging.getLogger(__name__)
def get_free_gpu_indices():
stats = gpustat.GPUStatCollection.new_query()
used_gpus = set()
@nqbao
nqbao / copy-snapshot.sh
Created November 8, 2017 07:15
Copy AMI across accounts
#!/bin/bash
raw=$1
ami_id=$(echo "$raw" | cut -d"," -f1)
image_name=$(echo "$raw" | cut -d"," -f2)
echo "Copying $image_name ($ami_id)"
aws ec2 modify-image-attribute --image-id $ami_id --launch-permission "{\"Add\":[{\"UserId\":\"$TO_ACCOUNT\"}]}" --profile $FROM_PROFILE
aws ec2 copy-image --name "$image_name" --source-image-id $ami_id --source-region $SOURCE_REGION --profile $TO_PROFILE
@nqbao
nqbao / delete_ami.py
Created December 28, 2017 19:27
A small python script to wipe out all AMI. Be extremely careful when you run this.
import boto3
ec2 = boto3.client('ec2')
images = ec2.describe_images(Owners=['self'])
for image in images['Images']:
if image['State'] == 'available':
snapshots = image['BlockDeviceMappings']
#!/usr/bin/env python
# grab the first unformatted disk and format it, then label it and update to fstab
# this must be ran as root
import subprocess
import time
import os
import datetime
import sys
@nqbao
nqbao / lambda_s3_event.py
Created February 13, 2020 21:36
Snippet to handle s3 events
import boto3
import json
import urllib.parse
import uuid
import os
def lambda_handler(event, context):
sfn = boto3.client('stepfunctions')
@nqbao
nqbao / lambda_slack.py
Created February 6, 2020 21:46
Snippet for integration slack with lambda
import json
import os
import urllib3
bot_token = os.environ.get('SLACK_BOT_TOKEN')
def send_slack_message(channel, message, blocks=[], attachments=[]):
http = urllib3.PoolManager()
import sys
import redis
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
source = redis.Redis.from_url(sys.argv[1])
dest = redis.Redis.from_url(sys.argv[2])
print "Migrating %s -> %s" % (source, dest)