Skip to content

Commit

Permalink
Add handler and more logging to SQS lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Oct 5, 2023
1 parent ede6b97 commit e5f0654
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions backend/src/tasks/scanExecution.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { SQSEvent, SQSRecord } from 'aws-lambda';
import { Handler, SQSRecord } from 'aws-lambda';
import * as AWS from 'aws-sdk';

const ecs = new AWS.ECS();
const sqs = new AWS.SQS();

export const invokeFargateTask = async (event: SQSEvent): Promise<any> => {
export const handler: Handler = async (event) => {
try {
// Get the SQS record and message body
const sqsRecord: SQSRecord = event.Records[0];
const commandOptions: string = sqsRecord.body;

console.log(commandOptions);

// Get the ARN of the SQS queue from the event
const sqsQueueArn: string | undefined = sqsRecord.eventSourceARN;

Expand All @@ -24,11 +27,12 @@ export const invokeFargateTask = async (event: SQSEvent): Promise<any> => {
.getQueueAttributes(sqsQueue)
.promise();
const sqsQueueUrl = queueAttributesResponse.Attributes?.QueueUrl;

console.log(sqsQueueUrl);
if (!sqsQueueUrl) {
throw new Error('SQS Queue URL not found');
}

// Run command in queue message in Fargate
const params: AWS.ECS.RunTaskRequest = {
cluster: process.env.FARGATE_CLUSTER_NAME!,
taskDefinition: process.env.FARGATE_TASK_DEFINITION_NAME!,
Expand All @@ -50,7 +54,6 @@ export const invokeFargateTask = async (event: SQSEvent): Promise<any> => {
]
}
};

const data = await ecs.runTask(params).promise();
console.log('Fargate task started:', data);

Expand Down

0 comments on commit e5f0654

Please sign in to comment.