Skip to content

Commit

Permalink
feat: add UsePreviousTemplate option
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Jun 28, 2024
1 parent 5b4c796 commit af23fd4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
24 changes: 20 additions & 4 deletions src/Deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default class Deployer {
async createChangeSet({
StackName,
TemplateBody,
UsePreviousTemplate,
ImportExistingResources,
Parameters,
Capabilities,
Expand All @@ -70,7 +71,8 @@ export default class Deployer {
Tags,
}: {
StackName: string
TemplateBody: string | Buffer | (() => Readable)
TemplateBody?: string | Buffer | (() => Readable)
UsePreviousTemplate?: boolean
ImportExistingResources?: boolean
Parameters?: Parameter[]
Capabilities?: Capability[]
Expand Down Expand Up @@ -111,6 +113,7 @@ export default class Deployer {
NotificationARNs?: string[]
TemplateURL?: string
TemplateBody?: string
UsePreviousTemplate?: boolean
ChangeSetType: ChangeSetType
ImportExistingResources?: boolean
Parameters?: Parameter[]
Expand All @@ -128,9 +131,15 @@ export default class Deployer {
Tags,
}

if (TemplateBody && UsePreviousTemplate) {
throw new Error(
`Passing both TemplateBody and UsePreviousTemplate is not allowed`
)
}

// If an S3 uploader is available, use TemplateURL to deploy rather than
// TemplateBody. This is required for large templates.
if (s3Uploader) {
if (s3Uploader && TemplateBody) {
const url = await s3Uploader.uploadWithDedup({
Body: TemplateBody,
extension: 'template',
Expand All @@ -141,8 +150,12 @@ export default class Deployer {
throw new Error(
'TemplateBody: () => stream.Readable is not supported without s3Uploader option'
)
} else {
} else if (TemplateBody) {
params.TemplateBody = TemplateBody.toString()
} else if (UsePreviousTemplate) {
params.UsePreviousTemplate = true
} else {
throw new Error(`Must provide TemplateBody or UsePrevioiusTemplate`)
}
if (RoleARN) params.RoleARN = RoleARN
if (NotificationARNs) params.NotificationARNs = NotificationARNs
Expand Down Expand Up @@ -275,6 +288,7 @@ export default class Deployer {
async createAndWaitForChangeSet({
StackName,
TemplateBody,
UsePreviousTemplate,
ImportExistingResources,
Parameters,
Capabilities,
Expand All @@ -284,7 +298,8 @@ export default class Deployer {
Tags,
}: {
StackName: string
TemplateBody: string | Buffer | (() => Readable)
TemplateBody?: string | Buffer | (() => Readable)
UsePreviousTemplate?: boolean
ImportExistingResources?: boolean
Parameters?: Parameter[]
Capabilities?: Capability[]
Expand All @@ -296,6 +311,7 @@ export default class Deployer {
const { ChangeSetName, ChangeSetType } = await this.createChangeSet({
StackName,
TemplateBody,
UsePreviousTemplate,
ImportExistingResources,
Parameters,
Capabilities,
Expand Down
20 changes: 18 additions & 2 deletions src/deployCloudFormationStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type DeployCloudFormationStackInput<
Template?: Template
TemplateFile?: string
TemplateBody?: string | Buffer | (() => Readable)
UsePreviousTemplate?: boolean
BlanketDeletionPolicy?: 'Delete' | 'Retain'
StackPolicy?: SetStackPolicyCommandInput['StackPolicyBody'] | object
Parameters?: CloudFormationTemplateParameterValues<Template> | Parameter[]
Expand Down Expand Up @@ -89,6 +90,7 @@ export default async function deployCloudFormationStack<
Template,
TemplateFile,
TemplateBody,
UsePreviousTemplate,
BlanketDeletionPolicy,
StackPolicy,
Parameters: _Parameters,
Expand Down Expand Up @@ -144,6 +146,17 @@ export default async function deployCloudFormationStack<
`BlankDeletionPolicy can only be used together with Template.`
)
}
if (
(TemplateBody != null ? 1 : 0) +
(Template != null ? 1 : 0) +
(TemplateFile != null ? 1 : 0) +
(UsePreviousTemplate ? 1 : 0) >
1
) {
throw new Error(
`Passing two of the following at the same time is not allowed: Template, TemplateFile, TemplateBody, or UsePreviousTemplate`
)
}
if (!TemplateBody) {
if (Template) {
if (BlanketDeletionPolicy) {
Expand All @@ -156,8 +169,10 @@ export default async function deployCloudFormationStack<
TemplateBody = s3Uploader
? () => fs.createReadStream(TemplateFile, 'utf8')
: await fs.readFile(TemplateFile, 'utf8')
} else {
throw new Error(`Template, TemplateFile or TemplateBody is required`)
} else if (!UsePreviousTemplate) {
throw new Error(
`Template, TemplateFile, TemplateBody, or UsePreviousTemplate is required`
)
}
}
async function watchDuring<R>(procedure: () => Promise<R>): Promise<R> {
Expand Down Expand Up @@ -333,6 +348,7 @@ export default async function deployCloudFormationStack<
StackName,
ImportExistingResources,
TemplateBody,
UsePreviousTemplate,
Parameters,
Capabilities,
RoleARN,
Expand Down

0 comments on commit af23fd4

Please sign in to comment.