Skip to content

Commit

Permalink
feat(toolkit): Stop creating 'empty' stacks (#779)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RomainMuller authored Sep 26, 2018
1 parent f697876 commit 1dddd8a
Showing 1 changed file with 3 additions and 24 deletions.
27 changes: 3 additions & 24 deletions packages/aws-cdk/lib/api/deploy-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<void> {
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
Expand Down

0 comments on commit 1dddd8a

Please sign in to comment.