Created
January 28, 2020 13:50
-
-
Save sjmach/6ad34f218319ddc4799f86292c6a5a38 to your computer and use it in GitHub Desktop.
AWS Lambda Proxy
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
var AWS = require("aws-sdk"); | |
exports.handler = async (event, context, callback) => { | |
var responseCode = 200; | |
var passcode; | |
if (event.body) { | |
console.log("Data going to encode"); | |
let body = JSON.parse(event.body); | |
/// Get Parameters from form | |
if (body.articleUrl) | |
passcode= body.passcode; | |
} | |
// form data | |
var postData = { | |
passcode: passcode, | |
}; | |
// TopicArn should be the SNS Topic ARN | |
var params ={ | |
Message: JSON.stringify(postData), | |
TopicArn: "XXXXXXXXX" | |
}; | |
var response = { | |
statusCode: responseCode, | |
headers: { | |
"Access-Control-Allow-Origin": "*" | |
}, | |
body: "Data has been saved" | |
}; | |
/// FIX for Options request | |
if(event.httpMethod =="OPTIONS"){ | |
context.succeed(response); | |
return 0; | |
} | |
var publishTextPromise = new AWS.SNS({apiVersion: '2010-03-31'}).publish(params).promise(); | |
// Handle promise's fulfilled/rejected states | |
return publishTextPromise.then( | |
function(data) { | |
console.log("MessageID is " + data.MessageId); | |
context.succeed(response); | |
return 0; | |
}).catch( | |
function(err) { | |
console.error(err, err.stack); | |
callback(err); | |
context.error("Failed"); | |
return 1; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment