Skip to content

Instantly share code, notes, and snippets.

View ottokruse's full-sized avatar

Otto Kruse ottokruse

  • AWS
View GitHub Profile
@ottokruse
ottokruse / lambda-execute-appsync-mutation.js
Last active July 23, 2019 06:17
Example AWS Lambda function that executes AWS AppSync GraphQL Mutation using AWS Amplify
const Amplify = require('aws-amplify');
Amplify.default.configure({
aws_appsync_graphqlEndpoint: 'https://something.appsync-api.eu-west-1.amazonaws.com/graphql',
aws_appsync_region: 'eu-west-1',
aws_appsync_authenticationType: 'API_KEY',
aws_appsync_apiKey: 'yourkey'
});
const mutation = `mutation CreatePerson($firstName: String!, $lastName: String!, bankAccountBalance: Float!) {
@ottokruse
ottokruse / worker.sh
Last active April 18, 2019 09:09
SQS worker written in Bash
#!/bin/bash
log() {
echo $(date -u +"%Y-%m-%dT%H:%M:%SZ") $@ # >> output.log
}
convert_single () {
cd /mnt/
FULL_PATH=$1
time wine /home/ubuntu/bin/Dat2Cvw_1.4.7.1.A/DAT2CVW.exe -i $FULL_PATH >/dev/null 2>&1
@ottokruse
ottokruse / index.ts
Last active August 23, 2019 08:15
NodeJs crypto secure random choice from string or Array
import { randomBytes } from 'crypto';
let bitsNeeded: number, bytesNeeded: number, chunks: number, tooBig: number, randomNumber: number, index: number;
export function randomChoiceFromIndexable(indexable: string | any[]) {
bitsNeeded = Math.log2(indexable.length);
bytesNeeded = Math.ceil(bitsNeeded / 8);
chunks = Math.floor(256 / indexable.length) || 1;
tooBig = indexable.length * chunks;
do {
randomNumber = randomBytes(bytesNeeded).readUIntBE(0, bytesNeeded);
@ottokruse
ottokruse / cfn-response.py
Last active December 13, 2019 10:53
A better cfn-response for Python3, only depends on stdlib
from urllib.request import Request, urlopen
import json
class CfnResponse:
SUCCESS = "SUCCESS"
FAILED = "FAILED"
@staticmethod
def send(
@ottokruse
ottokruse / cdk-package.ts
Last active August 9, 2023 11:06
Script to publish CDK assets (e.g. Lambda function code) to S3 and generate parameter files, so you can combine cdk synth with CloudFormation deployments. This is essentially the equivalent of 'sam package' but then for CDK. Tested to work for Lambda and S3-deployments
#!/usr/bin/env ts-node
// This script uploads your assets to the CDK staging bucket in S3 (just as cdk deploy would)
// and writes out two files:
// - parameters.ini to use in CLI deployments (see instructions below)
// - parameters.json to use in AWS CodePipeline for CloudFormation deployments
//
// Installation instructions:
// - Save this script cdk-package.ts to the root of your CDK repo (i.e. next to cdk.json) and make it executable
// - Install script dependencies: npm install jsonpath aws-sdk adm-zip @types/jsonpath @types/adm-zip
@ottokruse
ottokruse / install-dependencies.sh
Created May 1, 2020 14:57
Install Lambda Layer dependencies (Python 3.8)
#!/bin/sh
LAYER_DIR=lib/lambda-layers
for DIR in $(ls $LAYER_DIR); do
echo "Installing layer: $DIR"
cd $LAYER_DIR/$DIR
git check-ignore * > /dev/null && rm -r $(git check-ignore *)
docker run --rm --init -v "$(pwd):/${DIR}" lambci/lambda:build-python3.8 sh '-c' "pip install -r /${DIR}/requirements.txt --target /${DIR}/python"
done
@ottokruse
ottokruse / cfn-response.ts
Last active June 28, 2020 13:02
A better cfn-response for NodeJS, only depends on stdlib
import { request } from "https";
export enum Status {
"SUCCESS" = "SUCCESS",
"FAILED" = "FAILED",
}
export async function sendCfnResponse(props: {
event: {
StackId: string;
@ottokruse
ottokruse / aws_logger.py
Last active June 19, 2020 14:20
AWS Simple Logger (for when you know logs will be sent to CloudWatch Logs)
"""
Logging utility for logging to CloudWatch Logs, to use in e.g. AWS Lambda.
Some small features included:
- log structured JSON as that works really well with CloudWatch Logs filters and CloudWatch Logs Insights
- support turning any Python object into JSON
- replace line endings for nice folding of entries in CloudWatch Logs
- do not buffer stdout
"""
@ottokruse
ottokruse / aws-console
Last active November 9, 2024 09:35
Python script to launch the AWS console in your webbrowser, using a presigned URL generated from your AWS CLI credentials
#!/usr/bin/env python3
"""
Usage:
- Save this script somewhere on your path (e.g. `vi /usr/local/bin/aws-console && chmod +x /usr/local/bin/aws-console`)
- Make AWS credentials available in one of the usual places where boto3 can find them (~/.aws/credentials, env var, etc.)
- Excute the script: `aws-console --profile myprofile`
- :tada: Your browser opens and you are signed in into the AWS console
"""