Skip to content

Instantly share code, notes, and snippets.

View huevos-y-bacon's full-sized avatar

Huevos y Bacon huevos-y-bacon

View GitHub Profile
#!/usr/bin/env bash
# shellcheck disable=all
### Mount the instance store volume to the instance
# see:
# https://stackoverflow.com/questions/45167717/mounting-a-nvme-disk-on-aws-ec2
# https://gist.github.com/ktheory/3c3616fca42a3716346b
# install hdparm and htop
@huevos-y-bacon
huevos-y-bacon / stop_instances.py
Created March 8, 2024 13:19
Stop all running EC2 and (optionally) RDS Instances
#!/usr/bin/env python3
import boto3
ec2 = boto3.resource('ec2')
rds = boto3.client('rds')
include_rds = False
def lambda_handler(event, context):
# Stop EC2 instances

Create Athena DB and table and run queries on AWS Classic Load Balancer logs.

@huevos-y-bacon
huevos-y-bacon / aws_ec2_termination_protection.md
Last active February 20, 2024 07:56
Enable or disable EC2 instance "Termination Protection" via AWS CLI (shell)

Loop through all EC2 instances (excluding terminated and spot) and enable termination protection

for I in $(aws ec2 describe-instances --query \
  'Reservations[].Instances[?(InstanceLifecycle!=`spot` && InstanceState!=terminated)].[InstanceId]' \
  --output text); do
  aws ec2 modify-instance-attribute --disable-api-termination --instance-id $I;
done
@huevos-y-bacon
huevos-y-bacon / steampipe_commands.sh
Last active January 19, 2024 15:43
Steampipe AWS quick setup command (linux)
#!/usr/bin/env bash
# shellcheck disable=all
exit # don't run this, cat it instead
# git clone https://gist.github.com/38b154892c42ce7dde2f42c1eaf65706.git
curl -fsSL https://gist.githubusercontent.com/huevos-y-bacon/38b154892c42ce7dde2f42c1eaf65706/raw/c315a7d244f40a85c6f0d10cd78782b83f74e739/steampipe_commands.sh
# INSTALL STEAMPIPE AND PLUGINS, configure for eu-west-1
sudo /bin/sh -c "$(curl -fsSL https://raw.githubusercontent.com/turbot/steampipe/main/install.sh)"
@huevos-y-bacon
huevos-y-bacon / tsv_to_csv.sh
Created January 18, 2024 10:45
Simple script to convert tab separated files to CSV, e.g. DNS Zone files
#!/usr/bin/env bash
# Simple script to convert tab separated files to CSV
# E.g. DNS Zone files
if [ "$1" != "" ]; then
# File name might contain spaces
IFS=$'\n'
ZONE_FILES=("$1")
else ZONE_FILES=(
@huevos-y-bacon
huevos-y-bacon / aws_get_session_token.sh
Last active September 27, 2023 18:21
GET AWS SESSION TOKEN WITH MFA allowing CLI access to commands without switching to an IAM Role
#!/usr/bin/env bash
# shellcheck disable=2086,2162,2005,2046,1091
# GET AWS SESSION TOKEN WITH MFA
# allowing CLI access to commands without switching to an IAM Role, e.g. for accessing the Well-Architected Tool
source colours > /dev/null 2>&1 # import colours script if it exists in the PATH
usage(){
echo "
@huevos-y-bacon
huevos-y-bacon / aws_org_tree.sh
Last active August 25, 2023 15:52
AWS Organizations - Get Org Tree
#!/bin/bash
# shellcheck disable=SC2086
# This is the same as "set -eux" but also exits on pipefail.
# Also see https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425?permalink_comment_id=3935570#set--e--u--x--o-pipefail
set -euo pipefail
ROOT=$(aws organizations list-roots | jq -r '.Roots[0].Id')
# Function to list accounts and organizational units at a given depth
@huevos-y-bacon
huevos-y-bacon / tf_local_cache_prep.sh
Created August 21, 2023 09:32
Prepare local provider cache for Terraform
#!/usr/bin/env bash
[[ ! "$(uname -s)" == "Darwin" ]] && NOCOLOR=1 # Disable color output if not macOS
mkdir -p "${HOME}"/.terraform.d/plugin-cache # Create local plugin cache folder if not exists
ecco(){
if [[ -n "${NOCOLOR}" ]]; then echo -e "${*}"
else
echo -e "\033[0;1;3;32m${*}\033[0m" # echo green bold and italics
fi
}
@huevos-y-bacon
huevos-y-bacon / cw_ct_log_insights_query.sql
Created August 4, 2023 10:29
CloudWatch Log Insights Query - CloudTrail
# CloudTrail events: By Service / Role / principalId / etc
fields
eventTime
,eventName
,eventSource
,errorCode
,errorMessage
,userIdentity.principalId as principalId
,userIdentity.sessionContext.sessionIssuer.userName as SESSIONUSERID
,@message