Skip to content

Commit

Permalink
Merge branch 'master' into simple-app
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Feb 10, 2016
2 parents 76d5cf5 + 6ede2c3 commit 5168979
Show file tree
Hide file tree
Showing 50 changed files with 2,245 additions and 60 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ node_modules
# Users Environment Variables
.lock-wscript
.DS_Store

# These will be generated by the gulp script so don't need to be pushed to github
LambdaTest.zip
dist/
admin.env
.env
1,045 changes: 1,035 additions & 10 deletions README.md

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions api-gateway-dynamoDB-example/getUserInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var AWS = require('aws-sdk');
var DOC = require('dynamodb-doc');
var dynamo = new DOC.DynamoDB();

exports.handler = function(event, context) {
var callback = function(err, data) {
if (err) {
console.log('error on getUserInfo: ', err);
context.done('Unable to retrieve user information', null);
} else {
if(data.Item && data.Item.users) {
context.done(null, data.Item.users);
} else {
context.done(null, {});
}
}
};

dynamo.getItem({TableName:"Users", Key:{username:"default"}}, callback);
};
Binary file added api-gateway-dynamoDB-example/getUserInfo.zip
Binary file not shown.
3 changes: 3 additions & 0 deletions api-gateway-dynamoDB-example/no-op.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.handler = function(event, context) {
context.succeed('');
}
Binary file added api-gateway-dynamoDB-example/no-op.zip
Binary file not shown.
21 changes: 21 additions & 0 deletions api-gateway-dynamoDB-example/updateUserInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var AWS = require('aws-sdk');
var DOC = require('dynamodb-doc');
var dynamo = new DOC.DynamoDB();

exports.handler = fucntion(event, context) {
var item = { username:"default",
users: event.users || {}
};

var callback = function(err, data) {
if (err) {
console.log(err);
context.fail('unable to update users at this time');
} else {
console.log(data);
context.done(null, data);
}
};

dynamo.putItem({TableName:"Users", Item:item}, callback);
};
Binary file added api-gateway-dynamoDB-example/updateUserInfo.zip
Binary file not shown.
6 changes: 6 additions & 0 deletions dist/LambdaTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
console.log('Loading event');
console.log('Loading event again');

exports.handler = function(event, context) {
context.succeed(event.key1); // SUCCESS with message
};
9 changes: 8 additions & 1 deletion package.json → dist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "\"How-to Guide for AWS Lambda, DynamoDB, SNS & API Gateway\"",
"main": "index.js",
"scripts": {
"upload": "./lambda-upload-create.sh",
"test": "./node_modules/lab/bin/lab ./lambda-testing/test/index.js -m 0 -c -a code -r lcov -o lcov.info -r console -o stdout",
"coverage": "./node_modules/lab/bin/lab -r html -o ./test/coverage.html"
},
Expand All @@ -26,6 +27,12 @@
"devDependencies": {
"aws-lambda-mock-context": "^1.1.0",
"code": "^2.0.1",
"lab": "^7.2.0"
"lab": "^7.2.0",
"archiver": "^0.21.0",
"aws-sdk": "^2.2.29",
"gulp": "^3.9.0",
"gulp-install": "^0.6.0",
"gulp-zip": "^3.0.2",
"run-sequence": "^1.1.5"
}
}
164 changes: 164 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
var AWS = require('aws-sdk');
var gulp = require('gulp');
var zip = require('gulp-zip');
var install = require('gulp-install');
var runSequence = require('run-sequence');
var fs = require('fs');

var packageJson = require('./package.json');

//constants
var region = 'eu-west-1';
var functionName = 'LambdaTest';
var outputName = 'LambdaTest.zip';

var IAMRole = 'arn:aws:iam::655240720487:role/lambda_basic_execution';
var filesToPack = ['./lambda-testing/functions/LambdaTest.js'];

/**
* Adds the project files to the archive folder.
*/
gulp.task('js', function () {
return gulp.src(filesToPack, {base: './lambda-testing/functions'})
.pipe(gulp.dest('dist/'));
});

/**
* This task will copy all the required dependencies to
* the dist folder.
*/
gulp.task('node-mods', function () {
return gulp.src('./package.json')
.pipe(gulp.dest('dist/'))
.pipe(install({production: true}));
});

/**
* Create an archive based on the dest folder.
*/

gulp.task('zip', function () {
return gulp.src(['dist/**', '!dist/package.json'])
.pipe(zip(outputName))
.pipe(gulp.dest('./'));
});

/**
* Upload deployment package to S3 (lambda function file + dependencies)
*/
gulp.task('upload-to-s3', function () {
var s3 = new AWS.S3();
var zipFilePath = './' + outputName;
getZipFile(function (data) {
var params = {
Bucket: 'lambda-function-container',
Key: zipFilePath,
Body: data
};
s3.putObject(params, function(err, data) {
if (err) console.log('Object upload unsuccessful!');
else console.log('Object ' + outputName + ' was uploaded!');
});
});
function getZipFile (next) {
fs.readFile(zipFilePath, function (err, data) {
if (err) console.log(err);
else {
next(data);
}
});
}
});

/**
* update or create the lambda functon
*/
gulp.task('upload', function() {
AWS.config.region = region;
var lambda = new AWS.Lambda();
var s3 = new AWS.S3();
var zipFile = './' + outputName;
var bucketName = 'lambda-function-container';

lambda.getFunction({ FunctionName: functionName }, function(err, data) {
if (err) checkObject(createFunction);
else checkObject(updateFunction);
});
function checkObject (fn) {
var params = {
Bucket: bucketName,
Key: zipFile
};
s3.getObject(params, function (err, data) {
if (err) console.log('BUCKET ERROR', err);
else fn();
});
}
function createFunction () {
var params = {
Code: {
S3Bucket: bucketName,
S3Key: zipFile
},
FunctionName: functionName,
Handler: 'LambdaTest.handler',
Role: IAMRole,
Runtime: 'nodejs'
};

lambda.createFunction (params, function (err, data) {
if (err) console.error("CREATE ERROR", err);
else console.log('Function ' + functionName + ' has been created.');
});

}

function updateFunction () {
var params = {
FunctionName: functionName,
S3Bucket: bucketName,
S3Key: zipFile
};

lambda.updateFunctionCode(params, function(err, data) {
if (err) console.error(err);
else console.log('Function ' + functionName + ' has been updated.');
});
}

});

gulp.task('test-invoke', function() {
var lambda = new AWS.Lambda();

var params = {
FunctionName: functionName,
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: '{ "key1" : "name" }'
};

lambda.getFunction({ FunctionName: functionName }, function(err, data) {
if (err) console.log("FUNCTION NOT FOUND", err);
else invokeFunction();
});

function invokeFunction() {
lambda.invoke(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
})
}
})


gulp.task('deploy', function (callback) {
return runSequence(
['js', 'node-mods'],
['zip'],
['upload-to-s3'],
['upload'],
['test-invoke']
callback
);
});
9 changes: 9 additions & 0 deletions lambda-testing/functions/DynamoDBLambdaTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.handler = function(event, context) {
//console.log('Received event:', JSON.stringify(event, null, 2));
event.Records.forEach(function(record) {
console.log(record.eventID);
console.log(record.eventName);
console.log('DynamoDB Record: %j', record.dynamodb);
});
context.succeed(event.Records.length);
};
9 changes: 5 additions & 4 deletions lambda-testing/functions/lambdaTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
console.log('Loading event');
console.log('Loading event again');

exports.handler = function(event, context) {
context.succeed(event.key1); // SUCCESS with message
if(event.key1) {
context.succeed(event.key1);
} else {
context.fail("no key1");
}
};
30 changes: 30 additions & 0 deletions lambda-testing/test/ava.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
var test = require('ava');
var context = require('aws-lambda-mock-context');
var promisify = require('aws-lambda-pify');
var createDynamoDBEvent = require('./utils/eventCreators').createDynamoDBEvent;

/**
Create mock event and context objects
**/

var ctx = context();
var testEvent = { key1: 'name' }
var testDynamoDBEvent = createDynamoDBEvent();

/**
Promisify handlers and pass in mock context
**/

var LambdaTest = promisify(require('../functions/LambdaTest.js').handler, ctx);
var DynamoDBLambdaTest = promisify(require('../functions/DynamoDBLambdaTest.js').handler, ctx)

test('LambdaTest', async t => {
t.is( await LambdaTest(testEvent), 'name')
})

test('DynamoDBLambdaTest', async t => {
t.is( await DynamoDBLambdaTest(testDynamoDBEvent), 3)
})

console.log('Tests took', process.uptime(), "seconds to run");
55 changes: 55 additions & 0 deletions lambda-testing/test/callbacks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var describe = lab.experiment;
var expect = Code.expect;
var it = lab.test;
var createDynamoDBEvent = require('./utils/eventCreators').createDynamoDBEvent;

/**
Handlers
**/
var LambdaTest = require('../functions/LambdaTest.js').handler
var DynamoDBLambdaTest = require('../functions/DynamoDBLambdaTest.js').handler

/**
Create mock event and context objects
**/
var contextCreator = require('./utils/mockContext.js');
var testEvent = { key1: 'name' }
var testDynamoDBEvent = createDynamoDBEvent();

describe('LambdaTest', function(){
it("LambdaTest: returns value when given event with key1 property", function(done) {

function test(result){
expect(result).to.equal("name")
done();
}
var context = contextCreator(test);
LambdaTest(testEvent, context);
})
it("LambdaTest: returns error when given empty event", function(done) {
function test(error){
expect(error).to.equal("no key1")
done();
}
var context = contextCreator(test);
LambdaTest({}, context);
})
})

describe('DynamoDB Triggered Lambda Test', function(){
it("DynamoDBTest: returns number of records in the event", function(done) {

function test(result){
expect(result).to.equal(3)
done();
}
var context = contextCreator(test);

DynamoDBLambdaTest(testDynamoDBEvent, context);
})
})
3 changes: 0 additions & 3 deletions lambda-testing/test/data.json

This file was deleted.

Loading

0 comments on commit 5168979

Please sign in to comment.