Skip to content

Commit

Permalink
feat: add listStackResources
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jun 4, 2024
1 parent 21a9986 commit aca9a9f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './cfnResponse.js'
export * from './stackOutput.js'
export * from './toObject.js'
export * from './listStackResources.js'
48 changes: 48 additions & 0 deletions src/listStackResources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
ListStackResourcesCommand,
type CloudFormationClient,
} from '@aws-sdk/client-cloudformation'

/**
* Returns the resources of a stack that match the given resource type.
*/
export const listStackResources = async (
cf: CloudFormationClient,
stackName: string,
ResourceType: string | Array<string>, // e.g. 'AWS::Lambda::Function'
resources: Array<{
PhysicalResourceId: string
LogicalResourceId: string
}> = [],
nextToken?: string,
): Promise<
Array<{
PhysicalResourceId: string
LogicalResourceId: string
}>
> => {
const { StackResourceSummaries, NextToken } = await cf.send(
new ListStackResourcesCommand({
StackName: stackName,
NextToken: nextToken,
}),
)

resources.push(
...(StackResourceSummaries ?? [])
.filter((res) =>
Array.isArray(ResourceType)
? ResourceType.includes(res.ResourceType ?? '')
: res.ResourceType === ResourceType,
)
.map(({ LogicalResourceId, PhysicalResourceId }) => ({
PhysicalResourceId: PhysicalResourceId as string,
LogicalResourceId: LogicalResourceId as string,
})),
)

if (NextToken !== undefined)
return listStackResources(cf, stackName, ResourceType, resources, NextToken)

return resources
}

0 comments on commit aca9a9f

Please sign in to comment.