Skip to content

Commit

Permalink
fix(ecs-task-monitor): upgrade to AWS SDK v3 (#1457)
Browse files Browse the repository at this point in the history
Refactor the ecs-task-monitor lambda functions to use AWS SDK v3.


----

*By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license*
  • Loading branch information
mrgrain authored Sep 13, 2024
1 parent cec1dea commit 622464b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/__tests__/__snapshots__/construct-hub.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/__tests__/devapp/__snapshots__/snapshot.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 28 additions & 26 deletions src/backend/ecs-task-monitor/monitor.lambda.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {
DescribeTasksCommand,
ListTasksCommand,
StopTaskCommand,
} from '@aws-sdk/client-ecs';
import { metricScope, Configuration, Unit } from 'aws-embedded-metrics';
import { Context, ScheduledEvent } from 'aws-lambda';
import { Environment, METRICS_NAMESPACE, MetricName } from './constants';
import * as aws from '../shared/aws.lambda-shared';
import { ECS_CLIENT } from '../shared/aws.lambda-shared';
import { requireEnv } from '../shared/env.lambda-shared';

Configuration.namespace = METRICS_NAMESPACE;
Expand All @@ -16,35 +21,33 @@ export const handler = metricScope(
const CLUSTER_NAME = requireEnv(Environment.CLUSTER_NAME);
const TIMEOUT_MILLIS = parseInt(requireEnv(Environment.TIMEOUT_MILLIS), 10);

const ecs = aws.ecs();

const cutOffTime = Date.now() - TIMEOUT_MILLIS;
let theNextToken: string | undefined;
let killCount = 0;
try {
let taskCount = 0;
do {
const { nextToken, taskArns } = await ecs
.listTasks({
const { nextToken, taskArns } = await ECS_CLIENT.send(
new ListTasksCommand({
cluster: CLUSTER_NAME,
desiredStatus: 'RUNNING',
maxResults: 100,
nextToken: theNextToken,
})
.promise();
);
theNextToken = nextToken;

if (taskArns == null) {
continue;
}
taskCount += taskArns.length;

const { failures, tasks } = await ecs
.describeTasks({
const { failures, tasks } = await ECS_CLIENT.send(
new DescribeTasksCommand({
cluster: CLUSTER_NAME,
tasks: taskArns,
})
.promise();
);

if (failures != null && failures.length > 0) {
throw new Error(
Expand Down Expand Up @@ -75,23 +78,22 @@ export const handler = metricScope(
}

await Promise.all(
Array.from(toTerminate).map((task) =>
ecs
.stopTask({
cluster: CLUSTER_NAME,
task,
reason: `Terminated by ${context.functionName} (${context.awsRequestId}): Task timed out`,
})
.promise()
.then(
() => console.log(`SUCCESS: Terminated ${task}`),
(error) =>
console.error(
`WARNING: Failed to terminate ${task}: ${error}`
)
)
.finally(() => (killCount += 1))
)
Array.from(toTerminate).map(async (task) => {
try {
await ECS_CLIENT.send(
new StopTaskCommand({
cluster: CLUSTER_NAME,
task,
reason: `Terminated by ${context.functionName} (${context.awsRequestId}): Task timed out`,
})
);
console.log(`SUCCESS: Terminated ${task}`);
} catch (error) {
console.error(`WARNING: Failed to terminate ${task}: ${error}`);
} finally {
killCount += 1;
}
})
);
} while (theNextToken != null);

Expand Down

0 comments on commit 622464b

Please sign in to comment.