Skip to content

Instantly share code, notes, and snippets.

View markjschreiber's full-sized avatar

Mark Schreiber markjschreiber

  • Amazon Web Services
  • Massachusetts, USA
View GitHub Profile
@markjschreiber
markjschreiber / ListPrefix.java
Created May 16, 2024 21:52
Perform a recursive depth-first listing of an S3 prefix
/**
* Performs a recursive depth-first listing of an S3 prefix
* @param client the S3 client to use
* @param bucketName the name of the S3 bucket
* @param prefix the prefix to start from
*/
public static void listPrefix(S3AsyncClient client, String bucketName, String prefix) {
client.listObjectsV2Paginator(b -> b.bucket(bucketName)
.prefix(prefix)
.delimiter("/"))
@markjschreiber
markjschreiber / jvm-mem.sh
Created January 4, 2022 02:45
Set JVM Ram as a percentage of container RAM pre java 11
#! /bin/bash
# See https://www.atamanroman.dev/articles/jvm-memory-settings-container-environment/
# set at 75% then print the flag values and grep for heapsize and max ram
java -XX:MaxRAM=$(( $(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) * 75 / 100 )) -XX:MaxRAMFraction=1 -XX:+PrintFlagsFinal -version | grep -Ei "maxheapsize|maxram"
@markjschreiber
markjschreiber / neptune_status.sh
Created February 24, 2021 15:21
Make a signed Curl request to an IAM protected neptune-db to get it's status
#!/bin/bash
awscurl https://<HOST>:8182/status \
--service neptune-db \
--access_key $AWS_ACCESS_KEY_ID \
--secret_key $AWS_SECRET_ACCESS_KEY \
--region us-east-1
@markjschreiber
markjschreiber / temporary_creds.sh
Last active February 24, 2021 15:18
Gets a set of temporary credentials for an EC2 using a named profile that the instance profile of the EC2 is allowed to assume.
#!/usr/local/bin/bash
# Gets a set of temporary credentials for an EC2 using a named profile that the instance profile of the EC2 is allowed
# to assume. Sourcing the script will set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN
# usage: source temporary_creds.sh <service_role_name>
if [ -z "$1" ]; then echo "usage: source creds.sh <service_role_name>"; fi
service_role_name=$1
#! /bin/bash
# This script makes it easier to maintain security groups that allow SSH access
# from a computer with a dynamic IP, such as a computer on a home network or ISP.
#
# Using the script will allow you to SSH to an EC2 without having to allow
# access to the whole world (0.0.0.0/0). If you run this script whenever your IP
# changes then the security groups in your account specified by your AWS profile
# will be updated.
#
aws ecs update-cluster-settings --cluster CLUSTER-NAME --settings name=containerInsights,value=enabled
@markjschreiber
markjschreiber / run_docker_as_non_root.sh
Created May 12, 2020 19:06
How to allow non root user to run docker
# Manage Docker as a non-root user
# Stolen from https://askubuntu.com/a/477554
#
# The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The docker daemon always runs as the root user.
#
# If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
#
# Add the docker group if it doesn't already exist:
sudo groupadd docker
@markjschreiber
markjschreiber / retry-example.sh
Created April 9, 2020 14:29
Bash Retry function
# this example is from https://unix.stackexchange.com/a/137639/322623
function fail {
echo $1 >&2
exit 1
}
function retry {
local n=1
local max=5
@markjschreiber
markjschreiber / trap-demo.sh
Created April 8, 2020 13:43
Trap exit code example
#!/bin/bash
#exit on any line causing an error
set -e
#trap any exit or error and handle with the final() function
trap 'final $? $LINENO' EXIT
final() {
#write the return code
@markjschreiber
markjschreiber / logback.xml
Created March 5, 2020 16:24
basic logback configuration
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>