Skip to content

Instantly share code, notes, and snippets.

View perpil's full-sized avatar
🚀

perpil

🚀
View GitHub Profile
@perpil
perpil / coldstarter.js
Created August 15, 2025 17:49
How I measure E2E latency of lambdas
import { LambdaClient, InvokeCommand, LogType } from "@aws-sdk/client-lambda";
const client = new LambdaClient({
region: process.env.AWS_REGION,
maxAttempts: 1,
});
const invoke = async (funcName, payload = {}) => {
const command = new InvokeCommand({
FunctionName: funcName,
@perpil
perpil / query.sql
Last active September 6, 2025 16:34
Cloudwatch Logs Insights Query to get coldstart stats by lambda runtime
filter @message like /INIT_START|REPORT|START/ |
parse @message /INIT_START Runtime Version: (?<runtime>\S+)/ |
parse @message /^START RequestId: .* Version: (?<version>\S+)/ |
stats sortslast(runtime) as `lambda runtime version`, sortslast(@initDuration) as iD, sortslast(version) as `software version` by @logStream |
filter ispresent(`software version`) |
stats pct(iD, 50) as p50, pct(iD, 90) as p90, pct(iD, 95) as p95, pct(iD, 99) as p99 by `lambda runtime version`, `software version`
@perpil
perpil / handler.mjs
Last active November 12, 2024 16:54
Get node lambda runtime build date at run time
import { statSync } from "fs";
let runtimeBuildDate;
try {
runtimeBuildDate = statSync("/var/runtime").mtime;
} catch (e) {
console.error("Unable to determine runtime build date", e);
}
export const handler = async (event) => {
const response = {
statusCode: 200,
@perpil
perpil / alias
Created September 25, 2024 01:57
AWS CLI alias for printing account alias and important part of ARN of current credentials
[toplevel]
whoami =
!f() {
echo `als=$(aws iam list-account-aliases --query 'AccountAliases[0]' --output text) && \
echo "\033[0;33m$als\033[0m:";`\
`callerid=$(aws sts get-caller-identity --query 'Arn' --output text) && \
(echo $callerid | cut -d: -f5-6)`
}; f
@perpil
perpil / esbuild-cloudfront.mjs
Last active July 30, 2024 22:16
Minimal esbuild for minifying CloudFront Functions
import * as esbuild from "esbuild";
import * as fs from "fs";
import * as path from "path";
// esbuild plugin to add and remove export from the handler function for cloudfront
let cloudFrontFunctions = {
name: "cloudFrontFunctions",
setup(build) {
const write = build.initialOptions.write;
build.initialOptions.write = false;
@perpil
perpil / serverless+3.38.0.patch
Created March 28, 2024 20:18
Serverless .mjs support patch. Put in patches/ directory. Use with https://www.npmjs.com/package/patch-package
diff --git a/node_modules/serverless/lib/plugins/aws/invoke-local/index.js b/node_modules/serverless/lib/plugins/aws/invoke-local/index.js
index b227110..6d10a53 100644
--- a/node_modules/serverless/lib/plugins/aws/invoke-local/index.js
+++ b/node_modules/serverless/lib/plugins/aws/invoke-local/index.js
@@ -838,12 +838,15 @@ class AwsInvokeLocal {
if (error.code === 'ERR_REQUIRE_ESM') {
return await require('../../../utils/import-esm')(`${modulePath}.js`);
} else if (error.code === 'MODULE_NOT_FOUND') {
- // Attempt to require handler with `.cjs` extension
- pathToHandler = `${pathToHandler}.cjs`;
@perpil
perpil / coldstart-query
Created March 27, 2024 19:40
Cloudwatch Insights Query for Coldstarts
filter @type = 'REPORT' |
stats sum(ispresent(@initDuration))/count()*100 as `% coldstarts`,
sum((@initDuration+@duration)*ispresent(@initDuration))/sum(ispresent(@initDuration)) as `avg(coldstart)`,
sum(@duration*(1-ispresent(@initDuration)))/sum(1-ispresent(@initDuration)) as `avg(noncoldstart)`
@perpil
perpil / function.js
Last active January 21, 2025 23:39
Viewer request code for sigv4 signing in a cloudfront function to furl. This code calculates the signature, but it doesn work because the Authorization header is stripped by cloudfront. Use CloudFront Lambda OAC instead.
const crypto = require('crypto');
const querystring = require('querystring');
function hmac(key, string, encoding) {
return crypto
.createHmac('sha256', key)
.update(string, 'utf8')
.digest(encoding);
}
@aws-sdk/util-utf8-browser/dist-es/pureJs.js
@aws-sdk/util-utf8-browser/dist-es/whatwgEncodingApi.js
@aws-sdk/util-utf8-browser/dist-es/index.js
@aws-crypto/crc32/build/index.js
@smithy/util-hex-encoding/dist-es/index.js
@smithy/eventstream-codec/dist-es/Int64.js
@smithy/eventstream-codec/dist-es/HeaderMarshaller.js
@smithy/eventstream-codec/dist-es/splitMessage.js
@smithy/eventstream-codec/dist-es/EventStreamCodec.js
@smithy/eventstream-codec/dist-es/MessageDecoderStream.js
@perpil
perpil / markdownAction.yml
Created January 21, 2024 14:59
Sample of how to emit markdown for speedrun from a GitHub action
name: Print Markdown
on:
workflow_dispatch:
jobs:
print-markdown:
runs-on: ubuntu-latest
steps:
- name: Print Markdown