Last active
December 15, 2015 19:03
-
-
Save fwertz/a14385e9fcb1dbb1a102 to your computer and use it in GitHub Desktop.
Sample AWS Lambda Service Handlers
This file contains hidden or 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
var AWS = require( 'aws-sdk' ), | |
env = process.env; | |
var db = new AWS.DynamoDB({ | |
accessKeyId: env.ACCESS_KEY_ID, | |
secretAccessKey: env.SECRET_ACCESS_KEY | |
}); | |
module.exports = { | |
handler: function ( event, context ) { | |
if ( !event.email ) { | |
return context.fail( 'Please specify an `email`' ); | |
} | |
db.scan({ | |
TableName: 'tbl_SavedDesigns', | |
Select: 'ALL_ATTRIBUTES', | |
FilterExpression: 'Email = :v_email', | |
ExpressionAttributeValues: { | |
':v_email': { | |
S: event.email | |
} | |
} | |
}, function ( err, data ) { | |
if ( err ) { | |
context.fail( err ); | |
} else { | |
context.succeed( data ); | |
} | |
}); | |
} | |
} |
This file contains hidden or 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
var AWS = require( 'aws-sdk' ), | |
crypto = require( 'crypto' ), | |
env = process.env; | |
var db = new AWS.DynamoDB({ | |
accessKeyId: env.ACCESS_KEY_ID, | |
secretAccessKey: env.SECRET_ACCESS_KEY | |
}); | |
function generateToken() { | |
return crypto.createHash( 'md5' ).update( ''+Date.now() ).digest( 'hex' ).substr( 0, 7 ); | |
} | |
module.exports = { | |
handler: function ( event, context ) { | |
if ( !event.saveId && !event.email ) { | |
return context.fail( 'Please specify an `email`' ); | |
} | |
if ( !event.saveId && !event.clientId ) { | |
return context.fail( 'Please specify a `clientId`' ); | |
} | |
if ( !event.selected ) { | |
return context.fail( 'Please specify valid `selected` JSON' ); | |
} | |
for ( var category in event.selected ) { | |
event.selected[category] = { | |
S: event.selected[category] | |
} | |
} | |
var saveId = generateToken(); | |
db.putItem({ | |
TableName: 'tbl_SavedDesigns', | |
Item: { | |
SaveID: { | |
S: event.saveId || saveId | |
}, | |
Email: { | |
S: event.email | |
}, | |
ClientID: { | |
S: event.clientId | |
}, | |
Selected: { | |
M: event.selected | |
} | |
} | |
}, function ( err, data ) { | |
if ( err ) { | |
context.fail( err ); | |
} else { | |
context.succeed( data ); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment