Skip to content

Latest commit

 

History

History
207 lines (152 loc) · 5.11 KB

typescript.md

File metadata and controls

207 lines (152 loc) · 5.11 KB

Getting started with TypeScript

Prerequisites

Install CDK for Terraform CLI

npm install -g cdktf-cli

Learn more how to use the cdktf command-line interface here.

Initialize a new CDK for Terraform project

mkdir hello-terraform
cd hello-terraform
cdktf init --template="typescript" --local

This will initialize a brand new CDK for Terraform project in TypeScript using an interactive command.

Note: By supplying '--local' option you have chosen local storage mode for storing the state of your stack.
This means that your Terraform state file will be stored locally on disk in a file 'terraform.tfstate' in the root of your project.

We will now set up the project. Please enter the details for your project.
If you want to exit, press ^C.

Project Name: (default: 'hello-terraform')
Project Description: (default: 'A simple getting started project for cdktf.')

Also, this command installs the cdktf library so that it can be used in the project.

CDK for Terraform Application

You can now open up the main.ts file to view your application code.

vim main.ts
import { Construct } from "constructs";
import { App, TerraformStack } from "cdktf";

class MyStack extends TerraformStack {
  constructor(scope: Construct, name: string) {
    super(scope, name);

    // define resources here
  }
}

const app = new App();
new MyStack(app, "hello-terraform");
app.synth();

Refer to the examples directory for additional examples.

Let's take a simple TypeScript application that uses the CDK for Terraform package.

import { Construct } from "constructs";
import { App, TerraformStack } from "cdktf";
import { AwsProvider, Instance } from "./.gen/providers/aws";

class MyStack extends TerraformStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    new AwsProvider(this, "aws", {
      region: "us-east-1",
    });

    new Instance(this, "Hello", {
      ami: "ami-2757f631",
      instanceType: "t2.micro",
    });
  }
}

const app = new App();
new MyStack(app, "hello-terraform");
app.synth();

Synthesize Application

When you are ready you can run the synthesize command to generate Terraform JSON configuration for the application. Running the command will compile the application npm run compile in the background for you which will compile TypeScript to Javascript.

cdktf synth
Generated Terraform code in the output directory: cdktf.out

This command will generate a directory called cdktf.out. This directory contains the Terraform JSON configuration for your application.

cd cdktf.out

Terraform AWS provider and Instance expressed as Terraform JSON configuration.

cat cdktf.out/cdk.tf.json
{
  "terraform": {
    "required_providers": {
      "aws": "~> 2.0"
    }
  },
  "provider": {
    "aws": [
      {
        "region": "us-east-1"
      }
    ]
  },
  "resource": {
    "aws_instance": {
      "examplesimpleHelloF6D4983C": {
        "ami": "ami-2757f631",
        "instance_type": "t2.micro"
      }
    }
  }
}

Note: You can generate the Terraform JSON configuration while synthesizing the code by running cdktf synth --json.

Deploy Application

Note: You can use Terraform commands like terraform init, terraform plan, and terraform apply with the generated Terraform JSON configuration (learn more here) or optionally continue to use the CDK for Terraform CLI for a first-class experience.

You can now deploy your CDK for Terraform application using the cdktf deploy command.

cdktf deploy

This command will ask for confirmation on a generated diff and then deploy the application.

Stack: helloterraform
Resources
 + AWS_INSTANCE         Hello aws_instance.helloterraform_Hello_19940A68

Diff: 1 to create, 0 to update, 0 to delete.
Do you want to continue (Y/n)? y

Deployed application

Deploying Stack: helloterraform
Resources
 ✔ AWS_INSTANCE         Hello aws_instance.helloterraform_Hello_19940A68

Summary: 1 created, 0 updated, 0 destroyed.

The cdktf deploy command runs a terraform apply in the background. If you are using local storage mode then it creates a terraform.tfstate file in the root of the project.

Destroy Application

You can destroy the application by running cdktf destroy.

cdktf destroy

This command will ask for confirmation on a generated diff and then destroy the application if the user confirms that they want to continue with the destroy operation.

Stack: helloterraform
Resources
 - AWS_INSTANCE         hello aws_instance.helloterraform_Hello_19940A68

Diff: 0 to create, 0 to update, 1 to delete.
Do you want to continue (Y/n)?

Destroyed application

Destroying Stack: helloterraform
Resources
 ✔ AWS_INSTANCE         hello aws_instance.helloterraform_Hello_19940A68

Summary: 1 destroyed.