Skip to content

Instantly share code, notes, and snippets.

View leite08's full-sized avatar

Rafael Leite leite08

View GitHub Profile
@leite08
leite08 / update-aws-roles.md
Created May 3, 2025 12:29
Update roles on AWS Switch Role

AWS provides a way to switch accounts in the same browser using "Switch role": https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-console.html

Once you switch to another role/account, it will stay on that browser's "history" - it uses cookies to store the list of roles you switched to.

If you want to update that list, currently the only way is to update the cookie.

NOTE: you'll need to URL-encode the updated value before updating it in the browser's cookie.

In chrome, similar for other browsers:

@leite08
leite08 / compare-string-replace.ts
Created December 28, 2024 21:56
Compare different approaches to string replace all in JS for performance
/**
* As of 2024-12-28, Mac M1 Max
*
* replace took 592ms
* replaceAll took 641ms
* replace regex took 485ms
* replace NEW regex took 902ms
* replaceAll regex took 691ms
* replaceAll NEW regex took 1105ms
*/
@leite08
leite08 / aws-ecr-calculate-storage.sh
Created July 19, 2024 03:56
Calculate the storage used by an ECR repository
#!/bin/bash
# Based on https://stackoverflow.com/a/78081105/2099911
ECR_REPO=...
REGION=...
total_size=0
echo "Retrieving image details for $ECR_REPO..."
image_details=$(aws ecr describe-images --repository-name $ECR_REPO --region $REGION --no-cli-pager)
echo "Done; calculating the total size..."
for row in $(echo "${image_details}" | jq -c '.imageDetails[]'); do
@leite08
leite08 / sentry-raw.sh
Created June 18, 2024 17:16 — forked from ezy/sentry-raw.sh
CURL request to sentry without an SDK
# Replace <sentry_key>, <sentry-url> and <project-id> to formulate your CURL request. Don't forget to add a trailing slash to URL otherwise you'll get wierd CSRF errors!
curl -X POST --data '{ "exception": [{ "type": "$ErrorMessage"}] }' -H 'Content-Type: application/json' -H "X-Sentry-Auth: Sentry sentry_version=7, sentry_key=<sentry_key>, sentry_client=raven-bash/0.1" https://<sentry-url>/api/<project-id>/store/
@leite08
leite08 / aws-ecr-add-tag.sh
Last active April 16, 2024 03:00
AWS ECR - add tag to an image by tag
# This script adds a tag to an already tag ECR Image
ECR_REPO=...
CURR_TAG=...
NEW_TAG=latest
# Can replace imageTag="$CURR_TAG" by imageDigest="sha256:..." if the image doesn't have a tag
MANIFEST=$(aws ecr batch-get-image --repository-name $ECR_REPO --image-ids imageTag="$CURR_TAG" --output text --query 'images[].imageManifest')
aws ecr put-image --repository-name $ECR_REPO --image-tag $NEW_TAG --image-manifest "$MANIFEST"
@leite08
leite08 / insert-random.sql
Last active March 19, 2024 03:24
Populate Postgres table w/ random data
insert into table (id, another, jsonb_data)
with data as (
select i
from generate_series(1, 1000) as g(i)
)
select
gen_random_uuid()::text,
'<insert-id-here>',
('{"dob": "' || timestamp '1940-01-01 20:00:00' + random() * (timestamp '2020-01-01 10:00:00' - timestamp '1940-01-01 20:00:00') || '", "lastName": "last' || i || '", "firstName": "first' || i || '"}')::json
from data;
@leite08
leite08 / server.ts
Created March 3, 2024 18:26
Express server to log text payloads
import express, { Application, Request, Response } from "express";
const port = 8088;
const app: Application = express();
app.use(express.text({ type: "*/*" }));
app.post("/", (req: Request, res: Response) => {
console.log(`>>> HEADERS: ${JSON.stringify(req.headers, undefined, 2)}`);
console.log(`>>> BODY: ${String(req.body)}`);
@leite08
leite08 / db-conn.ts
Created March 1, 2024 06:10
Typescript code to time creating connections with Postgres
import { Client } from "pg";
type Result = {
connect: number;
close: number;
total: number;
};
let results: Result[] = [];
@leite08
leite08 / load-test.sh
Created March 27, 2023 18:17
Simple shell-based load testing script
#!/bin/bash
# Inspired by https://gist.github.com/bigomega/4de7dbc395be0e0f33b4
# TODO: allow multiple headers
# TODO: call curl with optional parameters so the code is not duplicated (DRY)
############################################################
# Help #
############################################################
Help()
@leite08
leite08 / untilfail.sh
Created March 23, 2023 21:25
Shell script to run a command until it fails
#!/bin/bash
count=0
while "$@"; do (( count++ )); done && echo "It failed on the $count iteration"