Skip to content

Instantly share code, notes, and snippets.

@jayers99
jayers99 / getFlowLogs.sh
Created March 18, 2019 18:46
Tail aws vpc flow logs
#!/bin/bash
export AWS_PROFILE=default
FULLPATH="s3://jayers99-flowlog-test/AWSLogs/441655563487/vpcflowlogs/"
BUCKET=$(echo "$FULLPATH" | cut -d "/" -f3)
echo "#$BUCKET#";
echo ;
LASTFILES=$(aws s3 ls $FULLPATH --recursive | sort | tail -n 10 | awk '{print $4}')
@jayers99
jayers99 / cloudtrailFun.sh
Last active October 9, 2019 21:41
AWS Cloudtrail AWS Cli with jq fun
aws cloudtrail lookup-events --profile dev --max-results 10 \
--lookup-attributes AttributeKey=EventName,AttributeValue=GetTrailStatus --query 'Events[*].CloudTrailEvent' | jq -r . | jq .
# events by some user
aws cloudtrail lookup-events --profile dev --max-results 50 \
--lookup-attributes AttributeKey=Username,AttributeValue=someUserName --query 'Events[*].CloudTrailEvent' | jq -r . | jq .eventName
# write events
aws cloudtrail lookup-events --profile dev --max-results 100 \
--lookup-attributes AttributeKey=ReadOnly,AttributeValue=false --query 'Events[*].CloudTrailEvent' | jq -r .
@jayers99
jayers99 / tail_s3_log.sh
Last active March 9, 2019 01:09
tail logs from s3 bucket
#!/bin/bash
export AWS_PROFILE=dev
FULLPATH="s3://somebucket/logs/AWSLogs/1234567890/elasticloadbalancing/us-west-2/2019/03/"
BUCKET=$(echo "$FULLPATH" | cut -d "/" -f3)
echo "#$BUCKET#";
echo ;
LASTFILES=$(aws s3 ls $FULLPATH --recursive | sort | tail -n 10 | awk '{print $4}')
@jayers99
jayers99 / tfmultiplan.sh
Last active March 5, 2019 00:54
terraform plan multiple directories
#!/bin/bash
# get the root repo directory
repo_dir=$(git rev-parse --show-toplevel)
# creast a list of directories to run the plan against
dirs=$(ls -d $repo_dir/network/vpc*)
# set the report file name
mkdir -p $HOME/reports/
@jayers99
jayers99 / tfplan.sh
Last active March 21, 2019 16:25
good format for terraform plan
#!/usr/bin/env bash
planfile=$HOME/plans/${PWD##*/}_$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 8)
planfilepr=$planfile'PR.md'
repodir=$(git rev-parse --show-prefix)
# set the proxy if not
if [[ -z "${http_proxy}" ]]; then setproxy; fi
if [[ ! -d .terraform ]]; then terraform init; fi
@jayers99
jayers99 / jqTfstateFun.sh
Last active February 22, 2019 18:02
jq terraform state fun
cat statefile.tfstate | jq '.. | objects | with_entries(select(.key | contains("aws_network_acl_rule"))) | select(. != {})'
# all the acl objects
jq '.modules[].resources[].primary | select(.id | startswith("acl-"))' statefile.tfstate
@jayers99
jayers99 / findDuplicatePolicies.sh
Last active February 14, 2019 05:22
scan terraform files to find duplicate policy names
grep -ir --no-filename --include \*.tf --exclude-dir=.terraform --exclude-dir=.git -P 'name\s+=\s+\"\$\{var\.\w+\}[\w\-]*\-policy"' | sed 's/\s*name\s*=\s*//' | sort | uniq -d
@jayers99
jayers99 / myNetCalc.py
Last active February 13, 2019 20:39
python network calculator fun
# Is this network contained in this other network
import ipaddress
rule = ipaddress.IPv4Network('10.19.252.0/24')
net = ipaddress.IPv4Network('10.19.252.16/29')
rule.overlaps(net)
net = ipaddress.IPv4Network('10.19.253.16/29')
rule.overlaps(net)
@jayers99
jayers99 / tfVarStats.sh
Last active February 9, 2019 18:10
terraform variable statistics
grep -Proh --include \*.tf --exclude-dir=.terraform --exclude-dir=.git '\$\{var\.[\w]+\}' | sort | uniq -c | sort -nr
find ~/tf/ -name *.tf -exec grep -Proh '\$\{var.[\w]+\}' {} \; | sort | uniq -c | sort -nr > ~/temp/varlist
find /home/jayers -exec ls -ald {} \; | awk '{ print $4," ",$NF }' | awk -F \/ '{ print $1,"/",$4,"/",$5,"/",$NF}' | grep -v jayers