How to template file in cdktf

Hello,

How would I go about templating a file using cdktf? I’m working through doing AWS fargate deployments and I’d like to template my container definitions.

You can use Fn.templatefile(path, vars) for this, it synthesises to the templatefile function.

Is there a working example I could look at?

Hi @kingemn :wave:

Which language are you using? If those container definitions are e.g. a string containing JSON you could also construct that JSON object in your language and pass that to Fn.jsonencode.
In TypeScript that could look something like this:

import * as aws from "./.gen/providers/aws";
new aws.ecs.EcsTaskDefinition(this, "service", {
  containerDefinitions: Fn.jsonencode([{ name: "app", image: "nginx", ... }])
});

You can also use variables and refer attributes of other resources instead of "nginx" for example. You don’t need a template when using CDKTF the way you if you were writing HCL.

So the answer depends a bit on your exact templating needs :slightly_smiling_face:

I was able to solve it, so I will leave it as a memo.
The second argument can specify a variable for interpolation.

import { Fn } from 'cdktf'

const tmpFile = Fn.templatefile(join(__dirname, './workflows/test.yaml'), {})