Skip to content

Instantly share code, notes, and snippets.

Avatar

Otto Kruse ottokruse

  • AWS
View GitHub Profile
@ottokruse
ottokruse / aws-console
Last active Jul 31, 2020
Python script to launch the AWS console in your webbrowser, using a presigned URL generated from your AWS CLI credentials
View aws-console
#!/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
"""
@ottokruse
ottokruse / aws_logger.py
Last active Jun 19, 2020
AWS Simple Logger (for when you know logs will be sent to CloudWatch Logs)
View aws_logger.py
"""
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 Jun 28, 2020
A better cfn-response for NodeJS, only depends on stdlib
View cfn-response.ts
import { request } from "https";
export enum Status {
"SUCCESS" = "SUCCESS",
"FAILED" = "FAILED",
}
export async function sendCfnResponse(props: {
event: {
StackId: string;
@ottokruse
ottokruse / install-dependencies.sh
Created May 1, 2020
Install Lambda Layer dependencies (Python 3.8)
View install-dependencies.sh
#!/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 / cdk-package.ts
Last active Aug 4, 2020
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
View cdk-package.ts
#!/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 / cfn-response.py
Last active Dec 13, 2019
A better cfn-response for Python3, only depends on stdlib
View cfn-response.py
from urllib.request import Request, urlopen
import json
class CfnResponse:
SUCCESS = "SUCCESS"
FAILED = "FAILED"
@staticmethod
def send(
@ottokruse
ottokruse / index.ts
Last active Aug 23, 2019
NodeJs crypto secure random choice from string or Array
View index.ts
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 / worker.sh
Last active Apr 18, 2019
SQS worker written in Bash
View worker.sh
#!/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 Jul 23, 2019
Example AWS Lambda function that executes AWS AppSync GraphQL Mutation using AWS Amplify
View lambda-execute-appsync-mutation.js
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!) {
You can’t perform that action at this time.