Skip to content

Commit

Permalink
Use aws-sdk paginators for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Aug 9, 2023
1 parent cbf28a9 commit bcee793
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
20 changes: 7 additions & 13 deletions src/sdk/find-resource-ids.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
CloudFormationClient,
ListStackResourcesCommand,
paginateListStackResources,
} from '@aws-sdk/client-cloudformation';

export async function findResourceIds(
Expand All @@ -9,26 +9,20 @@ export async function findResourceIds(
const client = new CloudFormationClient({});
const resourceIds: string[] = [];

let nextToken: string | undefined;

do {
const output = await client.send(
new ListStackResourcesCommand({
StackName: stackName,
NextToken: nextToken,
}),
);
const paginator = paginateListStackResources(
{client},
{StackName: stackName},
);

for await (const output of paginator) {
if (output.StackResourceSummaries) {
for (const {PhysicalResourceId} of output.StackResourceSummaries) {
if (PhysicalResourceId) {
resourceIds.push(PhysicalResourceId);
}
}
}

nextToken = output.NextToken;
} while (nextToken);
}

return resourceIds;
}
30 changes: 14 additions & 16 deletions src/sdk/find-stacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,41 @@ import {findStack} from './find-stack.js';
import {getOutputValue} from './get-output-value.js';
import {
CloudFormationClient,
DescribeStacksCommand,
paginateDescribeStacks,
} from '@aws-sdk/client-cloudformation';

type NamedStack = Stack & {StackName: string};

export async function findStacks(
hostedZoneName?: string,
): Promise<readonly Stack[]> {
const client = new CloudFormationClient({});
const stacks: Stack[] = [];

let nextToken: string | undefined;

do {
const output = await client.send(
new DescribeStacksCommand({NextToken: nextToken}),
);
const stacks: NamedStack[] = [];
const paginator = paginateDescribeStacks({client}, {});

for await (const output of paginator) {
if (output.Stacks) {
stacks.push(...output.Stacks);
for (const stack of output.Stacks) {
if (isMatchingStack(stack, hostedZoneName)) {
stacks.push(stack);
}
}
}

nextToken = output.NextToken;
} while (nextToken);
}

return Promise.all(
stacks
.filter((stack) => isMatchingStack(stack, hostedZoneName))
// For some reason `stack.EnableTerminationProtection` is always
// `undefined`. Describing a single stack instead, does return the correct
// value (true or false) for `EnableTerminationProtection`.
.map(async ({StackName}) => findStack(StackName!)),
.map(async ({StackName}) => findStack(StackName)),
);
}

function isMatchingStack(
stack: Stack,
hostedZoneName: string | undefined,
): boolean {
): stack is NamedStack {
if (!stack.StackName?.startsWith(`aws-simple`)) {
return false;
}
Expand Down

0 comments on commit bcee793

Please sign in to comment.