Skip to content

Instantly share code, notes, and snippets.

View leegilmorecode's full-sized avatar
:atom:
Serverless Hero

Lee Gilmore leegilmorecode

:atom:
Serverless Hero
View GitHub Profile
@leegilmorecode
leegilmorecode / get-note-types.vtl
Created July 15, 2021 11:53
VTL showing pulling through an array of objects using substitutions
{
"version": "2017-02-28",
"payload": ${noteTypes}
}
@leegilmorecode
leegilmorecode / get-note-types.js
Created July 15, 2021 11:54
Example function returning config values for the serverless substitutions values
function getNoteTypes(locale = "uk") {
const types = {
uk: [
{
noteTypeId: 1,
type: "Draft",
},
{
noteTypeId: 2,
type: "Published",
@leegilmorecode
leegilmorecode / generate-screenshot.ts
Created July 21, 2021 06:01
The generate screenshot code
/* eslint-disable @typescript-eslint/no-var-requires */
import AWS from 'aws-sdk';
import { v4 as uuid } from 'uuid';
import { APIGatewayProxyHandler, APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda';
import { config } from './shared/config';
import { Browser, Page } from 'puppeteer-core';
const chromium = require('chrome-aws-lambda');
let browser: Browser;
let page: Page;
@leegilmorecode
leegilmorecode / load.yml
Created July 29, 2021 09:16
Example artillery load testing file
config:
plugins:
expect: {} # this plugin allows for assertions: https://artillery.io/docs/guides/plugins/plugin-expectations-assertions.html
ensure:
p95: 1000 # ensure latency is equal to or under 1000ms
maxErrorRate: 0 # no percentage of error rate i.e. no errors or pipeline fails
payload:
path: "./data/data.csv" # pull in the employee data csv
fields:
- "id"
@leegilmorecode
leegilmorecode / functional.yml
Created July 29, 2021 09:25
An example of artillery functional and fuzzing tests
config:
plugins:
expect: {} # this plugin allows for assertions: https://artillery.io/docs/guides/plugins/plugin-expectations-assertions.html
fuzzer: {} # this plugin allows for fuzzing: https://artillery.io/docs/guides/plugins/plugin-fuzzer.html
ensure:
maxErrorRate: 0 # no percentage of error rate i.e. no errors or pipeline fails
payload:
path: "./data/data.csv" # pull in the employee data csv
fields:
- "id"
@leegilmorecode
leegilmorecode / validator.js
Created July 29, 2021 09:35
A basic validate function using JSON schema and AJV
import Ajv from "ajv";
import { AppError } from "../errors/app-error";
import { errorTypes, logLevels } from "../errors/error-constants";
export function validate(obj, schema) {
const ajv = new Ajv({
allErrors: true,
});
const validator = ajv.compile(schema);
@leegilmorecode
leegilmorecode / error-handler.js
Created July 29, 2021 09:41
Very basic example of a generic error handler
// basic error handler which will not expose any stack traces/secrets
// and will return the correct responses when invalid params from validator
export const errorHandler = (error) => {
let body = "An error has occurred";
let statusCode = 500;
const errorType = error && error.errorType ? error.errorType : 2;
switch (errorType) {
@leegilmorecode
leegilmorecode / create-employee.schema.js
Created July 29, 2021 09:43
Very basic example of JSON schema to test a create employee input
export const schema = {
type: "object",
required: ["id", "firstName", "surname", "age"],
maxProperties: 4,
minProperties: 4,
properties: {
id: {
type: "number",
},
firstName: {
@leegilmorecode
leegilmorecode / get-files-tmp.ts
Created July 31, 2021 14:06
Get files from the tmp folder on the lambda
import { APIGatewayProxyHandler, APIGatewayProxyResult } from 'aws-lambda';
import { getImagesFromBucket } from '../shared/get-images-from-bucket/get-images-from-bucket';
import { getFilesFromFolder } from '../shared/get-files-from-folder/get-files-from-folder';
import { writeFilesToFolder } from '../shared/write-files-to-folder/write-files-to-folder';
import { sortFiles } from '../shared/sort-files/sort-files';
import FileObject from '../shared/types/FileObject';
import { config } from '../shared/config/config';
async function getFiles(): Promise<FileObject[]> {
// attempt to get the files from /tmp on the lambda
@leegilmorecode
leegilmorecode / get-files-s3.ts
Created July 31, 2021 14:10
Example of pulling the files from S3
import { APIGatewayProxyHandler, APIGatewayProxyResult } from 'aws-lambda';
import { getImagesFromBucket } from '../shared/get-images-from-bucket/get-images-from-bucket';
import { sortFiles } from '../shared/sort-files/sort-files';
import { config } from '../shared/config/config';
import FileObject from '../shared/types/FileObject';
export const handler: APIGatewayProxyHandler = async (): Promise<APIGatewayProxyResult> => {
try {
console.log(`Get the files from the s3 bucket ${config.bucketName}`);