Skip to content

Commit

Permalink
borrow baseline AWS Lambda function handler code from https://github.…
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Feb 4, 2024
1 parent 1a40cad commit 132a46e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lambda/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
require('env2')('.env');
const save = require('../lib/s3.js').save;

/**
* `debug` is used to debug SNS notification events.
* it only gets executed if the NODE_ENV is set to "test".
* To save event data to S3 you will need to add AWS_S3_BUCKET to .env
* see: github.com/dwyl/aws-ses-lambda/issues/12
* @param {Object} event - the object we want to store on S3
*/
module.exports = function debug (event) {
// console.log("process.env.NODE_ENV:", process.env.NODE_ENV);
if (process.env.NODE_ENV === "test") {
if(event.Records && !event.key) {
event.key = "sns";
}
save(event, function callback (error, data) {
console.log("DEBUG - - - error:", error, " - - - data:");
console.log(data);
console.log(" - - - - - - - - - - - - - - - - - - - - ");
});
}
};
39 changes: 39 additions & 0 deletions lambda/http_request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

require("env2")(".env"); // ensure JWT_SECRET environment variable is defined.
const http = require('https'); // ALWAYS use TLS over the internets!
const jwt = require('jsonwebtoken');
/**
* simple_http_request is a bare-bones http request using node.js core http
* see: https://nodejs.org/api/http.html#http_http_request_options_callback
* @param {Object} json - the JSON data we want to send to the Phoenix App.
* @param {Function} callback - a standard callback with error & response args
* response is a JSON Object unless there is an error. No error handling yet ...
*/

module.exports = function simple_http_request (json, callback) {
const options = { // the json data is included in the token! 😮
headers: {
'Authorization': jwt.sign(json, process.env.JWT_SECRET),
'Accept': 'application/json'
},
hostname: process.env.EMAIL_APP_URL, // e.g: phemail.herokuapp.com
method: 'POST', // HTTP post sans body: stackoverflow.com/questions/4191593
port: '443',
path: '/api/sns' // the API endpoint that processes and stores SNS data
}

http.request(options, function (res) {
let resStr = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
resStr += chunk;
}).on('end', function () {
return callback(res.statusCode, JSON.parse(resStr));
});
})
// .on('error', (e) => {
// console.error(`problem with request: ${e.message}`);
// })
.end();
};
7 changes: 7 additions & 0 deletions lambda/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require("env2")(".env");
const debug = require("./lib/debug.js");

exports.handler = function handler (event, context, callback) {
debug(event);
return callback(null, event);
}
52 changes: 52 additions & 0 deletions lambda/s3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';
require('env2')('.env');
const AWS = require('aws-sdk');
AWS.config.region = 'eu-west-1';
var s3 = new AWS.S3({params: {Bucket: process.env.AWS_S3_BUCKET}});

/**
* `save` saves a JSON object to S3.
* if you need to specify the file name, use `json.key`
* @param {Object} json - the object we want to store on S3
* @param {Function} callback - called once the file has been uploaded
*/
module.exports.save = function save (json, callback) {
if (json) {
const filename = json.key || 'event'
const params = {
Key: filename + '.json',
Body: JSON.stringify(json),
ContentType: 'application/json',
ACL: 'public-read'
};

s3.upload(params, function (err, data) {
if (callback && typeof callback === "function") {
return callback(err, data);
}
else {
return data;
}
});

} else {
return callback('ERROR: please provide json data');
}
}

/**
* `get` retrieves and parses a JSON file from S3
* this function is only used to test that the `save` method.
* @param {String} key - the filename of the object to get from S3
* @param {Function} callback - called once the file has been uploaded
*/
module.exports.get = function get (key, callback) {
s3.getObject({Key: key}, function (error, data) {
if (error) {
return callback(error);
}
else {
return callback(error, JSON.parse(data.Body.toString()));
}
});
};

0 comments on commit 132a46e

Please sign in to comment.