CDK Cloning in DZ CLI
The CDK command in DZ CLI is for use with AWS CDK and AWS CloudFormation stack applications. It allows developers to create ephemeral stacks from a base deployment.
Here's a guide to getting started with CDK in CLI:
Create a DevZero account and Install DZ CLI #
- You can create a DevZero account following the guide here
- You can install the DZ CLI installation guide here or if you are using homebrew or chocoltey, follow the guide here
Accessing CloudFormation Utilities #
- The dz cdk command is used to access the CloudFormation utilities.
- The available sub-commands are:
- delete: delete an ephemeral CDK instance
- deploy: deploy an ephemeral CDK stack
- deployCFn: deploy an ephemeral stack from a CloudFormation template
- listStacks: list all ephemeral CDK stacks
- Check here for full CDK CLI reference
Deploy an Ephemeral CDK Stack #
To deploy an ephemeral CDK stack, use the following command:
dz cdk deploy -f /path/to/main/stack/class
- This command will compile and upload a new CDK app, mirroring the production version, containing the developer's local changes.
- The stack name will be similar to the production stack name with a random character string appended to the end.
- The command will return a link to the application in the AWS CloudFormation account matching the default profile defined in the user's settings.
- It will also return a list of outputs created by the deploy command, a link to the API gateway URI for an example.
- The ephemeral CDK deployment is cached locally. If the developer re-runs the same command, the previous ephemeral CDK stack will be deleted after the new one is successfully deployed.
Example: An organization has a CDK app checked into GitHub that represents a core service they use. It consists of a DynamoDB database, Lambda function, and API Gateway instance. A developer needs to make an adjustment to the behavior of the Lambda function. With the base CDK stack deployed in production as org1cdkapp, the developer will check out the repository, make their changes locally, and run
dz cdk deploy -f ./path-to-main-stack-class.
This command will compile and upload a new CDK app mirroring the production version, containing the developers local changes. The stack name will be similar to org1cdkappfkel. Note the random char string appended to the end of the original stack name. The command will return a link to the application in the AWS CloudFormation account matching the default profile defined in the user's settings. It will also return a list of outputs created by the deploy command, which in this example case would be a link to the API Gateway URI. Additionally, the ephemeral CDK deployment is cached locally. If the developer were to re-run
dz cdk deploy -f ... again,
the previous ephemeral CDK stack would be deleted after the new one was successfully deployed.
Deploy an Ephemeral Stack from a CloudFormation Template #
To deploy an ephemeral stack from a CloudFormation template, use the following command:
dz cdk deployCFn -f /path/to/cloudformation/template
- This command will bundle, package, and upload the developer's local lambda files (ensuring any local changes they have made are included) to an S3 bucket, whose location will be written into the ephemeral CloudFormation stack template.
- The result of this command is an ephemeral CloudFormation stack, similar to the cdk deploy command.
Example: An organization not using AWS CDK has an AWS Cloudformation stack created that represents a core service they use. It contains the same elements in the above example. Since they’re not using CDK, we need to programatically edit their cloudformation template in order to create an ephemeral stack. Treating the template as a manifest file, they have been previously instructed to edit the code such that the lambda resource in the template JSON points to the location of the lambda code in their workspace.
"MyLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "src", <-- this is the folder location in repo where the lambda is
"S3Key": "" <-- this can be anything, it gets overwritten
},
}
From the root of the repository containing the lambda code, a developer will run a command similar to
cdk deployCFn -f /Org-app/template-manifest.json
This command will bundle, package, and upload the developers local lambda files (ensuring any local changes they have made are included) to an S3 bucket, who’s location will be written into the ephemeral cloudformation stack template which also gets uploaded to AWS. The result of this command is an ephemeral cloudformation stack, similar to the cdk deploy command.
Congratulations! You have successfully deployed an ephemeral CDK stack or an ephemeral stack from a CloudFormation template using the DZ CLI with CDK cloning functionality.