Useful Snippets
Useful Snippets
AWS CLI
List pending maintenance actions for a given AWS region
This Bash script retrieves all pending RDS maintenance actions for a specific AWS region (defaulting to us-east-1
if not set). It categorizes the maintenance actions (like CA certificate rotations or system updates) using jq for clearer visibility. The JSON output is temporarily saved and then deleted to maintain cleanliness.
#!/bin/bash
AWS_REGION="${AWS_REGION:-us-east-1}"aws rds describe-pending-maintenance-actions \ --region $AWS_REGION \ --no-paginate > /tmp/pending-actions.json
cat /tmp/pending-actions.json | \ jq -r '{ "ca-certificate-rotation": [ .PendingMaintenanceActions[] | select(.PendingMaintenanceActionDetails[].Action == "ca-certificate-rotation") | .ResourceIdentifier ], "db-upgrade": [ .PendingMaintenanceActions[] | select(.PendingMaintenanceActionDetails[].Action == "db-upgrade") | .ResourceIdentifier ], "hardware-maintenance": [ .PendingMaintenanceActions[] | select(.PendingMaintenanceActionDetails[].Action == "hardware-maintenance") | .ResourceIdentifier ], "system-update": [ .PendingMaintenanceActions[] | select(.PendingMaintenanceActionDetails[].Action == "system-update") | .ResourceIdentifier ] }'
rm /tmp/pending-actions.json
List available IP addresses of AWS subnets
This AWS CLI command fetches details of subnets tagged with "Name=some-name"
, displaying only essential fields: available IP address count, subnet ARN, and CIDR block. It’s useful for checking subnet utilization at a glance.
aws ec2 describe-subnets \ --filters "Name=tag:Name,Values=some-name" \ | jq '.Subnets[] | with_entries(select(.key == ("AvailableIpAddressCount","SubnetArn","CidrBlock")))'
GitHub CLI
Install GitHub CLI
This command installs the GitHub CLI (gh
) using Homebrew, enabling you to interact with GitHub directly from the terminal.
brew install gh
Configure GitHub CLI
The first command sets GitHub’s hostname and configures gh
to use SSH for authentication.
The second command logs you into GitHub via the CLI, opening a browser-based authentication flow while skipping the creation of a new SSH key.
gh config set --hostname github.com git_protocol sshgh auth login \ --hostname github.com \ --git-protocol ssh \ --skip-ssh-key \ --web
Get Repository Metadata as JSON
This retrieves details of the specified repository in JSON format, extracting only the updatedAt
field. The output is piped through jq
for pretty-printing and easy parsing.
gh repo view m99coder/cloud-native-enterprise-nodejs-revamped --json updatedAt | jq
Kubernetes
Get all errored pods in a cluster
This kubectl
command lists all pods across namespaces and filters for those with error states. It extracts pod names and their container error reasons, helping operators quickly pinpoint which pods are failing and why.
kubectl get pods -A \ -o custom-columns="POD:metadata.name,STATE:status.containerStatuses[*].state.waiting.reason" \ | grep -v "<none>"
Follow JSON logs and parse them on the fly
By combining kubectl logs
with jq
, this command streams logs from a Kubernetes pod and attempts to parse each line as JSON. This is especially helpful when dealing with structured logs in real time.
kubectl logs pods/my-pod -f \ | jq -R 'fromjson?'
Shell
Run command and split result into batches
This snippet takes a long list of items and uses awk
to insert blank lines every 20 lines, effectively splitting the content into manageable batches. Ideal for paginating long output or for batch processing in scripts.
cat long-list \ | awk -v n=20 '1; NR % n == 0 {print ""}' > long-list-batches
Grep with colors
This command uses grep
with color highlighting to visually emphasize matches for a search term in a file. Piping to less -r
preserves the color formatting while enabling interactive scrolling.
grep --color=always -E "search-term|$" ./some.yaml \ | less -r
Grep with line numbers and X lines after
This grep
command highlights lines matching a pattern and shows three lines of context after each match. It also includes line numbers, making it easier to locate and analyze relevant sections in larger files.
cat ./some.yaml | grep -A 3 -n search-term
Closing Thoughts
Whether you’re managing cloud resources, debugging applications, or manipulating files in the terminal, these small but powerful commands can save you time and reduce repetitive work. Keep them handy as part of your daily toolkit—they’re simple, flexible, and highly adaptable to many real-world scenarios.