Paymahn Moghadasian
Paymahn Moghadasian

Reputation: 10349

How to load local file with cdktf for helm release?

I'd like to reference a local yaml file when creating a helm chart with cdktf.

I have the following cdktf config:

{
  "language": "typescript",
  "app": "npx ts-node main.ts",
  "projectId": "...",
  "terraformProviders": [
    "hashicorp/aws@~> 3.42",
    "hashicorp/kubernetes@ ~> 2.7.0",
    "hashicorp/http@ ~> 2.1.0",
    "hashicorp/tls@ ~> 3.1.0",
    "hashicorp/helm@ ~> 2.4.1",
    "hashicorp/random@ ~> 3.1.0",
    "gavinbunney/kubectl@ ~> 1.14.0"
  ],
  "terraformModules": [
    {
      "name": "secrets-store-csi",
      "source": "app.terraform.io/goldsky/secrets-store-csi/aws",
      "version": "0.1.5"
    }
  ],
  "context": {
    "excludeStackIdFromLogicalIds": "true",
    "allowSepCharsInLogicalIds": "true"
  }
}

Note npx ts-node main.ts as the app.

In main.ts I have the following helm release

    new helm.Release(this, "datadog-agent", {
      chart: "datadog",
      name: "datadog",
      repository: "https://helm.datadoghq.com",
      version: "3.1.3",
      set: [
        {
          name: "datadog.clusterChecks.enabled",
          value: "true",
        },
        {
          name: "clusterAgent.enabled",
          value: "true"
        },
      ],
      values: ["${file(\"datadog-values.yaml\")}"],
    });

Note that I'm referencing a yaml file called datadog-values.yaml similar to this example from the helm provider.

datadog-values.yaml is a sister file to main.ts

enter image description here

However, when I try to deploy this with cdktf deploy I get the following error

│ Error: Invalid function argument
│
│   on cdk.tf.json line 1017, in resource.helm_release.datadog-agent.values:
│ 1017:           "${file(\"datadog-values.yaml\")}"
│
│ Invalid value for "path" parameter: no file exists at
│ "datadog-values.yaml"; this function works only with files that are
│ distributed as part of the configuration source code, so if this file will
│ be created by a resource in this configuration you must instead obtain this
goldsky-infra-dev  ╷
                   │ Error: Invalid function argument
                   │
                   │   on cdk.tf.json line 1017, in resource.helm_release.datadog-agent (datadog-agent).values:
                   │ 1017:           "${file(\"datadog-values.yaml\")}"
                   │
                   │ Invalid value for "path" parameter: no file exists at
                   │ "datadog-values.yaml"; this function works only with files that are
                   │ distributed as part of the configuration source code, so if this file will
                   │ be created by a resource in this configuration you must instead obtain this
                   │ result from an attribute of that resource.

To run a deployment I execute npm run deploy:dev which is a customer script in my package.json:

    "build": "tsc",
    "deploy:dev": "npm run build && npx cdktf deploy",

How can I reference my datadog yaml file in a helm release like in the example shown by the helm provider?

Upvotes: 3

Views: 1726

Answers (1)

javierlga
javierlga

Reputation: 1650

To reference local files in CDKTF, you need to use assets. Assuming at the root level of your project there's a values folder where you store your values yaml file:

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

      new HelmRelease(this, 'helm-release', {
        name: this.releaseName,
        chart: this.chartName,
        repository: this.chartRepository,
        values: [ Fn.file(valuesAsset.path) ]
      })
    }

Note that I've used the file Terraform function to read the content of the file.

Upvotes: 6

Related Questions