Skip to content

Instantly share code, notes, and snippets.

@johnib
Created May 16, 2017 21:42
Show Gist options
  • Save johnib/1c93c1a958b06f4ca86aa34d92c0fe78 to your computer and use it in GitHub Desktop.
Save johnib/1c93c1a958b06f4ca86aa34d92c0fe78 to your computer and use it in GitHub Desktop.
"use strict";
let aws = require('aws-sdk'),
fs = require('fs'),
q = require('q'),
s3 = new aws.S3({signatureVersion: 'v4'}),
Logger = require('pretty-logger'),
log = new Logger(),
lambda = new aws.Lambda({region: 'us-east-1'});
function putObject(params) {
let defer = q.defer();
s3.putObject(params, (err, data) => {
if (err) defer.reject(err);
else defer.resolve(data);
});
return defer.promise;
}
function updateFunctionCode(params) {
let defer = q.defer();
lambda.updateFunctionCode(params, (err, data) => {
if (err) defer.reject(err);
else defer.resolve(data);
});
return defer.promise;
}
(function updateLambda(zipfile, funcName) {
if (!zipfile || !funcName) {
log.error('Specify filename to upload and function name to publish');
process.exit(1);
}
putObject({
Bucket: 'bucket-name',
Key: `sub/directory/${zipfile}`,
Body: fs.readFileSync(zipfile)
})
.then(() => {
log.info(`Successfully uploaded file: ${zipfile}`);
return updateFunctionCode({
FunctionName: funcName,
S3Bucket: 'johni',
S3Key: `twitter-trends/lambda/${zipfile}`,
Publish: false
})
})
.then(log.info.bind(log))
.catch(log.error.bind(log))
.finally(() => {
process.exit();
});
})(process.argv[2], process.argv[3]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment