Skip to content

skorfmann/cdktf-hybrid-module

Repository files navigation

A Terraform CDK Construct which is usable as Terraform Module as well

This repo demonstratest the usage of a cdktf construct package as source for:

  • A native Terraform module
  • A construct consumable in cdktf distributed via NPM
  • A native Terraform module distributed via NPM ;)

While this hybrid use-case isn't explictly supported in the Terraform CDK (cdktf) at the moment, it's totally possible do achieve this already.

Getting started

git clone git@github.com:skorfmann/cdktf-hybrid-module.git
cd cdktf-hybrid-module
yarn
yarn build
cd examples/cdktf-example
$(yarn bin)/cdktf diff

NB: The last step (cdktf diff) expects valid AWS credentials in ENV

The general idea

  • Synthesize a stack without a provider und use the synthesized folder as module.
  • Release the construct as part of the npm package
  • As bonus, package the Terraform JSON as well, so you could use NPM as module manager

cdktf-and-terraform

See also this blog post.

Examples

Current Limitations

  • Outputs are already fully supported by cdktf but due to the random naming a bit hard to use. There' an open issue to address this
  • Variables aren't directly supported in cdktf, but can still be done with escape hatches. There's an open issue
  • Since there aren't official prebuilt provider packages at the moment, this has to inline the generated constructs. That's the biggest drawback at the moment, but the work to improve this is underway - see this open issue.

Next Iteration

  • Add jsii for polyglot package
  • Add unit tests
  • Come up with a more realistic example for the construct

Conclusion

While it's not the best user experience at this point, I think this demonstrates that the Terraform CDK is capable of distributing hybrid modules. When the various issues around this topic are fixed, it'll be actually a pretty good user experience from my point of view.

Contributions

Contributions to this repository are more than welcome!

About

A Terraform CDK Construct which is also usable as Terraform Module

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published