hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.84k stars 450 forks source link

Custom Resources #661

Open skorfmann opened 3 years ago

skorfmann commented 3 years ago

Community Note

Description

It'd be fantastic to have the option to create custom, stateful resources on the fly. Essentially an cdktf-adhoc-provider, which acts as a Terraform Plugin. This should be auto generated from user written code.

Prior Art

There is the https://github.com/lukekaalim/terraform-plugin-node-SDK project which is doing something like this for nodejs already. While it seems to be more like a proof of concept, their readme describes the use-cases I have in mind pretty well:

Why create Plugins? Terraform has a wealth of plugins available to it already created that allow you to manage a vast amount of resources in an interconnected graph, but it doesn't have all of them.

When using terraform, you may wish to:

  • Create resources that talk to a proprietary or internal API
  • Create resource for an API that doesn't have a provider yet
  • Manage a resource for an existing provider that doesn't provide certain functionality
  • Disagree with the implementation of an API and want to give yourself greater flexibility
  • Model existing build scripts as declarative structures

There's also an example how their implementation looks like.

RichiCoder1 commented 1 year ago

While I think part of the hard part would be creating a maintained plugin framework to simplify custom resources (see below), I was also thinking about how CDKTF might bootstrap these custom "plugins".

In very abbreviated psudeo form, my thought process was:

/ ... some code ... / const provider = new CustomProvider(this, "MyProvider", { // Gets translated into the entrypoint defined above as appropraite handler: Handler.fromNodejs(path.join(__dirname, "src/index.js")), }); new CustomResource(this, "MyResource", { provider, // CDKTF can probably handle auto-prefixing resoruces" resource: "my_resource", properties: / my properties /, }); / ... more code ... /



The hard part off course is after. Creating NodeJS plugins is one thing, creating plugins that will work in any of CDKTF's supported runtimes is another matter. CloudFormation and the AWS CDK supports this via Lambda's runtime support and well-defined event model but it's not so simple with local execution.

A possibility is a JSII provided meta-framework that effectively duplicates https://github.com/hashicorp/terraform-plugin-framework via JSII, but you still have to solve for the communication layer between Terraform and the plugin without leaking too much to the consumer. I'm not sure there's a reasonable low effort way to support this in other language and an MVP might just be to implement Node-based custom resources and punt on JSII-based ones.