Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use aws `cdk synth` output via cloudformation

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.

like image 885
sumek Avatar asked Jun 10 '20 13:06

sumek


People also ask

Does CDK deploy synth?

The cdk synth generates a perfectly valid AWS CloudFormation template. You could take it and deploy it using the AWS CloudFormation console or another tool.

Does AWS CDK use CloudFormation?

AWS CDK is available to define and deploy AWS resources in all public regions. Since AWS CDK leverages the CloudFormation service, refer to Regional Products and Services for details about specific resource availability per AWS Region.

Does CDK deploy run CDK synth?

Conclusion # The cdk deploy command generates and then deploys the CloudFormation equivalent of our CDK code. Note that it's not necessary to run the cdk synth command before we run cdk deploy , because the CDK CLI will automatically synthesize a CloudFormation template for each stack before deployment.

What is a CDK in AWS CloudFormation?

AWS CDK apps are effectively only a definition of your infrastructure using code. When CDK apps are executed, they produce (or “ synthesize ”, in CDK parlance) an AWS CloudFormation template for each stack defined in your application. To synthesize a CDK app, use the cdk synth command.

Can I use a CDK synth with CloudFormation?

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. I would generally not recommend to use the output of cdk synth via CloudFormation except you know what you are doing very well.

How to create outputs in AWS CDK?

In order to create Outputs in CDK we use the CfnOutput construct. Outputs are values that we can import into other stacks or simply redirect to a file on the local file system. For instance, we can output the name of an S3 bucket or the domain name of an API. In order to define Outputs in AWS CDK we use the CfnOutput construct.

How do I synthesize a CDK template?

Synthesize a template from your app AWS CDK apps are effectively only a definition of your infrastructure using code. When CDK apps are executed, they produce (or “ synthesize ”, in CDK parlance) an AWS CloudFormation template for each stack defined in your application. To synthesize a CDK app, use the cdk synth command.


2 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

like image 136
Martin Löper Avatar answered Oct 13 '22 01:10

Martin Löper


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

like image 36
Daniel Scott Avatar answered Oct 13 '22 00:10

Daniel Scott