Skip to content

Instantly share code, notes, and snippets.

@cmtoomey
Created October 18, 2016 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmtoomey/8e690aa1b6a003385d822d3c42b0d1ae to your computer and use it in GitHub Desktop.
Save cmtoomey/8e690aa1b6a003385d822d3c42b0d1ae to your computer and use it in GitHub Desktop.
Lambda Function to populate DynamoDB tables
'use strict';
console.log('Loading function');
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
const dynamodb = new aws.DynamoDB('2012-08-10');
exports.handler = (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
// Get the object from the event and show its content type
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const params = {
Bucket: bucket,
Key: key,
};
s3.headObject(params, (err, data) => {
if (err) {
console.log(err);
const message = `Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.`;
console.log(message);
callback(message);
} else {
var tableName = 'Workers';
console.log(bucket);
console.log(key);
console.log(data.Metadata.versionid);
dynamodb.putItem({
"TableName": tableName,
"Item" : {
"versionid": {"S": data.Metadata.versionid },
"bucket": {"S": bucket },
"key": {"S": key}
}
}, function(err, data) {
if (err) {
context.done('error','putting item into dynamodb failed: '+err);
}
else {
console.log('great success: '+JSON.stringify(data, null, ' '));
context.done('K THX BY');
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment