-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
49 lines (40 loc) · 1.03 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const AWS = require('aws-sdk')
const sqs = new AWS.SQS({
endpoint: new AWS.Endpoint(process.env.ELASTICMQ_URL),
region: process.env.REGION
})
exports.producer = async (event) => {
const message = {
message: event.body,
created_at: new Date().toISOString()
}
await sqs.sendMessage({
MessageBody: JSON.stringify(message),
QueueUrl: process.env.QUEUE_URL,
DelaySeconds: 5
}).promise()
return {
statusCode: 201,
body: JSON.stringify('Message sent!')
}
}
exports.consumer = async (event) => {
for (message of event.Records) {
const body = JSON.parse(message.body)
console.log(`Message received: ${body.message}`)
await sqs.deleteMessage({
QueueUrl: process.env.QUEUE_URL,
ReceiptHandle: message.receiptHandle
}).promise()
console.log(`Message ${message.messageId} deleted!`)
}
}
exports.listQueues = async (event) => {
const { QueueUrls } = await sqs.listQueues().promise()
return {
statusCode: 200,
body: JSON.stringify({
queues: QueueUrls
})
}
}