Skip to content

Instantly share code, notes, and snippets.

Avatar

Eric Johnson singledigit

View GitHub Profile
@singledigit
singledigit / get-vars.py
Last active Nov 30, 2020
Get all env variables for all AWS Lamda functions in a CloudFormation/SAM stack
View get-vars.py
#! /usr/bin/env python
# Use at your own risk and reward.
# requires boto3 to be installed
# example `./get-vars.py MyStack > vars.json`
import sys, json
import boto3
if len(sys.argv) == 1:
@singledigit
singledigit / cognito.yaml
Last active Nov 28, 2020
Create a Cognito Authentication Backend via CloudFormation
View cognito.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: Cognito Stack
Parameters:
AuthName:
Type: String
Description: Unique Auth Name for Cognito Resources
Resources:
# Creates a role that allows Cognito to send SNS messages
SNSRole:
@singledigit
singledigit / RequestTemplate-vtl.json
Last active Nov 6, 2020
Example of using VTL on Amazon API Gateway to insert an epoch time into Amazon DynamoDB to be used as a TTL field
View RequestTemplate-vtl.json
## This examples converts the millisecond epoch of API Gateway
## to the seconds epoch of DynamoDB and adds 5 minutes for the TTL.
#set( $epoch = $context.requestTimeEpoch / 1000 + 300 )
{
"TableName":"Epoch",
"Item":{
"id":{"S":"$context.requestId"},
"ttlTime":{"N":"$epoch"}
}
@singledigit
singledigit / api.yaml
Created Sep 24, 2020
Example of an AWS Lambda Destinations configuration for AWS Lambda functions within an application.
View api.yaml
# You don't need this, but here it is anyways :)
openapi: "3.0.1"
info:
title: "Translation API (Async)"
version: "1.0.0"
paths:
/:
post:
summary: Create new translation request
@singledigit
singledigit / template.yaml
Created Aug 18, 2020
Creating an EventBridge rule with API Gateway as the target in AWS SAM
View template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Event Bridge -> API Gateway
Resources:
RestApiGateway:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
@singledigit
singledigit / template.yaml
Created Aug 11, 2020
Using a DynamoDB CloudFormation resource in a SAM template to add range.
View template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: DDB example
Resources:
Table:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
@singledigit
singledigit / 1_instructions.md
Last active Apr 25, 2020
Testing a Lambda function locally against DynamoDB on AWS.
View 1_instructions.md

Local development with SAM

This method allows you to iterate very quickly against Lambda code without having to run AWS resources locally.

Requirements

Step 1: Create and deploy a simple SAM Template (see template.yaml)

  • must include the environment varibales on the Lambda to pass the SAMPLE_TABLE in (see line 17)
  • use sam deploy --guided for the first time deploy
View sam-http-api.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: HTTP API test
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
@singledigit
singledigit / SAM-Lambda-Layer.yaml
Last active Oct 21, 2019
How to create and manage a Lambda Layer using AWS SAM
View SAM-Lambda-Layer.yaml
## Manage your Lambda layer in SAM
## This creates a layer and the permissions for the layer. This particular permission opens it to the world.
## I then use 'sam package' and 'sam deploy' to create/update the layer
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Lambda Layer
Resources:
MyLayer:
@singledigit
singledigit / handler.js
Created Oct 15, 2019
Lambda API Gateway Response Handler
View handler.js
const isLocal = process.env.AWS_SAM_LOCAL;
const handlerModel = (body, headers, code) => {
let response = {
'statusCode': code,
'body': JSON.stringify(body)
}
if(headers.length > 0) {
response.headers = response.headers || {};
You can’t perform that action at this time.