Skip to content

Instantly share code, notes, and snippets.

View alexcasalboni's full-sized avatar
✌️
Happy coding!

Alex Casalboni alexcasalboni

✌️
Happy coding!
View GitHub Profile
@alexcasalboni
alexcasalboni / macro-template.yml
Last active March 3, 2019 16:17
CloudFormation Macro definition
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Resources:
MyProcessingFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: python2.7
# ...
@alexcasalboni
alexcasalboni / template.yml
Created February 26, 2019 09:02
Amazon Kinesis Data Firehose - AWS Lambda data transformation app (YAML)
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
DeliveryBucket:
Type: AWS::S3::Bucket
StreamProcessFunction:
Type: AWS::Serverless::Function
Properties:
@alexcasalboni
alexcasalboni / handler.py
Created February 26, 2019 08:47
Amazon Kinesis Data Firehose - AWS Lambda processor
import json
from base64 import b64decode, b64encode
# some useful constants
STATUS_OK = 'Ok'
STATUS_DROPPED = 'Dropped'
STATUS_FAIL = 'ProcessingFailed'
class DroppedRecordException(Exception):
""" This exception can be raised if a record needs to be skipped/dropped """
@alexcasalboni
alexcasalboni / template.yml
Last active February 25, 2019 23:59
AWS Config - AWS Lambda Custom Rule example (YAML)
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
# ...
# all the other properties here
# ...
@alexcasalboni
alexcasalboni / utility.js
Created February 25, 2019 23:30
AWS Config Custom Rule - AWS Lambda (utiity)
const aws = require('aws-sdk');
const config = new aws.ConfigService();
/**
* Get the configurationItem for the resource using the getResourceConfigHistory API.
*/
async function getConfigurationFromHistory(configurationHistory, callback) {
const params = {
resourceType: configurationHistory.resourceType,
resourceId: configurationHistory.resourceId,
@alexcasalboni
alexcasalboni / index.js
Last active February 25, 2019 23:30
AWS Config Custom Rule - AWS Lambda
const aws = require('aws-sdk');
const utility = require('./utility');
const config = new aws.ConfigService();
/**
* In this example, the resource is compliant if it is an instance and its type matches the type specified as the desired type.
* If the resource is not an instance, then this resource is not applicable.
*/
function evaluateChangeNotificationCompliance(configurationItem, ruleParameters) {
if (configurationItem.resourceType !== 'AWS::EC2::Instance') {
@alexcasalboni
alexcasalboni / template.yml
Last active February 19, 2019 22:26
Amazon Cognito User Pools - AWS Lambda hook example (YAML)
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
# ...
# all the other properties here
# ...
@alexcasalboni
alexcasalboni / index.js
Created February 19, 2019 22:08
Amazon Cognito User Pools - Custom Message Hook with AWS Lambda (Node.js)
exports.handler = (event, context, callback) => {
if(event.userPoolId === "theSpecialUserPool") {
// check event type
if(event.triggerSource === "CustomMessage_SignUp") {
// customize message and subject content
event.response.smsMessage = "Welcome to the service. Your confirmation code is " + event.request.codeParameter;
event.response.emailSubject = "Welcome to the service";
event.response.emailMessage = "Thank you for signing up. " + event.request.codeParameter + " is your verification code";
}
}
@alexcasalboni
alexcasalboni / aws-sam-automatic-rollback-demo.md
Last active March 22, 2023 05:27
AWS SAM Demo - Automatic Rollback for AWS Lambda with AWS CodeDeploy

How to Build and Deploy Serverless Apps [AWS Summit]

This demo was presented at the AWS Summit @ Cape Town on Jul 12th.

You can find the slides here.

What's included in this Gist?

  • index.js: The node.js code used for AWS Lambda
  • sam_template.yaml: The AWS SAM template in YAML format (i.e. CloudFormation)
@alexcasalboni
alexcasalboni / aws-lambda-static-type-checker.md
Last active May 22, 2023 07:31
AWS Lambda Static Type Checker Example (Python3)

How to use Python3 Type Hints in AWS Lambda

TL;DR

Static Type Checkers help you find simple (but subtle) bugs in your Python code. Check out lambda_types.py and incrementally improve your code base and development/debugging experience with type hints.

Your Lambda Function code will go from this: