Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 817da40

Browse files
committedJun 22, 2024··
Polish.
1 parent c3a7494 commit 817da40

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed
 

‎packages/aws-cdk-lib/aws-cloudfront/README.md

+20-14
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ In which case you use `FunctionCode.fromFile()` instead of `FunctionCode.fromInl
549549

550550
```ts
551551
new cloudfront.Function(this, 'Function', {
552-
code: cloudfront.FunctionCode.fromFile('function-code.js'),
552+
code: cloudfront.FunctionCode.fromFile({ filePath: 'function-code.js' }),
553553
runtime: cloudfront.FunctionRuntime.JS_2_0
554554
});
555555
```
@@ -566,7 +566,7 @@ Consider this CloudFront function in `function-code.js`. The intention to
566566
replace `%DEFAULT_REDIRECT%` with a URL that is different for different
567567
environments.
568568

569-
```ts
569+
```js
570570
function handler(event) {
571571
//
572572
return {
@@ -576,33 +576,37 @@ function handler(event) {
576576
}
577577
```
578578

579-
You can achieve this in your CDK app with this.
579+
You can have CDK replace `%DEFAULT_REDIRECT%` by adding a `findReplace`
580+
option to `FunctionCode.fromFile`. The `findReplace` option is also
581+
available with `FunctionCode.fromInline`.
580582

581583
```ts
582584
declare const defaultRedirect: string // The actual URL depends on the environment, dev vs prod for example
583585
new cloudfront.Function(this, 'Function', {
584-
code: cloudfront.FunctionCode.fromFile('function-code.js'),
585586
runtime: cloudfront.FunctionRuntime.JS_2_0,
586-
findReplace: [
587-
{ find: '%DEFAULT_REDIRECT%', replace: defaultRedirect, all: true }, // all defaults to false
588-
],
587+
code: cloudfront.FunctionCode.fromFile({
588+
filePath: 'function-code.js',
589+
findReplace: [
590+
{ find: '%DEFAULT_REDIRECT%', replace: defaultRedirect, all: true }, // all defaults to false
591+
],
592+
}),
589593
});
590594
```
591595

592596
If `defaultRedirect` were `https://www.example.com/` during synthesis, the CDK would
593597
update your CloudFront function with this code.
594598

595-
```ts
599+
```js
596600
function handler(event) {
597601
//
598602
return {
599603
statusCode: 302, statusDescription: 'Found',
600-
headers: { location: { value: 'https://www.example.com/' } }, // %DEFAULT_REDIRECT% is replaced during CDK synth
604+
headers: { location: { value: 'https://www.example.com/' } }, // https://www.example.com/ is replaced during CDK synth
601605
}
602606
}
603607
```
604608

605-
> **Note:** This is simple find and replace, like that of a non-intelligent text
609+
> **Note:** This is a simple find and replace, like that of a non-intelligent text
606610
> editor. The CDK does not look for whole identifiers, strings, or words. The
607611
> CDK replaces anything in your code that matches `find` regardless of its context.
608612
>
@@ -675,12 +679,12 @@ new cloudfront.Function(this, 'Function', {
675679

676680
In your CloudFront function, put `%KVS_ID%` where you would normally put the key value store ID.
677681

678-
```ts
682+
```js
679683
import cf from 'cloudfront';
680684
const kvsId = "%KVS_ID%";
681685
const kvsHandle = cf.kvs(kvsId);
682686

683-
function handler(event) {
687+
async function handler(event) {
684688
//
685689
}
686690
```
@@ -690,10 +694,12 @@ Then in your CDK app, find `%KVS_ID%` and replace it with the key value store ID
690694
```ts
691695
const store = new cloudfront.KeyValueStore(this, 'KeyValueStore');
692696
new cloudfront.Function(this, 'Function', {
693-
code: cloudfront.FunctionCode.fromFile('function-code.js'),
694697
runtime: cloudfront.FunctionRuntime.JS_2_0,
695698
keyValueStore: store,
696-
findReplace: [{ find: '%KVS_ID%', replace: store.keyValueStoreId }],
699+
code: cloudfront.FunctionCode.fromFile({
700+
filePath: 'function-code.js',
701+
findReplace: [{ find: '%KVS_ID%', replace: store.keyValueStoreId }],
702+
}),
697703
});
698704
```
699705

0 commit comments

Comments
 (0)
Please sign in to comment.