Skip to content

Instantly share code, notes, and snippets.

@hrmsk66
Last active April 4, 2023 12:05
Show Gist options
  • Save hrmsk66/e6a78010d2f4a20b028ef17c96257e3e to your computer and use it in GitHub Desktop.
Save hrmsk66/e6a78010d2f4a20b028ef17c96257e3e to your computer and use it in GitHub Desktop.

Convert Fastly Terraform Project to CDKTF (TypeScript)

Note: As of April 2023, it appears that only TypeScript supports the conversion of existing Terraform projects.

Working Directory

The project directory structure in this example steps is as follows: cdktf is the working directory and tf is the existing Terraform Project.

$ tree
├── cdktf
└── tf
    ├── main.tf
    └── vcl
        └── main.vcl

Prerequisites

Install the cdktf-cli

$ npm install --global cdktf-cli@latest

Steps

  1. Run the cdktf init command and specify the Terraform project directory when prompted:
$ cdktf init --template=typescript --local
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.<STACK NAME>.tfstate' in the root of your project.
? Project Name ts
? Project Description A simple getting started project for cdktf.
? Do you want to start from an existing Terraform project? Yes
? Please enter the path to the Terraform project ../tf <--- (Existing Terraform Project)
? Do you want to send crash reports to the CDKTF team? No
  1. Add the following line to tsconfig.json:
"ignoreDeprecations": "5.0",
  1. Add FastlyProvider to main.ts and include the necessary import statement
import { FastlyProvider } from './.gen/providers/fastly/provider';
( snip )
    new FastlyProvider(this, 'fastly', {
      apiKey: 'XXXXX',
    });

API tokens should be imported from outside main.ts using dotenv or similar.

  1. Add the VCL file as a TerraformAsset
import { AssetType, TerraformAsset } from 'cdktf';
( snip )
    const VCLAsset = new TerraformAsset(this, 'values-asset', {
      path: `${process.cwd()}/../tf/vcl/main.vcl`,
      type: AssetType.FILE,
    });
  1. Specify the TerraformAsset (VCL file) using the file function
import { Fn } from 'cdktf';
( snip )
    new fastly.serviceVcl.ServiceVcl(this, 'service', {
      backend: [
        {
          address: 'httpbin.org',
          name: 'httpbin',
          port: 443,
          sslCertHostname: 'httpbin.org',
          sslSniHostname: 'httpbin.org',
          useSsl: true,
        },
      ],
      domain: [
        {
          name: domain,
        },
      ],
      forceDestroy: true,
      name: domain,
      vcl: [
        {
          content: Fn.file(VCLAsset.path), <--- (Replaced Part)
          main: true,
          name: 'main',
        },
      ],
    });

Complete main.ts After the Steps

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as fastly from './.gen/providers/fastly';

import { App, AssetType, Fn, TerraformAsset, TerraformStack } from 'cdktf';
import { Construct } from 'constructs';
import { FastlyProvider } from './.gen/providers/fastly/provider';

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

    new FastlyProvider(this, 'fastly', {
      apiKey: 'XXXXX',
    });

    const VCLAsset = new TerraformAsset(this, 'values-asset', {
      path: `${process.cwd()}/../tf/vcl/main.vcl`,
      type: AssetType.FILE,
    });

    const domain = 'cdktest.global.ssl.fastly.net';
    new fastly.serviceVcl.ServiceVcl(this, 'service', {
      backend: [
        {
          address: 'httpbin.org',
          name: 'httpbin',
          port: 443,
          sslCertHostname: 'httpbin.org',
          sslSniHostname: 'httpbin.org',
          useSsl: true,
        },
      ],
      domain: [
        {
          name: domain,
        },
      ],
      forceDestroy: true,
      name: domain,
      vcl: [
        {
          content: Fn.file(VCLAsset.path),
          main: true,
          name: 'main',
        },
      ],
    });
  }
}

const app = new App();
new MyStack(app, 'ts');
app.synth();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment