Skip to content

Commit

Permalink
Mkae sure the lambda checks 0 messages and sets desired count to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
aloftus23 committed Nov 22, 2023
1 parent 5a87acf commit 94124e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
20 changes: 19 additions & 1 deletion backend/src/tasks/scanExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ export const handler: Handler = async (event) => {
} else {
console.log('Shodan is the only script type available right now.');
}

// After processing each message, check if the SQS queue is empty
const sqsAttributes = await sqs
.getQueueAttributes({
QueueUrl: process.env.SHODAN_QUEUE_URL!,
AttributeNames: ['ApproximateNumberOfMessages']
})
.promise();

const approximateNumberOfMessages = parseInt(
sqsAttributes.Attributes?.ApproximateNumberOfMessages || '0',
10
);

// If the queue is empty, scale down to zero tasks
if (approximateNumberOfMessages === 0) {
await startFargateTask(clusterName, process.env.SHODAN_SERVICE_NAME!, 0);
}
} catch (error) {
console.error(error);
return {
Expand Down Expand Up @@ -59,7 +77,7 @@ export async function startFargateTask(
const service = serviceDescription.services[0];

// Check if the desired task count is less than # provided
if (service.desiredCount! < desiredCountNum) {
if (service.desiredCount! !== desiredCountNum) {
const updateServiceParams = {
cluster: clusterName,
service: serviceName,
Expand Down
17 changes: 15 additions & 2 deletions infrastructure/pe_worker.tf
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,20 @@ resource "aws_ecs_service" "shodan_service" {
launch_type = "FARGATE"
desired_count = 0 # Initially set to 0, plan to start it dynamically
network_configuration {
subnets = aws_subnet.worker.*.id
security_groups = [aws_security_group.worker.id]
subnets = aws_subnet.worker.id
security_groups = [aws_security_group.worker.id]
assign_public_ip = true
}
}

# Create the log group
resource "aws_cloudwatch_log_group" "worker" {
name = var.pe_worker_ecs_log_group_name # should match awslogs-group in service.json
retention_in_days = 3653
kms_key_id = aws_kms_key.key.arn
tags = {
Project = var.project
Stage = var.stage
Owner = "Crossfeed managed resource"
}
}

0 comments on commit 94124e0

Please sign in to comment.