cdktf / cdktf-tf-module-stack

A drop-in replacement for cdktf.TerraformStack that lets you define Terraform modules as constructs
Mozilla Public License 2.0
20 stars 2 forks source link
cdktf constructs terraform terraform-module

cdktf-tf-module-stack

Status: Tech Preview Releases LICENSE build

A drop-in replacement for cdktf.TerraformStack that lets you define Terraform modules as constructs.

cdktf-tf-module-stack is in technical preview, which means it's a community supported project. It still requires extensive testing and polishing to mature into a HashiCorp officially supported project. Please file issues generously and detail your experience while using the library. We welcome your feedback.

By using the software in this repository, you acknowledge that:

Compatibility

Available Packages

NPM

The npm package is available at https://www.npmjs.com/package/@cdktf/tf-module-stack.

npm install @cdktf/tf-module-stack

NOTE: Originally, this package was named cdktf-tf-module-stack, and the legacy versions (<= 0.2.0) can be found on npm here.

PyPI

The PyPI package is available at https://pypi.org/project/cdktf-tf-module-stack.

pipenv install cdktf-tf-module-stack

Nuget

The Nuget package is available at https://www.nuget.org/packages/HashiCorp.Cdktf.TfModuleStack.

dotnet add package HashiCorp.Cdktf.TfModuleStack

Maven

The Maven package is available at https://mvnrepository.com/artifact/com.hashicorp/cdktf-tf-module-stack.

<dependency>
    <groupId>com.hashicorp</groupId>
    <artifactId>cdktf-tf-module-stack</artifactId>
    <version>[REPLACE WITH DESIRED VERSION]</version>
</dependency>

Go

The go package is generated into the github.com/cdktf/cdktf-tf-module-stack-go package.

go get github.com/cdktf/cdktf-tf-module-stack-go/tfmodulestack

Usage

Typescript

import { App } from "cdktf";
import {
  TFModuleStack,
  TFModuleVariable,
  TFModuleOutput,
  ProviderRequirement,
} from "@cdktf/tf-module-stack";
import { Resource } from '@cdktf/provider-null/lib/resource';

class MyAwesomeModule extends TFModuleStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    new ProviderRequirement(this, "null", "~> 2.0");
    const resource = new Resource(this, "resource");

    new TFModuleVariable(this, "my_var", {
      type: "string",
      description: "A variable",
      default: "default",
    });

    new TFModuleOutput(this, "my_output", {
      value: resource.id,
    });
  }
}

const app = new App();
new MyAwesomeModule(app, "my-awesome-module");
app.synth();

Python

from constructs import Construct
from cdktf import App, TerraformStack
from imports.null.resource import Resource
from cdktf_tf_module_stack import TFModuleStack, TFModuleVariable, TFModuleOutput, ProviderRequirement

class MyAwesomeModule(TFModuleStack):
    def __init__(self, scope: Construct, ns: str):
        super().__init__(scope, ns)

        ProviderRequirement(self, "null", provider_version_constraint="~> 2.0")

        TFModuleVariable(self, "my_var", type="string", description="A variable", default="default")

        resource = Resource(self, "resource")

        TFModuleOutput(self, "my_output", value=resource.id)

app = App()
MyAwesomeModule(app, "my-awesome-module")
app.synth()

This will synthesize a Terraform JSON file that looks like this:

{
  "output": {
    "my_output": [
      {
        "value": "${null_resource.resource.id}"
      }
    ]
  },
  "resource": {
    "null_resource": {
      "resource": {}
    }
  },
  "terraform": {
    "required_providers": {
      "null": {
        "source": "null",
        "version": "~> 2.0"
      }
    },
    "variable": {
      "my_var": {
        "default": "default",
        "description": "A variable",
        "type": "string"
      }
    }
  }
}

Please note that the provider section is missing, so that the Terraform Workspace using the generated module can be used with any provider matching the version.