Skip to content

Instantly share code, notes, and snippets.

@mijdavis2
mijdavis2 / aws_security_group_rule-missing_sg_id-output.txt
Last active February 5, 2020 03:24
Result of malformed aws_security_group_rule resource; TF v0.12.6; provider.aws v2.47.0
#
# For security purposes, the output has been obfuscated with '***' in a few places.
#
Error: Expected to find one security group with ID "", got: []*ec2.SecurityGroup{{
Description: "SG for access to *** layer",
GroupId: "sg-*****",
GroupName: "*****",
IpPermissions: [
{
@mijdavis2
mijdavis2 / hop-with-ssh-keys.sh
Created November 7, 2019 17:24
Bring ssh keys along through jump boxes without copying private keys onto nodes.
eval $( ssh-agent )
# Can add as many keys as you want
ssh-add /path/to/key
ssh -A user@host
@mijdavis2
mijdavis2 / dockercleanup.sh
Created September 11, 2019 15:58
Docker cleanup
#!/bin/bash
#
# To add this as a fish function:
# function dockercleanup
# docker volume rm (docker volume ls -f dangling=true -q); docker system prune -a
# end
# funcsave dockercleanup
#
@mijdavis2
mijdavis2 / upload-cert-to-aws-iam.fish
Created July 19, 2019 05:09
Upload SSL cert to AWS IAM (useful for LB incompatible certs like 4096)
#!/usr/bin/fish
# Name allows for "."
# so might as well use full domain minus the "*"
# if you are using a wildcard cert...
#
# But you should at least use one subdomain if you wildcard
# ...for security... just sayin.
# Using absolute path is more reliable.
@mijdavis2
mijdavis2 / get-instance-names.sh
Last active October 29, 2020 21:10
Get "Name" tag for all AWS EC2 instances in a region
#!/bin/bash
PROFILE=${AWS_PROFILE:-default}
TAG=${TAG:-Name}
STATE=${STATE:-running}
aws ec2 --profile ${PROFILE} describe-instances \
--filters Name=instance-state-name,Values=${STATE} | \
jq .Reservations[].Instances[].Tags | \
jq -c ".[] | select(.Key | contains(\"${TAG}\"))" | \
@mijdavis2
mijdavis2 / available_azs.tf
Created March 12, 2019 15:06
How to get available AZs in terraform from data and subnet list.
data "aws_availability_zones" "available" {}
# Assuming we vars for list of private_subnet_ids and or public_subnet_ids
# we take a slice of available AZs.
#
# Add or remove private/public vars when necessary.
locals {
available_azs = "${
slice(
data.aws_availability_zones.available.names,
@mijdavis2
mijdavis2 / threadwrap.py
Created February 25, 2019 14:10
Python multithreading made easy
"""
Note: Found somewhere on Stack Overflow but was unable to find again.
If found, please comment and I will update this gist with the source.
"""
def threadwrap(func, args, kwargs):
class res(object):
result = None
def inner(*args, **kwargs):
res.result = func(*args, **kwargs)
@mijdavis2
mijdavis2 / print_table.py
Last active February 17, 2019 14:57
Print a table from a list of dictionaries
def print_table(data, column_order=None):
""" Pretty print a list of dictionaries (data) as a dynamically sized table.
If column names (column_order) aren't specified, they will show in random order.
Author: Thierry Husson - Use it as you want but don't blame me.
Source: https://stackoverflow.com/questions/17330139/python-printing-a-dictionary-as-a-horizontal-table-with-headers
PEP8ed by: mijdavis2
"""
if not column_order:
column_order = list(data[0].keys() if data else [])
l = [column_order] # 1st row = header
@mijdavis2
mijdavis2 / timeit.py
Last active July 12, 2019 00:12
Decorator for timing functions in python
import time
def timeit(method):
"""
Decorator useful for timing functions.
Source: https://medium.com/pythonhive/python-decorator-to-measure-the-execution-time-of-methods-fa04cb6bb36d
Usage:
@timeit
def my_function():
# Gets latest aws linux ami published by amazon
# Example use with an optional var:
# ${upper(var.ami) == "NONE" ? data.aws_ami.default.id : var.ami}
data "aws_ami" "default" {
most_recent = true
filter {
name = "name"
values = ["amzn-ami-*-x86_64-gp2"]
}
filter {