banner

KuchBhiLearning - A free website to learn and code

This is a good learning site. This contains details of cloud computing, AWS, AWS-CDK, AWS-SDK codes and examples including S3, Redis, lambda, api-gateway, cloudfront, cloudformation.

 Add custom header in Cloudfront/Pass custom header from cloudfront using AWS CDK


Add custom header in Cloudfront/Pass custom header from cloudfront using AWS CDK

We have discussed the use cases of custom header using AWS CLI here.

How to do it using CDK?

It is easy to do these changes in CDK.

We need to create the origin request policy like this.

  const originRequestPolicy = new cloudFront.OriginRequestPolicy(
    stack,
    `cf-origin-req-policy`,
    {
      cookieBehavior: cloudFront.OriginRequestCookieBehavior.all(),
      // Any custom header should be allowed from Cloudfront, else it will not forward the custom header
      headerBehavior: cloudFront.OriginRequestHeaderBehavior.allowList('example-header-name'),
      queryStringBehavior: cloudFront.OriginRequestQueryStringBehavior.all()
    }
  );

If we closely look in origin request policy, we can see that OriginHeaderBehavior has explicit method called allowList.

We need to pass the custom header name which we were looking for, in this example we have mentioned example-header-name. So this way CloudFront will know that it need to explicitly allow this header as part of request.


Further Reading

Custom Header in Cloudfront(https://kuchbhilearning.blogspot.com/2022/11/add-custom-header-in-cloudfrontpass.html)

API Gateway and CloudFront in the same domain with different path(https://kuchbhilearning.blogspot.com/2022/10/api-gateway-and-cloud-front-in-same.html)

Pass query params from CloudFront to API Gateway AWS-CDK(https://kuchbhilearning.blogspot.com/2022/10/pass-query-params-from-cloudfront-to.html)

Add CloudFront Behavior and Origin using AWS-CDK(https://kuchbhilearning.blogspot.com/2022/10/add-cloudfront-behavior-and-origin.html)

AWS CloudFront Function(https://kuchbhilearning.blogspot.com/2022/09/aws-cloudfront-function-aws-has.html)

No comments:

Post a Comment

If you have any doubts, Please let me know

Copyright 2022, KuchBhiLearning - A free website to learn and code. All rights Reserved.
| Designed by Yaseen Shariff