Skip to content

Instantly share code, notes, and snippets.

View ottokruse's full-sized avatar

Otto Kruse ottokruse

  • AWS
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / invoke-aws-api-gateway-with-cognito-client-credentials.py
Last active August 11, 2020 11:22
Invoke an AWS API Gateway Endpoint using Cognito Client Credentials
"""
This example does a HTTP POST, change it to whatever you want.
The code here assumes that:
- Your API Gateway has been setup with a Cognito User Pool authorizer
- That User Pool authorizer is set up with http header "authorization" as Token Source
- The API Gateway Method you are invoking is set up to require at least one OAuth scope
"""
from urllib.request import Request, urlopen
@ottokruse
ottokruse / package_lambda.py
Created October 12, 2020 10:44
Package Lambda functions and layers with CDK bundling, use cache to prevent unnecessary docker runs
from typing import List
import os
import shutil
import hashlib
import tempfile
from aws_cdk import core, aws_lambda
import jsii
from pathlib import Path