I am working in an environment with existing Cloudformation based pipelines. I am wondering if it's possible to describe my infrastructure in CDK and then generate the Cloudformation to be used in the pipeline with no changes to the pipelines. I want the fact that I use CDK to be completely transparent.
-
1If i understood your question you are asking about pipeline which will run `cdk synth` grab the yaml the generated and use it as template ? – Amit Baranes Jun 11 '20 at 12:14
-
1I meant to run `cdk synth` before the commit, locally, possibly as a Git precommit hook I don't have possibility of changing the pipeline, so the fact of using cdk instead of cloudformation would have to be transparent to the pipeline – sumek Jun 11 '20 at 14:35
-
It's possible, i have an example using code build and code pipeline. code build listen to git repo when commit created new build started compile cdk and run cdk synth, later code pipeline deploy cdk stack. i guess you want to change to second part to your cloudformation based pipeline. does is it sounds a possible solution for you ? – Amit Baranes Jun 14 '20 at 21:36
-
keep in mind that, depending on your cdk stack, its not easily possible to call create-stack on a synthesized template because of CfnParameters which cant be resolved without some workarounds. – Logemann Jun 17 '20 at 09:44
3 Answers
I would generally not recommend to use the output of cdk synth
via CloudFormation except you know what you are doing very well.
Here is the reason why: There are some edge cases in which the CDK does bootstrapping and asset publishing in advance, e.g. for so called asset sources (docker images, s3 files, etc).
The topic has some overlap with issues in the CDK repository on GitHub asking about CI/CD integration. [1]
There is work in progress to develop a fully automated CI/CD process for the CDK [2]. The so called cloud assembly [3] contains all resources which are necessary to deploy via CloudFormation but as the RFC points out:
The cloud assembly includes a CloudFormation template for each stack and asset sources (docker images, s3 files, etc) that must be packaged and published to the asset store in each environment that consumes them.
If you are not using any assets or have the option to package and deploy them before using CloudFormation, using cdk synth output should be possible in CloudFormation when supplying the correct CFN params (as others have already pointed out in this thread).
References
[1] https://github.com/aws/aws-cdk/issues/6894
[2] https://github.com/aws/aws-cdk-rfcs/blob/master/text/0049-continuous-delivery.md
[3] https://github.com/aws/aws-cdk/blob/master/packages/@aws-cdk/cloud-assembly-schema/README.md

- 6,471
- 1
- 16
- 40
Yes, it is possible. I have a process which uses the CDK to 'build' the CloudFormation template using cdk synth
. This template is then uploaded into an S3 bucket on a versioned path.
You can then deploy the CloudFormation template from the bucket using the --template-url
option on create-stack
.
https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html

- 7,418
- 5
- 39
- 58
-
because synth resulted CfnTemplates can contain CfnParameters which dont have values. Like for instance an S3Bucket CfnParameter. They also dont have default values resulting in a stack which cant be deployed as is. – Logemann Jun 17 '20 at 10:10
-
Well, yes. But you'd have had to supply the parameter value during `cdk deploy`, so you need to do the same with a `create-stack`. – Daniel Scott Jun 17 '20 at 11:52
-
perfectly right. Just wanted to mention that just calling "as is" (means without params) might work or not. I fell for that some time ago. As a side note, you dont need to supply parameters to cdk deploy when CDK has put those params in there because CDK does it somehow internally. So its not that easy to catch. – Logemann Jun 17 '20 at 11:56
The output of cdk synth can be stored in a json.
build_spec=BuildSpec.from_object({
"version": "0.2",
"run-as": "root",
"phases": {
"install": {
"commands": [
'npm install -g aws-cdk',
'pip install -r requirements.txt',
]
},
"build": {
"commands": [
'cdk synth stack-dev --verbose --debug=true -o > output.json'
]
}
},
"artifacts": {
"files": "output.json"
},
})
cdk_build_action = CodeBuildAction(
action_name="CDKBuild",
project=cdk_build,
input=source_artifact,
outputs=[cdk_build_output]
)
This json can be forwarded to deploy stage with CloudFormationCreateUpdateStackAction
CloudFormationCreateUpdateStackAction(template_path=cdk_build_output.at_path("output.json"))

- 946
- 11
- 14