Skip to content

Instantly share code, notes, and snippets.

View thomasmichaelwallace's full-sized avatar
🐧

thomas michael wallace thomasmichaelwallace

🐧
View GitHub Profile
@thomasmichaelwallace
thomasmichaelwallace / lambda_function.py
Last active September 8, 2022 12:55
Lambda function to launch an ec2-instance
""" Lambda to launch ec2-instances """
import boto3
REGION = 'eu-central-1' # region to launch instance.
AMI = 'ami-24bd1b4b'
# matching region/setup amazon linux ami, as per:
# https://aws.amazon.com/amazon-linux-ami/
INSTANCE_TYPE = 'm3.medium' # instance type to launch.
EC2 = boto3.client('ec2', region_name=REGION)
@thomasmichaelwallace
thomasmichaelwallace / fixJIRAMenu.css
Created August 7, 2017 20:27
A fun fix for JIRA's default filters cluttering up submenus.
@-moz-document regexp('https://.*\\.atlassian\\.net/secure/.*') {
#filter_lnk_all {
display: none;
}
#filter_lnk_my {
display: none;
}
@thomasmichaelwallace
thomasmichaelwallace / asyncSingleton.js
Last active December 4, 2018 10:02
How to make a singleton promise.
// a connection singleton.
let dbConnectionPromise;
function getDb() {
if (dbConnectionPromise) {
return dbConnectionPromise;
}
dbConnectionPromise = DbClient.connect(options);
return dbConnectionPromise;
}
@thomasmichaelwallace
thomasmichaelwallace / unique.js
Created August 1, 2017 16:20
Getting unique values using sets in ES6.
// with arrays
const dupArr = [1, 1, 2, 3, 1];
const uniArr = [...(new Set(dupArr))];
// [1, 2, 3]
// with objects on a key.
const dupObj = [{ id: 1, value: 'a' }, { id: 2, value: 'b' }, { id: 1, value: 'c' }];
const uniKeys = [...(new Set(dupObj.map(({ id }) => id)))];
// [ '1', '2' ]
@thomasmichaelwallace
thomasmichaelwallace / doubleDotPerf.js
Created July 28, 2017 17:15
A quick and dirty look at the overhead of calling a double-dot method.
const testCount = 1e7;
const keyCount = 10;
const times = count => Array.from(Array(count))
const message = (test, runtime) => `${test} took ${((runtime[0] * 1e9) + runtime[1]) / 1e6}ms`;
const obj = times(keyCount)
.map(Math.random)
.reduce((o, p) => Object.assign({}, o, { [p.toString()]: Math.random() }), {});
const hasKey = Object.keys(obj)[0];
@thomasmichaelwallace
thomasmichaelwallace / performanceTest.js
Last active July 27, 2017 18:09
A simple snippet for a time based test.
describe('My awesome new way of doing things', () => {
it('should be performant.' () => {
const maxRunTimeMs = 500;
const start = process.hrtime();
return doSomethingAwesome()
.then(() => {
const runtime = process.hrtime(start);
const runtimeNs = (runtime[0] * 1e9) + runtime[1];
const runtimeMs = runtimeNs / 1e6;
expect(runtimeMs).to.be.lessThan(maxRunTimeMs);
@thomasmichaelwallace
thomasmichaelwallace / nodelogs_fix.config
Created July 26, 2017 16:26
An ebextension to fix node log streaming after rotation.
files:
"/etc/cron.hourly/cron.logrotate.elasticbeanstalk.nodejs.conf":
mode: "000655"
owner: root
group: root
content: |
#!/bin/sh
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.elasticbeanstalk.hourly/logrotate.elasticbeanstalk.nodejs.conf
/sbin/service awslogs restart
@thomasmichaelwallace
thomasmichaelwallace / amazing-authd-lamda.js
Created July 20, 2017 16:39
An amazing example of the fun things you can do with an authorized context.
const amazing = (event, context, callback) => {
const { requestContext: { authorizer } } = event;
const body = {
hello: 'world',
authContext: authorizer,
};
const response = {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
@thomasmichaelwallace
thomasmichaelwallace / simple-custom-authoriser.js
Created July 20, 2017 16:25
A very simple custom authoriser.
const yourAuthorizationLogic = require('./yourAuthorizationLogic');
const generateIamPolicy = (principalId, Effect, Resource, context) => ({
principalId,
policyDocument: {
Version: '2012-10-17',
Statement: [{ Action: 'execute-api:Invoke', Effect, Resource }],
},
context,
});
@thomasmichaelwallace
thomasmichaelwallace / eb-memory-monitor.config
Last active September 26, 2019 10:56
EB CloudWatch memory usage reporting .ebextensions file
container_commands:
00download:
command: "wget http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip"
ignoreErrors: true
01extract:
command: "unzip -o CloudWatchMonitoringScripts-1.2.1.zip"
ignoreErrors: true
02rmzip:
command: "rm CloudWatchMonitoringScripts-1.2.1.zip"
ignoreErrors: true