Skip to content

Commit

Permalink
feat(cli): pass cloudformation parameters to "cdk deploy"
Browse files Browse the repository at this point in the history
Adding integration test for nested stacks.

Adding section in README.
  • Loading branch information
Viphor committed Feb 27, 2020
1 parent dc5a0f3 commit df63489
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/aws-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ currently deployed stack to the template that is about to be deployed and will
skip deployment if they are identical. Use `--force` to override this behavior
and always deploy the stack.

In order to pass parameters to your template during deployment, you can use `--parameters
(STACK:KEY=VALUE)`. This will apply the value `VALUE` to the key `KEY` for stack `STACK`. The stack
name can either be `*` or omitted in order to apply to all stacks.
Note: The parameters will not propagate to NestedStacks. These has to be sent with the constructor
of these.

#### `cdk destroy`
Deletes a stack from it's environment. This will cause the resources in the stack to be destroyed (unless they were
configured with a `DeletionPolicy` of `Retain`). During the stack destruction, the command will output progress
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function parseCommandLineArguments() {
.option('tags', { type: 'array', alias: 't', desc: 'Tags to add to the stack (KEY=VALUE)', nargs: 1, requiresArg: true })
.option('execute', { type: 'boolean', desc: 'Whether to execute ChangeSet (--no-execute will NOT execute the ChangeSet)', default: true })
.option('force', { alias: 'f', type: 'boolean', desc: 'Always deploy stack even if templates are identical', default: false })
.option('parameters', { type: 'array', alias: 'P', desc: 'Additional parameters passed to CloudFormation at deploy time (STACK:KEY=VALUE)', nargs: 1, requiresArg: true, default: {} })
.option('parameters', { type: 'array', desc: 'Additional parameters passed to CloudFormation at deploy time (STACK:KEY=VALUE)', nargs: 1, requiresArg: true, default: {} })
)
.command('destroy [STACKS..]', 'Destroy the stack(s) named STACKS', yargs => yargs
.option('exclusively', { type: 'boolean', alias: 'e', desc: 'Only destroy requested stacks, don\'t include dependees' })
Expand Down
3 changes: 2 additions & 1 deletion packages/aws-cdk/test/integ/cli/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const iam = require('@aws-cdk/aws-iam');
const sns = require('@aws-cdk/aws-sns');
const lambda = require('@aws-cdk/aws-lambda');
const docker = require('@aws-cdk/aws-ecr-assets');
const { StackWithNestedStack } = require('./nested-stack');
const { StackWithNestedStack, StackWithNestedStackUsingParameters } = require('./nested-stack');

const stackPrefix = process.env.STACK_NAME_PREFIX || 'cdk-toolkit-integration';

Expand Down Expand Up @@ -204,5 +204,6 @@ if (process.env.ENABLE_VPC_TESTING) { // Gating so we don't do context fetching
new ConditionalResourceStack(app, `${stackPrefix}-conditional-resource`)

new StackWithNestedStack(app, `${stackPrefix}-with-nested-stack`);
new StackWithNestedStackUsingParameters(app, `${stackPrefix}-with-nested-stack-using-parameters`);

app.synth();
25 changes: 23 additions & 2 deletions packages/aws-cdk/test/integ/cli/app/nested-stack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const cfn = require('@aws-cdk/aws-cloudformation');
const sns = require('@aws-cdk/aws-sns');
const { Stack } = require('@aws-cdk/core');
const { Stack, CfnParameter } = require('@aws-cdk/core');

class StackWithNestedStack extends Stack {
constructor(scope, id) {
Expand All @@ -17,4 +17,25 @@ class MyNestedStack extends cfn.NestedStack {
}
}

exports.StackWithNestedStack = StackWithNestedStack;
class StackWithNestedStackUsingParameters extends Stack {
constructor(scope, id) {
super(scope, id);
new CfnParameter(this, 'MyTopicParam');
new MyNestedStackUsingParameters(this, 'MyNested', {
parameters: {'MyTopicParam': 'ThereIsASpoon'}
});
}
}

class MyNestedStackUsingParameters extends cfn.NestedStack {
constructor(scope, id, props) {
super(scope, id, props);

new sns.Topic(this, 'MyTopic', {
topicName: new CfnParameter(this, 'MyTopicParam')
});
}
}

exports.StackWithNestedStack = StackWithNestedStack;
exports.StackWithNestedStackUsingParameters = StackWithNestedStackUsingParameters;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -euo pipefail
scriptdir=$(cd $(dirname $0) && pwd)
source ${scriptdir}/common.bash
# ----------------------------------------------------------

setup

stack_arn=$(cdk deploy -v ${STACK_NAME_PREFIX}-with-nested-stack-using-parameters --parameters "MyTopicParam=ThereIsNoSpoon")
echo "Stack deployed successfully"

# verify that we only deployed a single stack (there's a single ARN in the output)
assert_lines "${stack_arn}" 1

# verify the number of resources in the stack
response_json=$(mktemp).json
aws cloudformation describe-stack-resources --stack-name ${stack_arn} > ${response_json}
resource_count=$(node -e "console.log(require('${response_json}').StackResources.length)")
if [ "${resource_count}" -ne 1 ]; then
fail "stack has ${resource_count} resources, and we expected two"
fi

# destroy
cdk destroy -f ${STACK_NAME_PREFIX}-with-nested-stack-using-parameters

echo "✅ success"
1 change: 1 addition & 0 deletions packages/aws-cdk/test/integ/cli/test-cdk-ls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ${STACK_NAME_PREFIX}-param-test-2
${STACK_NAME_PREFIX}-test-1
${STACK_NAME_PREFIX}-test-2
${STACK_NAME_PREFIX}-with-nested-stack
${STACK_NAME_PREFIX}-with-nested-stack-using-parameters
${STACK_NAME_PREFIX}-order-consuming
HERE

Expand Down

0 comments on commit df63489

Please sign in to comment.