Skip to content

Instantly share code, notes, and snippets.

View maatthc's full-sized avatar

Alexandre Andrade maatthc

  • MaaT Tech
  • Melbourne
View GitHub Profile
@maatthc
maatthc / aws-mock.ts
Created July 24, 2020 06:45
How to easily mock multiple AWS services using Jest - JavaScript
jest.mock('aws-sdk', () => {
return {
SSM: jest.fn(() => ({
getParameter: jest.fn(() => ({
promise: jest.fn().mockResolvedValue({ Parameter: { Value: 'SECRET-KEY' } }),
})),
})),
SQS: jest.fn(() => ({})),
S3: jest.fn(() => ({})),
SNS: jest.fn(() => ({})),
@maatthc
maatthc / service.spec.js
Last active July 16, 2020 06:13
(JavaScript) Jest test example use of toHaveBeenCalledTimes, objectContaining and arrayContaining in AWS Lambda
describe('Run method', () => {
const event: CloudWatchLogsEvent = {
awslogs: {
data: '',
},
}
it('should confirm shipments where emitted', async () => {
expect.assertions(2)
await service.run(event)
expect(sendBatchJSONMessages).toHaveBeenCalledTimes(1)
@maatthc
maatthc / OrderDynamics.ts
Last active July 15, 2020 06:12
OrderDynamics Helper Class - WIP
import { soap } from "strong-soap"
abstract class OrderDynamicsClient {
protected isAuthenticated: Boolean = false
protected soapClient: any
protected url: any
abstract authenticate(): Promise<void>
createClient(options: any) {
@maatthc
maatthc / build-linux-lambda.sh
Created June 25, 2020 10:44
Builds AWS Lambda packages with native Linux dependencies when using MacOs
#!/bin/sh
DOCKER_IMAGE=node:12.18.0
docker kill lamda-builder 2>/dev/null
docker rm lamda-builder 2>/dev/null
rm -rf build && rm -rf dist
mkdir -p build && mkdir -p ../dist
cp -r index.js package.json yarn.lock src deps build 2>/dev/null || :
@maatthc
maatthc / budget.yml
Created April 2, 2020 05:26
Cloud Formation template for Budget, using tags
AWSTemplateFormatVersion: "2010-09-09"
Description: Budget alerts for various AWS services
Parameters:
Environment:
Type: String
Description: Deployment environment for the budget checks
GlueDevEndpointDPUHours:
Type: Number
Description: Number of Glue dev endpoint DPU hours we budget for in the period
AWSTemplateFormatVersion: "2010-09-09"
Description: CloudFormation for creating Python Glue Jobs and schedule it's execution
Parameters:
ServiceName:
Type: String
Description: Name of the service
ArtifactBucket:
Type: String
Description: A bucket to get artefacts from
@maatthc
maatthc / async_tasks.py
Created February 8, 2020 11:16
Running asynchronous functions (coroutines) with Python 3 - similarly with JavaScript Promise.all
import asyncio
import random
loop = asyncio.get_event_loop()
async def execute_task():
time_out = random.uniform(0.5, 5)
print("Starting {:1.2f}..".format(time_out))
await asyncio.sleep(time_out)
print("Stoping {:1.2f}..".format(time_out))
@maatthc
maatthc / aws-websockets-api-gw-mock.yaml
Last active April 30, 2024 18:13
Basic WebSocket mock in AWS ApiGateway using CloudFormation in AWS
AWSTemplateFormatVersion: '2010-09-09'
Description: |
AWS CloudFormation template for Mock WebSocket API Gateway. When deploying this stack please remember to check the option:
- I acknowledge that AWS CloudFormation might create IAM resources.
This template can help you to solve issues like:
- CloudWatch Logs role ARN must be set in account settings to enable logging
- Execution failed due to configuration error: statusCode should be an integer which defined in request template
- This custom domain name cannot map to WEBSOCKET protocol APIs
- Error during WebSocket handshake: Unexpected response code: 500
@maatthc
maatthc / promise_timeout.js
Last active August 14, 2019 03:54
Javascript Promises Timeout
//
// Javascript Promises Timeout
//
// A way to implement timeouts in jS promises using :
// - setTimeout():
// sets up a function to be called after a specified delay in milliseconds.
// - Promise.race():
// Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
//
// Run 'seq 10 | xargs -Iz node promise_timeout.js' on the command line to see the results flapping between 'Done' and 'TimeOut'
const BATCH_SIZE = 10
const buildParamforSqsBatch = (products, queueUrl) => {
const entries = products.map(product => toOutputMessage(product))
return {
Entries: entries,
QueueUrl: queueUrl
}
}
const emitUpdateEvent = (params) => {