Skip to content

Commit 1dddd8a

Browse files
authored
feat(toolkit): Stop creating 'empty' stacks (#779)
When deploying a stack for the first time in an environment, the toolkit would until now create the stack with a dummy resource (a WaitConditionHandle), so it could then prepare & execute a ChangeSet. It turns out it is possible to use a CHangeSet to create a stack as well, by passing a CHangeSetType parameter to the CreateChangeSet API. This commit changes the toolkit to use this feature, so that the first deployment of a stack is going to behave a lot more consistently compared to the subsequent update deployments. It also removes quite a bit of code.
1 parent f697876 commit 1dddd8a

File tree

1 file changed

+3
-24
lines changed

1 file changed

+3
-24
lines changed

packages/aws-cdk/lib/api/deploy-stack.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,14 @@ export async function deployStack(stack: cxapi.SynthesizedStack,
4141
const cfn = await sdk.cloudFormation(stack.environment, Mode.ForWriting);
4242
const bodyParameter = await makeBodyParameter(stack, toolkitInfo);
4343

44-
if (!await stackExists(cfn, deployName)) {
45-
await createEmptyStack(cfn, deployName, quiet);
46-
} else {
47-
debug('Stack named %s already exists, updating it!', deployName);
48-
}
44+
const update = await stackExists(cfn, deployName);
4945

5046
const changeSetName = `CDK-${executionId}`;
51-
debug('Attempting to create ChangeSet %s on stack %s', changeSetName, deployName);
47+
debug(`Attempting to create ChangeSet ${changeSetName} to ${update ? 'update' : 'create'} stack ${deployName}`);
5248
const changeSet = await cfn.createChangeSet({
5349
StackName: deployName,
5450
ChangeSetName: changeSetName,
51+
ChangeSetType: update ? 'UPDATE' : 'CREATE',
5552
Description: `CDK Changeset for execution ${executionId}`,
5653
TemplateBody: bodyParameter.TemplateBody,
5754
TemplateURL: bodyParameter.TemplateURL,
@@ -87,24 +84,6 @@ async function getStackOutputs(cfn: aws.CloudFormation, stackName: string): Prom
8784
return result;
8885
}
8986

90-
async function createEmptyStack(cfn: aws.CloudFormation, stackName: string, quiet: boolean): Promise<void> {
91-
debug('Creating new empty stack named %s', stackName);
92-
93-
const template = {
94-
Resources: {
95-
WaitCondition: {
96-
Type: 'AWS::CloudFormation::WaitConditionHandle'
97-
}
98-
}
99-
};
100-
101-
const response = await cfn.createStack({ StackName: stackName, TemplateBody: JSON.stringify(template, null, 2) }).promise();
102-
debug('CreateStack response: %j', response);
103-
const monitor = quiet ? undefined : new StackActivityMonitor(cfn, stackName, undefined, 1).start();
104-
await waitForStack(cfn, stackName);
105-
if (monitor) { monitor.stop(); }
106-
}
107-
10887
/**
10988
* Prepares the body parameter for +CreateChangeSet+, putting the generated CloudFormation template in the toolkit-provided
11089
* S3 bucket if present, otherwise using in-line template argument. If no +ToolkitInfo+ is provided and the template is

0 commit comments

Comments
 (0)