-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathpublisher.ts
More file actions
53 lines (47 loc) · 1.45 KB
/
publisher.ts
File metadata and controls
53 lines (47 loc) · 1.45 KB
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
50
51
52
53
import { APIGatewayEvent, ProxyResult } from 'aws-lambda';
import SQS from 'aws-sdk/clients/sqs';
import { v1 } from 'uuid';
import { generateSQSQueueUrlFromArn, getOfflineSqsQueueUrl, isLocalHost } from './utils';
export const handlePublish = async (event: APIGatewayEvent): Promise<ProxyResult> => {
console.log('Publishing message to SQS queue from Lambda');
const sqsQueueUrl = generateSQSQueueUrlFromArn(process.env.FIFO_QUEUE_ARN);
if (!sqsQueueUrl) {
return {
statusCode: 404,
body: JSON.stringify('Queue not found'),
};
}
const id = v1();
const sqs = new SQS();
const url = isLocalHost(event) ? getOfflineSqsQueueUrl(sqsQueueUrl) : sqsQueueUrl;
try {
const result = await sqs
.sendMessage({
QueueUrl: url,
MessageBody: JSON.stringify({
id,
message: 'Hello from Lambda!',
}),
MessageDeduplicationId: id,
MessageGroupId: `Test-Group-${id}`,
MessageAttributes: {
id: {
DataType: 'String',
StringValue: id,
},
},
})
.promise();
console.info(`SQS publish result from Lambda: ${JSON.stringify(result, null, 2)}`);
} catch (error) {
console.error('Error:', error);
return {
statusCode: 500,
body: JSON.stringify({ message: 'Error publishing message to SQS' }),
};
}
return {
statusCode: 200,
body: JSON.stringify('Published message to SQS queue'),
};
};