Event driven code with Node.js + AWS Lambda
Workshop
https://github.com/jpizarrom/event-driven-code-with-nodejs-aws-lambda-workshop
@jpizarrom
@jpizarrom
- Lead Software Engineer @ Admetricks.com
- AWS Lambda
- Demo
- QA
Demo: mercado publico
Functions
- collector
- download_list
- list_stream_processor
- download_detail
- detector
- detail_stream_processor
- detector
- notificator
- notificator
- elastic
- elastic
Hands on
Lab 0: Deps
- nvm
- https://github.com/creationix/nvm
- get code
- https://github.com/jpizarrom/event-driven-code-with-nodejs-aws-lambda-workshop
- npm install serverless@0.5.0
- api key
- http://api.mercadopublico.cl/modules/api.aspx
Lab 1: collector-download_detail
deploy
cd collector/download_detail && npm install && cd ../..
./node_modules/.bin/serverless function deploy collector-download_detail
run
./node_modules/.bin/serverless function run collector-download_detail
events
./node_modules/.bin/serverless event deploy
{
"name": "collector-download_detail",
"runtime": "nodejs",
"description": "Serverless Lambda function for project: serverless-mp",
"customName": false,
"customRole": false,
"handler": "handler.handler",
"timeout": 120,
"memorySize": 128,
"authorizer": {},
"custom": {
"excludePatterns": []
},
"endpoints": [],
"events": [
{
"name": "detailsTable",
"type": "kinesisStream",
"config": {
"streamArn": "${detailStreamARN}",
"startingPosition": "LATEST",
"batchSize": 1,
"enabled": true
}
}
],
"environment": {
"SERVERLESS_PROJECT": "${project}",
"SERVERLESS_STAGE": "${stage}",
"SERVERLESS_REGION": "${region}",
"DETAILS_TABLE": "$${detailsTable}",
"MERCADOPUBLICO_TICKET": "${mercadopublicoTicket}"
},
"vpc": {
"securityGroupIds": [],
"subnetIds": []
}
}
download_list
download
var dynamodb = new AWS.DynamoDB();
Promise.promisifyAll(Object.getPrototypeOf(dynamodb));
var validateInput = function(data) {
return new Promise(function(resolve, reject) {
resolve(data);
});
}
var createList = function(data) {
return new Promise(function(resolve, reject) {
var user = {
id: data.code,
created: moment().unix().toString(),
updated: moment().unix().toString(),
info: data.info,
}
resolve(user);
});
}
var storeList = function(user) {
debug('Saving ' + JSON.stringify(user));
return dynamodb.putItemAsync({
TableName: process.env.DETAILS_TABLE,
Item: {
id: { S: user.id },
created: { S: user.created },
updated: { S: user.updated },
info: { S: user.info },
},
// ConditionExpression: 'attribute_not_exists (id)'
});
}
CI/CD
build:
ci:
- npm install serverless@0.5.2
on_success:
- |
cat << EOF > admin.env
SERVERLESS_ADMIN_AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
SERVERLESS_ADMIN_AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
EOF
- cat admin.env
- mkdir .aws
- touch .aws/credentials
- mkdir -p _meta/variables/
- |
cat << EOF > _meta/variables/s-variables-common_.json
{
"project": "$PROJECT"
}
EOF
- |
cat << EOF > _meta/variables/s-variables-$BRANCH.json
{
"stage": "$BRANCH"
}
EOF
- |
cat << EOF > _meta/variables/s-variables-$BRANCH-`echo $AWS_DEFAULT_REGION | sed -e 's/-//g'`.json
{
"region": "$AWS_DEFAULT_REGION",
"resourcesStackName": "$RESOURCES_STACK_NAME",
"iamRoleArnLambda": "$IAM_ROLE_ARN_LAMBDA"
}
EOF
- cd collector/download_list && npm install && cd ../..
- HOME=$PWD ./node_modules/.bin/serverless function deploy collector-download_list -s $BRANCH -r $AWS_DEFAULT_REGION --debug
- cd collector/download_detail && npm install && cd ../..
- HOME=$PWD ./node_modules/.bin/serverless function deploy collector-download_detail -s $BRANCH -r $AWS_DEFAULT_REGION --debug
- cd collector/list_stream_processor && npm install && cd ../..
- HOME=$PWD ./node_modules/.bin/serverless function deploy collector-list_stream_processor -s $BRANCH -r $AWS_DEFAULT_REGION --debug
- cd detector/detector && npm install && cd ../..
- HOME=$PWD ./node_modules/.bin/serverless function deploy detector-detector -s $BRANCH -r $AWS_DEFAULT_REGION --debug
- cd detector/detail_stream_processor && npm install && cd ../..
- HOME=$PWD ./node_modules/.bin/serverless function deploy detector-detail_stream_processor -s $BRANCH -r $AWS_DEFAULT_REGION --debug
- cd notificator/notificator && npm install && cd ../..
- HOME=$PWD ./node_modules/.bin/serverless function deploy notificator-notificator -s $BRANCH -r $AWS_DEFAULT_REGION --debug
- cd elastic/elastic && npm install && cd ../..
- HOME=$PWD ./node_modules/.bin/serverless function deploy elastic-elastic -s $BRANCH -r $AWS_DEFAULT_REGION --debug
- http://www.slideshare.net/AmazonWebServices/aws-lambda-eventdriven-code-in-the-cloud-53366393/13
- http://www.slideshare.net/AmazonWebServices/aws-lambda-eventdriven-code-in-the-cloud-53366393/19
- http://www.slideshare.net/apigee/serverless-computing-with-aws-lambda/14
- http://www.slideshare.net/AmazonWebServices/aws-lambda-and-the-serverless-cloud-popup-loft/8
- http://www.slideshare.net/AmazonWebServices/eventdriven-serverless-apps-popup-loft-tel-aviv/26
- http://www.slideshare.net/AmazonWebServices/aws-lambda-how-glide-uses-aws-lambda-for-increased-simplicity-scale-and-speed-popup-loft-tel-aviv/6
- http://www.slideshare.net/AmazonWebServices/bdt209-launch-amazon-elasticsearch-for-realtime-data-analytics/41
- https://codeship.com/documentation/continuous-deployment/deployment-to-aws-lambda/
- http://www.slideshare.net/AmazonWebServices/getting-started-with-serverless-architectures
- http://www.slideshare.net/AmazonWebServices/aws-march-2016-webinar-series-aws-iot-real-time-stream-processing-with-aws-lambda
Auth
Event driven code with Node.js + AWS Lambda Workshop
By Juan Pizarro
Event driven code with Node.js + AWS Lambda Workshop
- 1,603