Skip to content

Instantly share code, notes, and snippets.

View leegilmorecode's full-sized avatar
:atom:
That Serverless Guy

Lee Gilmore leegilmorecode

:atom:
That Serverless Guy
View GitHub Profile
@leegilmorecode
leegilmorecode / serverless.ts
Created June 19, 2021 14:16
Example of configuring alarms only for certain environments
import type { AWS } from "@serverless/typescript";
import hello from "@functions/hello";
import { configureAlarms } from "@libs/alarms";
const serverlessConfiguration: AWS = {
service: "test",
frameworkVersion: "2",
custom: {
webpack: {
@leegilmorecode
leegilmorecode / alarms.ts
Created June 19, 2021 14:19
Bespoke IaC configuration depending on stage being deployed
import { config } from "@shared/config";
import { Stages } from "@models/Stages";
import hello from "@functions/hello";
export const configureAlarms = () => {
// only add alarm if prod or staging
if (config.stage === Stages.prod || config.stage === Stages.staging) {
return {
HelloFunctionErrorAlarm: {
Type: "AWS::CloudWatch::Alarm",
@leegilmorecode
leegilmorecode / serverless.ts
Created June 19, 2021 14:37
Example base serverless.ts file when autogenerated using the CLI
import type { AWS } from "@serverless/typescript";
import hello from "@functions/hello";
const serverlessConfiguration: AWS = {
service: "test",
frameworkVersion: "2",
custom: {
webpack: {
webpackConfig: "./webpack.config.js",
@leegilmorecode
leegilmorecode / serverless.yml
Created June 27, 2021 14:26
Example of AWS AppConfig alongside the Serverless Framework for the use of Feature Flags
service: serverless-feature-flag
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
memorySize: 128
stage: ${opt:stage, 'develop'}
region: eu-west-1
apiGateway:
shouldStartNameWithService: true
@leegilmorecode
leegilmorecode / handler.js
Created June 27, 2021 14:29
Example of a lambda handler using the AWS AppConfig Lambda Layer Extension.
/* eslint-disable no-undef */
const axios = require("axios");
const {
STAGE: stage,
APPLICATION: application,
ENVIRONMENT: environment,
CONFIGURATION: configuration,
} = process.env;
@leegilmorecode
leegilmorecode / serverless.yml
Created July 13, 2021 09:37
Serverless Lambda Destinations
service: serverless-lambda-destinations
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
memorySize: 128
stage: ${opt:stage, 'develop'}
region: eu-west-1
apiGateway:
shouldStartNameWithService: true
@leegilmorecode
leegilmorecode / getErrorMessages.js
Created July 15, 2021 11:44
Basic JS function to return the write config based on locale
function getErrorMessages(locale = "uk") {
const errorMessages = {
uk: {
noDataErrorMessage: "No data",
accessDeniedErrorMessage: "Access denied",
invalidValueErrorMessage: "The value {0} is invalid",
},
fr: {
noDataErrorMessage: "Pas de données",
accessDeniedErrorMessage: "Accès refusé",
@leegilmorecode
leegilmorecode / getErrorTypes.js
Created July 15, 2021 11:45
Basic example function which returns the correct config values based on locale
function getErrorTypes(locale = "uk") {
const errorTypes = {
uk: {
noDataErrorType: "NoData",
accessDeniedErrorType: "AccessDenied",
invalidValueErrorType: "InvalidValue",
},
fr: {
noDataErrorType: "PasDeDonnées",
accessDeniedErrorType: "AccèsRefusé",
@leegilmorecode
leegilmorecode / get-note-request.vtl
Created July 15, 2021 11:48
Example VTL showing substitutions and placeholders
#if($ctx.args.noteId == 0)
$utils.error($util.str.toReplace("${invalidValueErrorMessage}", "{0}", "$ctx.args.noteId"), "${invalidValueErrorType}")
#else
{
"version" : "2017-02-28",
"operation" : "GetItem",
"key" : {
"noteId" : { "S" : "${context.arguments.noteId}" },
},
"consistentRead" : true
@leegilmorecode
leegilmorecode / serverless.js
Created July 15, 2021 11:50
serverless.js file showing importing values from JS files into the substitutions property
/* eslint-disable no-undef */
const { getNoteTypes } = require("./src/note-types/get-note-types");
const { getErrorTypes } = require("./src/error-types/get-error-types");
const { getErrorMessages } = require("./src/error-messages/get-error-messages");
const serverlessConfiguration = {
service: "serverless-appsync-i18n",
frameworkVersion: "2",
plugins: ["serverless-appsync-plugin"],
provider: {