Skip to content

Instantly share code, notes, and snippets.

@NEbere
Created October 22, 2021 13:36
Show Gist options
  • Save NEbere/92d565b212a68b0d8f7d58307e924b4d to your computer and use it in GitHub Desktop.
Save NEbere/92d565b212a68b0d8f7d58307e924b4d to your computer and use it in GitHub Desktop.
const express = require('express')
const AWS = require("aws-sdk")
var bodyParser = require('body-parser')
const MessageValidator = require('sns-validator')
const config = {
"SNS_TOPIC_ARN": "",
"USER_ARN": "",
"USER_ACCESS_KEY_ID": "",
"USER_SECRET_ACCESS_KEY": "",
"DLQ_ARN": "",
"REGION": "region"
};
const app = express()
const port = 3040
app.use(
express.json({
type: [
'application/json',
'text/plain', // AWS sends this content-type for its messages/notifications
],
})
)
// const credentials = new AWS.SharedIniFileCredentials({profile: 'sns_profile'});
const SNS = new AWS.SNS({
credentials: {
accessKeyId: config.USER_ACCESS_KEY_ID,
secretAccessKey: config.USER_SECRET_ACCESS_KEY,
},
region: config.REGION
})
const Dynamodb = new AWS.DynamoDB({
credentials: {
accessKeyId: config.USER_ACCESS_KEY_ID,
secretAccessKey: config.USER_SECRET_ACCESS_KEY,
},
region: config.REGION
})
const message = {
"_id": "617283d4f1d213b4adc38468",
"guid": "5a821ffb-6fd5-446b-aeb7-0341b50afc3c",
"age": 31,
"name": "Adriana Banks",
"company": "MOLTONIC",
"email": "[email protected]",
}
app.get('/', (req, res) => {
res.send({ response: 'Hello World! - SNS publisher service' })
})
// Endpoint used to publish message to SNS topic
app.post('/publish', async (req, res) => {
let params = {
TopicArn: config.SNS_TOPIC_ARN,
Message: JSON.stringify(message),
};
const publishTextPromise = SNS.publish(params).promise();
publishTextPromise.then(async data => {
console.log(`Message ${params.Message} sent to the topic ${params.TopicArn}`);
console.log("MessageID is " + data.MessageId);
// save to dynamoDB
await Dynamodb.putItem({
TableName: 'test-billing-order-sending-table',
Item: {
'entity_id': { S: message._id },
'guid': { S: message.guid },
'name': { S: message.name },
'company': { S: message.company },
'email': { S: message.email },
'age': { S: `${message.age}` },
'state': { S: 'success' },
}
}).promise();
res.send("message published")
}).catch(err => {
console.error(err, err.stack);
});
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment