TypeScript-first infrastructure as code for Terraform
npm install tftsA TypeScript SDK for defining Terraform infrastructure as code. Drop-in replacement for CDKTF.
``bash`
npm install tfts
Create cdktf.json in your project root:
`json`
{
"language": "typescript",
"app": "bun run main.ts",
"terraformProviders": ["hashicorp/google@~>6.0"]
}
`bash`
npx tfts get
`typescript
// main.ts
import { App, TerraformStack, TerraformOutput } from "tfts";
import { GoogleProvider } from "./.gen/providers/hashicorp/google/provider.js";
import { ComputeInstance } from "./.gen/providers/hashicorp/google/resources/compute-instance.js";
class MyStack extends TerraformStack {
constructor(scope: App, id: string) {
super(scope, id);
new GoogleProvider(this, "google", {
project: "my-project",
region: "us-central1",
});
const instance = new ComputeInstance(this, "vm", {
name: "my-instance",
machineType: "e2-micro",
zone: "us-central1-a",
bootDisk: {
initializeParams: {
image: "debian-cloud/debian-11",
},
},
networkInterface: [{ network: "default" }],
});
new TerraformOutput(this, "instance-ip", {
value: instance.networkInterface.get(0).networkIp,
});
}
}
const app = new App();
new MyStack(app, "my-stack");
app.synth();
`
`bash`
npx tfts synth
cd cdktf.out/stacks/my-stack
terraform init
terraform apply
| Command | Description |
|---------|-------------|
| tfts get | Generate provider bindings from cdktf.json |tfts synth
| | Synthesize Terraform JSON configuration |tfts diff
| | Show planned changes (terraform plan) |tfts deploy
| | Deploy the stack (terraform apply) |tfts destroy
| | Destroy the stack (terraform destroy) |tfts output
| | Show stack outputs (terraform output) |tfts list
| | List all stacks |tfts force-unlock
| | Release a stuck state lock |
`typescript`
const app = new App();
new MyStack(app, "production");
new MyStack(app, "staging");
app.synth();
`typescript
const region = new TerraformVariable(this, "region", {
type: "string",
default: "us-central1",
});
new TerraformOutput(this, "url", {
value: instance.selfLink,
sensitive: true,
});
`
Resource attributes automatically create Terraform references:
`typescript`
const bucket = new StorageBucket(this, "bucket", { name: "my-bucket" });
new ComputeInstance(this, "vm", {
metadata: { bucket: bucket.name }, // Creates ${google_storage_bucket.bucket.name}
});
`typescript
import { Fn } from "tfts";
Fn.join("-", ["hello", "world"]);
Fn.lookup(myMap, "key", "default");
Fn.base64encode("hello");
`
`typescript
import { GcsBackend, S3Backend, RemoteBackend } from "tfts";
new GcsBackend(this, { bucket: "my-tf-state", prefix: "prod" });
`
See the Migration Guide for detailed instructions.
Quick start:
`bashRemove CDKTF, add tfts
npm remove cdktf cdktf-cli @cdktf/provider-aws @cdktf/provider-google
npm install tfts
MIT