npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@cdktf/hcl2cdk

v0.21.0

Published

Transform HCL into CDK

Readme

@cdktf/hcl2cdk

Uses @cdktf/hcl2cdk to transform HCL configuration to CDK constructs.

Usage

yarn add @cdktf/hcl2cdk

Convert HCL strings to Constructs

import { convert } from "@cdktf/hcl2cdk";

const hcl = `
  variable "name" {
    description = "Name to be used on all the resources as identifier"
    type        = string
    default     = ""
  }
`(async () => {
  const ts = await convert(hcl, { language: "typescript" });
  console.log(ts.imports); // just the necessary imports
  console.log(ts.code); // just the constructs
  console.log(ts.all); // code with imports
})();

// =>
import * as cdktf from "cdktf";

new cdktf.TerraformVariable(this, "imageId", {
  type: "string",
  default: "ami-abcde123",
  description: "What AMI to use to create an instance",
});

Convert a project

import * as hcl2cdk from "@cdktf/hcl2cdk";
import {
  readSchema,
  ConstructsMakerProviderTarget,
  LANGUAGES,
  config,
} from "@cdktf/provider-generator";

(async () => {
  const hcl = hcl2cdk.getTerraformConfigFromDir("/path/to/terraform/project");
  const providerRequirements = await hcl2cdk.parseProviderRequirements(hcl);
  const targets = Object.entries(providerRequirements).map(([name, version]) =>
    ConstructsMakerProviderTarget.from(
      new config.TerraformProviderConstraint(`${name}@ ${version}`),
      LANGUAGES[0],
    ),
  );
  // Get all the provider schemas, making the conversion more precise
  const { providerSchema } = await readSchema(targets);

  const mainTs = await hcl2cdk.convert(hcl, {
    language: "typescript",
    providerSchema: providerSchema,
  });

  await hcl2cdk.convertProject(
    hcl,
    mainTs.code,
    require("../cdktf.json"),
    { language: "typescript", providerSchema }, // Currently we only support Typescript for project conversion
  );
})();

This transforms your Terraform project into a CDK for Terraform project, besides the resource naming within Terraform the deployed resources should not differ between terraform plan and cdktf plan.

Known Limitations

Providers guessed can be not functional

If your HCL includes providers that are not mentioned under required_providers we infer the name, e.g. if you use the datadog_dashboard resource we infer the provider datadog which is right, but the namespace is missing, for DataDog it would be datadog/datadog. Instead we will try to use hashicorp/datadog and fail because this provider is not known to the registry. Please see the required providers docs for more information on how to specify providers.

Local Modules and Files

We don't move modules or files for you, if you reference local modules you have to move them so that the relative paths are correct. If you want to make use of files you need to wrap them in a TerraformAsset before using them.

Development

We have two types of test cases, one within lib that are on the unit level and one within test that are testing the entire package at once by converting and then synthesizing the resulting code.

In general, both test types can be run by npx jest <pathToTestCase>. You can add -u to update the snapshots and --watch to run the tests in watch mode.

To make the tests inside test faster we disable synthesizing and multi-language snapshots by default. You can enable them by setting the envinronment variable CI=true. Another way of improving the performance significantly is setting the TF_PLUGIN_CACHE_DIR to a valid directory in order to cache the provider binaries used within the tests. E.g. by running TF_PLUGIN_CACHE_DIR=(mktemp -d) npx jest <pathToTestCase> --watch.