From 1dddd8a641fd36e149d23da648c44768010111d6 Mon Sep 17 00:00:00 2001 From: Romain Marcadier-Muller Date: Wed, 26 Sep 2018 14:01:42 +0200 Subject: [PATCH] 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. --- packages/aws-cdk/lib/api/deploy-stack.ts | 27 +++--------------------- 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/packages/aws-cdk/lib/api/deploy-stack.ts b/packages/aws-cdk/lib/api/deploy-stack.ts index f4cb93c38f865..2ac0bf8b8a71b 100644 --- a/packages/aws-cdk/lib/api/deploy-stack.ts +++ b/packages/aws-cdk/lib/api/deploy-stack.ts @@ -41,17 +41,14 @@ export async function deployStack(stack: cxapi.SynthesizedStack, const cfn = await sdk.cloudFormation(stack.environment, Mode.ForWriting); const bodyParameter = await makeBodyParameter(stack, toolkitInfo); - if (!await stackExists(cfn, deployName)) { - await createEmptyStack(cfn, deployName, quiet); - } else { - debug('Stack named %s already exists, updating it!', deployName); - } + const update = await stackExists(cfn, deployName); const changeSetName = `CDK-${executionId}`; - debug('Attempting to create ChangeSet %s on stack %s', changeSetName, deployName); + debug(`Attempting to create ChangeSet ${changeSetName} to ${update ? 'update' : 'create'} stack ${deployName}`); const changeSet = await cfn.createChangeSet({ StackName: deployName, ChangeSetName: changeSetName, + ChangeSetType: update ? 'UPDATE' : 'CREATE', Description: `CDK Changeset for execution ${executionId}`, TemplateBody: bodyParameter.TemplateBody, TemplateURL: bodyParameter.TemplateURL, @@ -87,24 +84,6 @@ async function getStackOutputs(cfn: aws.CloudFormation, stackName: string): Prom return result; } -async function createEmptyStack(cfn: aws.CloudFormation, stackName: string, quiet: boolean): Promise { - debug('Creating new empty stack named %s', stackName); - - const template = { - Resources: { - WaitCondition: { - Type: 'AWS::CloudFormation::WaitConditionHandle' - } - } - }; - - const response = await cfn.createStack({ StackName: stackName, TemplateBody: JSON.stringify(template, null, 2) }).promise(); - debug('CreateStack response: %j', response); - const monitor = quiet ? undefined : new StackActivityMonitor(cfn, stackName, undefined, 1).start(); - await waitForStack(cfn, stackName); - if (monitor) { monitor.stop(); } -} - /** * Prepares the body parameter for +CreateChangeSet+, putting the generated CloudFormation template in the toolkit-provided * S3 bucket if present, otherwise using in-line template argument. If no +ToolkitInfo+ is provided and the template is