Created
          November 7, 2024 11:50 
        
      - 
      
 - 
        
Save rujmah/90025d0303cc7c4a602f83b0428d366c to your computer and use it in GitHub Desktop.  
Revisions
- 
        
rujmah created this gist
Nov 7, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ // src/functions/readMessage.ts import { SQSClient, ReceiveMessageCommand, DeleteMessageCommand } from '@aws-sdk/client-sqs'; import type { APIGatewayProxyHandler } from 'aws-lambda'; const sqsReaderClient = new SQSClient({}); export const handler: APIGatewayProxyHandler = async () => { const QUEUE_URL = process.env.QUEUE_URL; if (!QUEUE_URL) { return { statusCode: 500, body: JSON.stringify({ error: 'Queue URL is required' }), }; } console.log(QUEUE_URL, 'QUEUE_URL'); try { const receiveCommand = new ReceiveMessageCommand({ QueueUrl: QUEUE_URL, MaxNumberOfMessages: 1, }); const result = await sqsReaderClient.send(receiveCommand); if (!result.Messages || result.Messages.length === 0) { return { statusCode: 404, body: JSON.stringify({ message: 'No messages available' }), }; } const message = result.Messages[0]; // Delete the message after reading const deleteCommand = new DeleteMessageCommand({ QueueUrl: QUEUE_URL, ReceiptHandle: message?.ReceiptHandle!, }); await sqsReaderClient.send(deleteCommand); return { statusCode: 200, body: JSON.stringify({ message: message?.Body }), }; } catch (error) { console.error('Error reading message:', error); return { statusCode: 500, body: JSON.stringify({ error: 'Failed to read message' }), }; } }; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ // src/functions/sendMessage.ts import { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs'; import type { APIGatewayProxyHandler } from 'aws-lambda'; const sqsClient = new SQSClient({}); export const handler: APIGatewayProxyHandler = async (event) => { const QUEUE_URL = process.env.QUEUE_URL; if (!QUEUE_URL) { return { statusCode: 500, body: JSON.stringify({ error: 'Queue URL is required' }), }; } console.log(QUEUE_URL, 'QUEUE_URL'); const body = JSON.parse(event.body || '{}'); const message = body.message; if (!message) { return { statusCode: 400, body: JSON.stringify({ error: 'Message is required' }), }; } try { const command = new SendMessageCommand({ QueueUrl: QUEUE_URL, MessageBody: message, }); await sqsClient.send(command); return { statusCode: 200, body: JSON.stringify({ success: true, message: 'Message sent successfully' }), }; } catch (error) { console.error('Error sending message:', error); return { statusCode: 500, body: JSON.stringify({ error: 'Failed to send message', msg: (error as Error).message, }), }; } }; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ import type { AWS } from '@serverless/typescript'; const region = (process.env.AWS_REGION || 'eu-west-1') as AWS['provider']['region']; const serverlessConfiguration: AWS = { service: 'sqs-serverless-api', frameworkVersion: '4', provider: { name: 'aws', stage: '${opt:stage, "dev"}', runtime: 'nodejs20.x', region, iamRoleStatements: [ { Effect: 'Allow', Action: [ 'sqs:SendMessage', 'sqs:ReceiveMessage', 'sqs:DeleteMessage', 'sqs:GetQueueAttributes', ], Resource: { 'Fn::GetAtt': ['MyTestQueue', 'Arn'], }, }, ], }, resources: { Resources: { MyTestQueue: { Type: 'AWS::SQS::Queue', Properties: { QueueName: '${self:service}-${self:provider.stage}-queue', }, }, }, }, functions: { sendMessage: { handler: 'src/functions/sendMessage.handler', events: [ { http: { method: 'post', path: 'send-message', }, }, ], environment: { QUEUE_URL: { 'Fn::GetAtt': ['MyTestQueue', 'QueueUrl'], }, }, }, readMessage: { handler: 'src/functions/readMessage.handler', events: [ { http: { method: 'get', path: 'read-message', }, }, ], environment: { QUEUE_URL: { 'Fn::GetAtt': ['MyTestQueue', 'QueueUrl'], }, }, }, }, }; module.exports = serverlessConfiguration;