Skip to content

Instantly share code, notes, and snippets.

Avatar
Make Stuff Works

Bao Nguyen nqbao

Make Stuff Works
View GitHub Profile
@nqbao
nqbao / lambda_s3_event.py
Created Feb 13, 2020
Snippet to handle s3 events
View lambda_s3_event.py
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 Feb 6, 2020
Snippet for integration slack with lambda
View lambda_slack.py
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()
View format_ebs.py
#!/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
View migrate_redis.py
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)
@nqbao
nqbao / ssm_parameter_store.py
Last active Jan 10, 2022
Python class to provide a dictionary-like interface to access AWS SSM Parameter Store easily
View ssm_parameter_store.py
# 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 / install.sh
Last active Feb 15, 2018
Install common prometheus exporter to a node
View install.sh
#!/bin/bash
if [ ! -f "/etc/systemd/system/node_exporter.service" ]; then
wget https://github.com/prometheus/node_exporter/releases/download/v0.15.2/node_exporter-0.15.2.linux-amd64.tar.gz
tar -xvf node_exporter*
sudo mv node_exporter-0.15.2.linux-amd64/node_exporter /usr/sbin
sudo bash -c 'cat <<EOT > /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
View gist:0726c771c6021a08daa759a4a66d27fa
----------------------------------------
Exception happened during processing of request from ('10.26.0.173', 63486)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 290, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 318, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
@nqbao
nqbao / delete_ami.py
Created Dec 28, 2017
A small python script to wipe out all AMI. Be extremely careful when you run this.
View delete_ami.py
import boto3
ec2 = boto3.client('ec2')
images = ec2.describe_images(Owners=['self'])
for image in images['Images']:
if image['State'] == 'available':
snapshots = image['BlockDeviceMappings']
@nqbao
nqbao / copy-snapshot.sh
Created Nov 8, 2017
Copy AMI across accounts
View copy-snapshot.sh
#!/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 / gpu_allocation.py
Created Aug 27, 2017
gpu_allocation.py
View 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()