Skip to content

Instantly share code, notes, and snippets.

View singledigit's full-sized avatar

Eric Johnson singledigit

View GitHub Profile
@singledigit
singledigit / 1_instructions.md
Last active April 25, 2020 04:55
Testing a Lambda function locally against DynamoDB on AWS.

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
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 / handler.js
Created October 15, 2019 19:51
Lambda API Gateway Response Handler
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 || {};
@singledigit
singledigit / EventBridgeRule.yaml
Created September 25, 2019 14:25
Using EventBridge rules as events in AWS SAM
TranslateFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: translate/
Timeout: 10
Handler: app.lambdaHandler
Runtime: nodejs10.x
Events:
TranslateEvent:
Type: CloudWatchEvent
@singledigit
singledigit / SAM-Lambda-Layer.yaml
Last active October 21, 2019 16:47
How to create and manage a Lambda Layer using AWS SAM
## 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 / vue.config.js
Created July 8, 2019 17:47
Enable preview and live reloading on Cloud9 with Vue-CLI apps
module.exports = {
devServer: {
disableHostCheck: true,
public: 'https://[your cloud9 host id].vfs.cloud9.[region].amazonaws.com'
}
}
const http = require('http');
const hostname = '0.0.0.0';
const port = process.env.PORT || 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
@singledigit
singledigit / infrastructure.yaml
Last active August 17, 2018 07:22
Secure S3 hosting bucket with CloudFront distro. Only allows distro access to the bucket
AWSTemplateFormatVersion: "2010-09-09"
Description: AWS S3 Hosting bucket and CloudFront Distrobution
Resources:
## Origin Access ID for CloudFront
HostAccessIdentity:
Type: "AWS::CloudFront::CloudFrontOriginAccessIdentity"
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: MyHostBucketId
@singledigit
singledigit / handler.js
Last active June 22, 2018 21:43
RDS Snapshot Lambda for automating manual backups.
const AWS = require('aws-sdk');
const rds = new AWS.RDS();
const sns = new AWS.SNS();
const prefix = `snapper-${process.env.TIME_TO_LIVE}-${process.env.TIME_TO_LIVE_METRIC}-${process.env.CLUSTER_ID}`
const createClusterSnapshot = () => {
let params = {
DBClusterIdentifier: process.env.CLUSTER_ID,
DBClusterSnapshotIdentifier: `${prefix}-${Date.now()}`,
Tags: [{ Key: 'type', Value: 'snapper' }]
@singledigit
singledigit / cognito.yaml
Last active January 16, 2024 16:15
Create a Cognito Authentication Backend via CloudFormation
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: