Skip to content

Commit 00c7b7a

Browse files
andrew-nowakCopilot
andcommitted
More defensive programming on values returned from AWS API
Co-authored-by: Copilot <[email protected]>
1 parent d6ed006 commit 00c7b7a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/aws/autoscaling.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ export async function launchNewInstance(instance: Instance, asgName: string): Pr
3232
`getting current capacity in ${asgName}`,
3333
5,
3434
);
35+
if (!asgs.AutoScalingGroups || asgs.AutoScalingGroups.length === 0) {
36+
throw new Error(`No AutoScalingGroup found with name ${asgName}`);
37+
}
3538
const capacity = asgs.AutoScalingGroups[0].DesiredCapacity;
39+
if (typeof capacity !== 'number' || isNaN(capacity)) {
40+
throw new Error(`DesiredCapacity is not defined or not a number for ASG ${asgName}`);
41+
}
3642

3743
return retry(
3844
() => awsAutoscaling.send(new SetDesiredCapacityCommand({

src/getTargetNode.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import {Instance} from "./aws/types";
77
import { type AutoScalingGroup } from '@aws-sdk/client-auto-scaling';
88

99
function asgTagsToRecord(asg: AutoScalingGroup): Record<string, string> {
10-
return Object.fromEntries(asg.Tags.map(tag => ([tag.Key, tag.Value])));
10+
return Object.fromEntries(
11+
(asg.Tags ?? [])
12+
.filter(tag => tag.Key !== undefined && tag.Value !== undefined)
13+
.map(tag => [tag.Key!, tag.Value!])
14+
);
1115
}
1216

1317
/** attempt to find an ASG with the same tagging as the target instance's,

0 commit comments

Comments
 (0)