@@ -549,7 +549,7 @@ In which case you use `FunctionCode.fromFile()` instead of `FunctionCode.fromInl
549
549
550
550
``` ts
551
551
new cloudfront .Function (this , ' Function' , {
552
- code: cloudfront .FunctionCode .fromFile (' function-code.js' ),
552
+ code: cloudfront .FunctionCode .fromFile ({ filePath: ' function-code.js' } ),
553
553
runtime: cloudfront .FunctionRuntime .JS_2_0
554
554
});
555
555
```
@@ -566,7 +566,7 @@ Consider this CloudFront function in `function-code.js`. The intention to
566
566
replace ` %DEFAULT_REDIRECT% ` with a URL that is different for different
567
567
environments.
568
568
569
- ``` ts
569
+ ``` js
570
570
function handler (event ) {
571
571
// …
572
572
return {
@@ -576,33 +576,37 @@ function handler(event) {
576
576
}
577
577
```
578
578
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 ` .
580
582
581
583
``` ts
582
584
declare const defaultRedirect: string // The actual URL depends on the environment, dev vs prod for example
583
585
new cloudfront .Function (this , ' Function' , {
584
- code: cloudfront .FunctionCode .fromFile (' function-code.js' ),
585
586
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
+ }),
589
593
});
590
594
```
591
595
592
596
If ` defaultRedirect ` were ` https://www.example.com/ ` during synthesis, the CDK would
593
597
update your CloudFront function with this code.
594
598
595
- ``` ts
599
+ ``` js
596
600
function handler (event ) {
597
601
// …
598
602
return {
599
603
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
601
605
}
602
606
}
603
607
```
604
608
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
606
610
> editor. The CDK does not look for whole identifiers, strings, or words. The
607
611
> CDK replaces anything in your code that matches ` find ` regardless of its context.
608
612
>
@@ -675,12 +679,12 @@ new cloudfront.Function(this, 'Function', {
675
679
676
680
In your CloudFront function, put ` %KVS_ID% ` where you would normally put the key value store ID.
677
681
678
- ``` ts
682
+ ``` js
679
683
import cf from ' cloudfront' ;
680
684
const kvsId = " %KVS_ID%" ;
681
685
const kvsHandle = cf .kvs (kvsId);
682
686
683
- function handler(event ) {
687
+ async function handler (event ) {
684
688
// …
685
689
}
686
690
```
@@ -690,10 +694,12 @@ Then in your CDK app, find `%KVS_ID%` and replace it with the key value store ID
690
694
``` ts
691
695
const store = new cloudfront .KeyValueStore (this , ' KeyValueStore' );
692
696
new cloudfront .Function (this , ' Function' , {
693
- code: cloudfront .FunctionCode .fromFile (' function-code.js' ),
694
697
runtime: cloudfront .FunctionRuntime .JS_2_0 ,
695
698
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
+ }),
697
703
});
698
704
```
699
705
0 commit comments