Skip to content

Instantly share code, notes, and snippets.

package main
import (
"os"
"os/exec"
"strings"
)
func main() {
os.Chdir("/tmp") // Change working directory to /tmp so we can write files
@mhart
mhart / test.sh
Last active February 13, 2020 18:31
AWS internal error creating a 200MiB Lambda Layer
# Try to create a 200MiB layer
head -c 209715200 /dev/random > file.dat
rm -f layer.zip && zip layer.zip file.dat
aws s3 cp layer.zip s3://my-bucket/my-key
aws lambda publish-layer-version --cli-connect-timeout 0 --layer-name large-200MiB-dep --content S3Bucket=my-bucket,S3Key=my-key
# Results in:
# An error occurred (ServiceException) when calling the PublishLayerVersion operation (reached max retries: 2): An error occurred and the request cannot be processed.
# One byte less and it works (`aws lambda publish-layer-version` takes about 60 secs)
@mhart
mhart / plistToOpml.js
Last active October 13, 2019 02:18
Quick hack to convert from NetNewsWire 3.x to something you can import into 5.x
// First, create a new directory, copy this file into it, and run: `npm install xml2js`
// Then `plutil -convert xml1 ~/Library/Application\ Support/NetNewsWire/Subscriptions.plist`
// Then `node plistToOpml.js > exported.opml`
// Then File->Import Subscriptions... in NetNewsWire 5.x
const fs = require('fs')
const xml2js = require('xml2js')
const data = fs.readFileSync(`${process.env.HOME}/Library/Application Support/NetNewsWire/Subscriptions.plist`, 'utf8')
@mhart
mhart / ci.yml
Last active May 23, 2022 04:36
GitHub Actions running 5 tslint jobs in parallel (each tests every 5th file)
name: CI
on: [push]
jobs:
tslint:
runs-on: ubuntu-latest
strategy:
matrix:
job: [0, 1, 2, 3, 4]
@mhart
mhart / npm-ci.sh
Created September 28, 2019 18:26
Simple module caching script, for CI or similar
#!/bin/bash -ex
# Tries to download latest cached node_modules based on package-lock.json
# If it can't, then `npm ci` and push up node_modules to cache
# Assumes S3_BUCKET env var has been set, and that `aws` credentials
# are configured (either in env, or ~/.aws/credentials)
CHECKSUM=$(sha256sum package-lock.json | awk '{print $1}')
@mhart
mhart / canvas-lambda.md
Last active August 26, 2019 20:52
Installing node-canvas for Lambda using docker-lambda

Installing node-canvas for Lambda using docker-lambda

Assumes your Lambda handler (eg index.js, app.js, etc) is in the current directory and you've already run npm install locally (ie, you have ./node_modules already) – the OS you ran it on shouldn't matter.

nodejs8.10 and nodejs10.x

This will reinstall any native module binaries, with our current directory mounted as /var/task, which mirrors where it mounts in Lambda

@mhart
mhart / index.js
Created July 24, 2019 21:04
Log errors to Sentry from a Cloudflare Worker
// Get the key from the "DSN" at: https://sentry.io/settings/<org>/projects/<project>/keys/
// The "DSN" will be in the form: https://<SENTRY_KEY>@sentry.io/<SENTRY_PROJECT_ID>
// eg, https://[email protected]/123456
const SENTRY_PROJECT_ID = '123456'
const SENTRY_KEY = '0000aaaa1111bbbb2222cccc3333dddd'
// Useful if you have multiple apps within a project – not necessary, only used in TAGS and SERVER_NAME below
const APP = 'my-app'
// https://docs.sentry.io/error-reporting/configuration/?platform=javascript#environment
@mhart
mhart / removed.bin.txt
Last active March 6, 2020 09:49
Binaries and libraries removed from the Lambda 2019-05-14 update (refers to all runtimes except nodejs10.x which is opt-in to a completely new OS)
/bin/dash
/bin/dnsdomainname
/bin/domainname
/bin/dumpkeys
/bin/hostname
/bin/ipcalc
/bin/iptables-xml
/bin/kbd_mode
/bin/loadkeys
/bin/mountpoint
@mhart
mhart / bootstrap
Created December 4, 2018 02:15
Out-of-the-box AWS Lambda custom runtime (implemented in bash!)
#!/bin/sh
set -euo pipefail
# Handler format: <script_name>.<function_name>
# The script file <script_name>.sh must be located in
# the same directory as the bootstrap executable.
source $(dirname "$0")/"$(echo $_HANDLER | cut -d. -f1).sh"
while true
@mhart
mhart / steps.sh
Last active September 11, 2023 11:01
Get SCL (and GCC 7) working on Amazon Linux 2017.03
yum install -y iso-codes # Needed for scl-utils-build
curl -O http://vault.centos.org/6.5/SCL/x86_64/scl-utils/scl-utils-20120927-11.el6.centos.alt.x86_64.rpm
curl -O http://vault.centos.org/6.5/SCL/x86_64/scl-utils/scl-utils-build-20120927-11.el6.centos.alt.x86_64.rpm
curl -O http://mirror.centos.org/centos/6/extras/x86_64/Packages/centos-release-scl-rh-2-3.el6.centos.noarch.rpm
curl -O http://mirror.centos.org/centos/6/extras/x86_64/Packages/centos-release-scl-7-3.el6.centos.noarch.rpm
rpm -Uvh *.rpm # Had to run this twice? Get an error first time, maybe Docker related
rm *.rpm