-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CMDCT-4238 - Prevent destroy from destroying serverless stacks unless…
… particular resources are set to retain (#15026) Co-authored-by: Pete Dunlap <[email protected]>
- Loading branch information
1 parent
d28e9bc
commit 5bd8e40
Showing
4 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { | ||
CloudFormationClient, | ||
GetTemplateCommand, | ||
paginateDescribeStacks, | ||
} from "@aws-sdk/client-cloudformation"; | ||
|
||
type Tag = { | ||
Key: string; | ||
Value: string; | ||
}; | ||
|
||
async function getAllStacksForRegion(region: string) { | ||
const client = new CloudFormationClient({ region: region }); | ||
const stacks = []; | ||
for await (const page of paginateDescribeStacks({ client }, {})) { | ||
stacks.push(...(page.Stacks || [])); | ||
} | ||
return stacks; | ||
} | ||
|
||
export async function getAllStacksForStage( | ||
region: string, | ||
stage: string, | ||
addFilters?: Tag[] | ||
) { | ||
let stacks = await getAllStacksForRegion(region); | ||
const matchTags = [ | ||
{ | ||
Key: "STAGE", | ||
Value: stage, | ||
}, | ||
]; | ||
matchTags.push(...(addFilters || [])); | ||
for (let matchTag of matchTags) { | ||
stacks = stacks.filter((i) => | ||
i.Tags?.find((j) => j.Key == matchTag.Key && j.Value == matchTag.Value) | ||
); | ||
} | ||
return stacks; | ||
} | ||
|
||
export async function getCloudFormationTemplatesForStage( | ||
region: string, | ||
stage: string, | ||
filters?: Tag[] | ||
): Promise<Record<string, any>> { | ||
const stacks = await getAllStacksForStage(region, stage, filters); | ||
|
||
// If no stacks found, return an empty object | ||
if (stacks.length === 0) { | ||
return {}; | ||
} | ||
|
||
const cfnClient = new CloudFormationClient({ region }); | ||
const templatesByStack: Record<string, any> = {}; | ||
|
||
// For each matching stack, retrieve and parse its template | ||
for (const stack of stacks) { | ||
const { TemplateBody } = await cfnClient.send( | ||
new GetTemplateCommand({ | ||
StackName: stack.StackName, | ||
}) | ||
); | ||
|
||
let parsedTemplate: any; | ||
try { | ||
parsedTemplate = JSON.parse(TemplateBody ?? ""); | ||
} catch { | ||
console.log("error, received yaml, need to update to handle"); | ||
console.log(TemplateBody); | ||
} | ||
|
||
templatesByStack[stack.StackName!] = parsedTemplate; | ||
} | ||
|
||
return templatesByStack; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters