Create CloudFront Functions that modify request origin and add custom headers to responses, deployed with a CloudFront distribution.
Run npm ci to get setup.
Have a look at the CloudFront Function code in ./src/origin.ts and ./src/header.ts. This example demonstrates two functions:
origin.ts- Changes the request origin togithub.com/mrgrain/cdk-esbuildheader.ts- Adds a custom headerx-custom-headerto responses
The CDK stack creates:
- Two CloudFront Functions built from TypeScript using
CloudFrontTypeScriptCode.fromFile() - A CloudFront distribution with an HTTP origin (
moritzkornher.de) - The origin function is associated as a viewer request function
- The header function is associated as a viewer response function
Once you are happy, deploy your app with npx cdk deploy. If you haven't used the CDK in your account before, you will have bootstrap the account first (typically by running npx cdk bootstrap) and sort out permissions. Please refer to the official AWS CDK documentation to get started.
As part of the deployment, both the CloudFront distribution domain name and function ARN will be displayed. You can test the functions by making requests to the distribution URL - requests will be redirected to the GitHub repository and responses will include the custom header.
Don't forget to tear everything down with npx cdk destroy - otherwise you will incur costs.
npm installstart with thisnpm testperform the jest unit testsnpx cdk bootstrapsetup your AWS environmentnpx cdk deploydeploy this stack to your default AWS account/regionnpx cdk destroydestroy this stack to ensure you don't incur any costsnpx cdk diffcompare deployed stack with current statenpx cdk synthemits the synthesized CloudFormation template