Skip to content

Commit

Permalink
feat(deployCloudFormationStack): add BlanketDeletionPolicy option
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed May 21, 2024
1 parent e8e5921 commit 4760a6c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 64 deletions.
60 changes: 38 additions & 22 deletions src/deployCloudFormationStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,7 @@ import {
} from '@aws-sdk/client-cloudformation'
import { S3Client } from '@aws-sdk/client-s3'

export default async function deployCloudFormationStack({
cloudformation: _cloudformation,
region,
awsConfig,
approve,
StackName,
Template,
TemplateFile,
TemplateBody,
StackPolicy,
Parameters: _Parameters,
Capabilities,
RoleARN,
NotificationARNs,
Tags: _Tags,
s3,
readOutputs,
replaceIfCreateFailed,
logEvents = true,
}: {
export type DeployCloudFormationStackInput = {
cloudformation?: CloudFormationClient
region?: string
awsConfig?: CloudFormationClientConfig
Expand All @@ -56,6 +37,7 @@ export default async function deployCloudFormationStack({
Template?: any
TemplateFile?: string
TemplateBody?: string | Buffer | (() => Readable)
BlanketDeletionPolicy?: 'Delete' | 'Retain'
StackPolicy?: SetStackPolicyCommandInput['StackPolicyBody']
Parameters?:
| {
Expand All @@ -79,14 +61,38 @@ export default async function deployCloudFormationStack({
logEvents?: Writable | boolean
readOutputs?: boolean
replaceIfCreateFailed?: boolean
}): Promise<{
}

export type DeployCloudFormationStackOutput = {
ChangeSetName: string
ChangeSetType: string
HasChanges: boolean
Outputs: {
[resourceName: string]: string
}
}> {
}

export default async function deployCloudFormationStack({
cloudformation: _cloudformation,
region,
awsConfig,
approve,
StackName,
Template,
TemplateFile,
TemplateBody,
BlanketDeletionPolicy,
StackPolicy,
Parameters: _Parameters,
Capabilities,
RoleARN,
NotificationARNs,
Tags: _Tags,
s3,
readOutputs,
replaceIfCreateFailed,
logEvents = true,
}: DeployCloudFormationStackInput): Promise<DeployCloudFormationStackOutput> {
if (!StackName) throw new Error('missing StackName')
if (!awsConfig)
awsConfig = {
Expand Down Expand Up @@ -122,8 +128,18 @@ export default async function deployCloudFormationStack({
s3: new S3Client(awsConfig),
})
: undefined
if (BlanketDeletionPolicy && !Template) {
throw new Error(
`BlankDeletionPolicy can only be used together with Template.`
)
}
if (!TemplateBody) {
if (Template) {
if (BlanketDeletionPolicy) {
for (const key in Template.Resources) {
Template.Resources[key].DeletionPolicy = BlanketDeletionPolicy
}
}
TemplateBody = JSON.stringify(Template, null, 2)
} else if (TemplateFile) {
TemplateBody = s3Uploader
Expand Down
49 changes: 8 additions & 41 deletions src/deployCloudFormationStacks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {
Capability,
CloudFormationClient,
CloudFormationClientConfig,
Parameter,
SetStackPolicyCommandInput,
Tag,
} from '@aws-sdk/client-cloudformation'
import deployCloudFormationStack from './deployCloudFormationStack'
import { Readable, Writable } from 'stream'
import deployCloudFormationStack, {
DeployCloudFormationStackInput,
DeployCloudFormationStackOutput,
} from './deployCloudFormationStack'

export default function deployCloudFormationStacks({
awsConfig,
Expand All @@ -23,41 +21,10 @@ export default function deployCloudFormationStacks({
SSEKMSKeyId?: string
forceUpload?: boolean
}
stacks: ReadonlyArray<{
region?: string
awsConfig?: CloudFormationClientConfig
StackName: string
Template?: any
TemplateFile?: string
TemplateBody?: Buffer | string | (() => Readable)
StackPolicy?: SetStackPolicyCommandInput['StackPolicyBody']
Parameters?:
| {
[key: string]: Parameter['ParameterValue']
}
| Parameter[]
Capabilities?: Capability[]
RoleARN?: string
NotificationARNs?: string[]
Tags?:
| {
[key: string]: Tag['Value']
}
| Tag[]
readOutputs?: boolean
replaceIfCreateFailed?: boolean
logEvents?: Writable | boolean
}>
}): Promise<
Array<{
ChangeSetName: string
ChangeSetType: string
HasChanges: boolean
Outputs: {
[resourceName: string]: string
}
}>
> {
stacks: ReadonlyArray<
Omit<DeployCloudFormationStackInput, 'cloudformation' | 'approve' | 's3'>
>
}): Promise<Array<DeployCloudFormationStackOutput>> {
return Promise.all(
stacks.map((stack) =>
deployCloudFormationStack({
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export { default as deployCloudFormationStack } from './deployCloudFormationStack'
export {
default as deployCloudFormationStack,
type DeployCloudFormationStackInput,
type DeployCloudFormationStackOutput,
} from './deployCloudFormationStack'
export { default as deployCloudFormationStacks } from './deployCloudFormationStacks'
export { default as describeCloudFormationFailure } from './describeCloudFormationFailure'
export { copyECRImage } from './ecr'
Expand Down

0 comments on commit 4760a6c

Please sign in to comment.