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

**URL:** <https://discuss.hashicorp.com/t/how-to-specify-code-for-cloudfront-function-not-lambda-edge/47398>\
**Category:** CDK for Terraform\
**Created:** [November 28, 2022, 6:57pm UTC](https://discuss.hashicorp.com/t/how-to-specify-code-for-cloudfront-function-not-lambda-edge/47398 "2022-11-28T18:57:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyrfer](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/cyrfer/32/31241_2.png) [@cyrfer](https://discuss.hashicorp.com/u/cyrfer)\
**Post date:** [November 28, 2022, 6:57pm UTC](https://discuss.hashicorp.com/t/how-to-specify-code-for-cloudfront-function-not-lambda-edge/47398/1 "2022-11-28T18:57:36Z")

</div>

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](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:

```auto
    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?
    })

```

---

<div class="post-metadata">

**Author:** ![ansgarm](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/ansgarm/32/18680_2.png) [@ansgarm](https://discuss.hashicorp.com/u/ansgarm)\
**Post date:** [November 28, 2022, 8:41pm UTC](https://discuss.hashicorp.com/t/how-to-specify-code-for-cloudfront-function-not-lambda-edge/47398/2 "2022-11-28T20:41:33Z")

</div>

Hi @cyrfer 👋

Does [this example](https://github.com/cdktf/cdktf-integration-serverless-example) help?

---

<div class="post-metadata">

**Author:** ![cyrfer](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/cyrfer/32/31241_2.png) [@cyrfer](https://discuss.hashicorp.com/u/cyrfer)\
**Post date:** [December 5, 2022, 5:31pm UTC](https://discuss.hashicorp.com/t/how-to-specify-code-for-cloudfront-function-not-lambda-edge/47398/3 "2022-12-05T17:31:23Z")

</div>

@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:

> <https://github.com/cdktf/cdktf-integration-serverless-example/blob/main/lib/nodejs-function.ts#L43>

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

---

<div class="post-metadata">

**Author:** ![jsteinich](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jsteinich/32/9502_2.png) [@jsteinich](https://discuss.hashicorp.com/u/jsteinich)\
**Post date:** [December 6, 2022, 2:16am UTC](https://discuss.hashicorp.com/t/how-to-specify-code-for-cloudfront-function-not-lambda-edge/47398/4 "2022-12-06T02:16:52Z")

</div>

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](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_object) to download an S3 object and use that.

---

<div class="post-metadata">

**Author:** ![cyrfer](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/cyrfer/32/31241_2.png) [@cyrfer](https://discuss.hashicorp.com/u/cyrfer)\
**Post date:** [December 6, 2022, 3:51am UTC](https://discuss.hashicorp.com/t/how-to-specify-code-for-cloudfront-function-not-lambda-edge/47398/5 "2022-12-06T03:51:50Z")

</div>

@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](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_function#code)

---

<div class="post-metadata">

**Author:** ![jsteinich](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jsteinich/32/9502_2.png) [@jsteinich](https://discuss.hashicorp.com/u/jsteinich)\
**Post date:** [December 7, 2022, 4:09am UTC](https://discuss.hashicorp.com/t/how-to-specify-code-for-cloudfront-function-not-lambda-edge/47398/6 "2022-12-07T04:09:45Z")

</div>

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](https://github.com/hashicorp/terraform-provider-aws/blob/a421ee175fdae8d1d2d5ef71b86d46809b4780fb/internal/service/cloudfront/function.go#L74).

`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.
