-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into simple-app
- Loading branch information
Showing
50 changed files
with
2,245 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports.handler = function(event, context) { | ||
context.succeed(''); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.