How to specify code for Cloudfront Function (not Lambda@Edge)

Hi,

I am trying to adapt this terraform usage to CDKTF usage:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_function#example-usage

I don’t know what to do with “file(…)”. I would like to provide s3 path to a zip file as I put my function code there already.

My CDKTF code:

    const requestFunction = new aws.cloudfrontFunction.CloudfrontFunction(this, 'edge-request-function', {
      name    : `${name}-request-edge-function`,
      runtime : "cloudfront-js-1.0",
      comment : `${name}`,
      publish : true,
      code    : file("${path.module}/function.js") // HOW TO DO THIS?
    })

Hi @cyrfer :wave:

Does this example help?

@ansgarm I’m not sure. Does it assume the code is available locally? I’m trying to use code on S3.

I traced the logic to this:

I think that shows me how I might pull in code from S3. I was hoping CDKTF had something for such a common scenario.

Checking both the Terraform cloudfront_function resource and the AWS apis make it look like direct S3 paths aren’t supported and it’s assumed that the function code is available locally.
You may be able to use Terraform Registry to download an S3 object and use that.

@jsteinich What reference did you use to check the Terraform resource?

I did not understand how examples knew to use “file(…)” for the ‘code’ attribute documented here:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_function#code

I just read the provider documentation for the resource that you linked. If you want more details, the provider source for that resource is here.

file is a built-in Terraform function that reads in the contents of a file and returns it. It’s also possible to inline the actual function code, but properly escaping newlines and such adds complexity; not to mention verbosity.