Skip to content

Instantly share code, notes, and snippets.

@stekern
stekern / .zshrc
Created July 7, 2024 09:53
no-my-zsh
# ====== Options ======
setopt NO_CASE_GLOB
setopt SHARE_HISTORY
setopt EXTENDED_HISTORY
setopt APPEND_HISTORY
setopt HIST_VERIFY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_FIND_NO_DUPS
setopt HIST_REDUCE_BLANKS
@stekern
stekern / cfn-ddb-sfn.yml
Created November 12, 2023 16:12
CloudFormation template that demonstrates how to start a Step Functions State Machine using DynamoDB Streams and EventBridge Pipes
AWSTemplateFormatVersion: "2010-09-09"
Description: Creates a Step Functions State Machine that is executed when new items are added to a DynamoDB Table.
Resources:
Table:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
- AttributeName: "PK"
AttributeType: "S"
KeySchema:
@stekern
stekern / clear-github-dependency-graph.sh
Created October 11, 2023 14:54
Utility script for creating an empty GitHub dependency graph snapshot
#!/usr/bin/env bash
#
# Utility script for creating an empty GitHub dependency graph snapshot for a given <detector>-<job-correlator> combination.
# This can be useful to update your dependency graph in GitHub if you've used Syft or similar to manually publish
# dependency graph snapshots for artifacts that have been removed from the codebase.
#
# The script asks for confirmation before actually making the POST request to GitHub's Dependency Submission API.
set -euo pipefail
IFS=$'\n\t'
@stekern
stekern / shell-stuff.md
Last active October 6, 2023 13:09
shell stuff

shell stuff

A dumping ground for shell commands and regular expressions that should be POSIX-compliant and portable, as well as some bash stuff that should be portable across bash versions (>= 2).

bash

Storing output from command as array

read -r -d '' -a arr < <( command && printf '\0' )
@stekern
stekern / list-all-cloudformation-stacks.sh
Created April 26, 2023 10:46
List all CloudFormation stacks in all enabled regions
#!/usr/bin/env bash
#
# Script that uses the AWS CLI to list all active CloudFormation stacks
# in all enabled regions.
#
set -euo pipefail
IFS=$'\n\t'
regions="$(aws account list-regions --region-opt-status-contains "ENABLED" "ENABLED_BY_DEFAULT" --query "Regions[].[RegionName]" --output text && printf '\0')"
read -r -d '' -a regions < <(aws account list-regions --region-opt-status-contains "ENABLED" "ENABLED_BY_DEFAULT" --query "Regions[].[RegionName]" --output text && printf '\0')
@stekern
stekern / cfn-monthly-budget.yml
Created April 10, 2023 09:49
CloudFormation template for creating a monthly AWS budget with email notifications.
# NOTE: In order for IAM users and roles to have access to AWS billing and cost management,
# you'll first need to activate IAM access using your AWS root user. You can follow the guide
# at the link below if you haven't already configured this.
# https://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_billing.html?icmpid=docs_iam_console#tutorial-billing-step1
AWSTemplateFormatVersion: 2010-09-09
Description: |
Creates an AWS Budgets Budget that notifies you by email once you're on track to exceed the monthly budget."
Parameters:
Email:
@stekern
stekern / s3-filename-sync.sh
Last active October 9, 2022 20:17
Sync contents of an S3 bucket with a local folder, skipping local files with the same filename
#!/usr/bin/env bash
#
# Experimental shell script for downloading files from S3 and
# skipping local files with the same filenames.
#
# Example usage: bash shell.sh --bucket-name "my-bucket" --target-dir "./my-local-dir" --prefix "my-prefix/" --flatten "true"
set -euo pipefail
IFS=$'\n\t'
@stekern
stekern / replace_placeholders.sh
Created June 23, 2022 13:29
Shell script for replacing placeholders in files in a portable-ish manner
#!/usr/bin/env bash
#
# Helper functions for replacing placeholders in files.
#
# Distributed under terms of the MIT license.
set -euo pipefail
IFS=$'\n\t'
# Print example usage of the script.
@stekern
stekern / locate-cdk-v1-stacks.sh
Last active June 2, 2022 12:21
Locate CloudFormation stacks created using CDK v1
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
read -r -d '' -a regions < <(aws ec2 describe-regions --query "Regions[].[RegionName]" --output text && printf '\0')
for region in "${regions[@]}"; do
echo "[$region] Finding CloudFormation stacks deployed using CDK"
read -r -d '' -a stacks < <(aws cloudformation list-stacks --region "$region" --query "StackSummaries[?!contains(keys(@), 'DeletionTime')].[StackName]" --output text && printf '\0')
test -z "${stacks:-}" && continue
@stekern
stekern / script.sh
Created November 12, 2020 07:50
Shell one-liner for pretty-printing JSON
python -c "import sys; import json; j = json.load(sys.stdin); json.dump(j, sys.stdout, indent=2, sort_keys=True, separators=(',', ': '))"