Search code examples
amazon-web-servicesamazon-cloudfrontaws-lambda-edgecloudfront-functions

Amazon CloudFront - What is the running order of Continous Deployment and Functions?


Amazon CloudFront has these 2 features:

  • Continous Deployment: a Staging distribution to test changes before deploying to Main distribution, requests can route to staging by Weight or by Header
  • Function: run code using CloudFront Function/Lambda@Edge to process request/response before/after the going to the Origin.

For a scenario where:

  • I have CloudFront distribution distMain with Continous Deployment enabled, staging distribution distStg, the routing to staging is based on header aws-cf-cd-foo = bar
  • My distributions have associated a Function (can be either CloudFront Function or Lambda@Edge) funcChangeFoo which will update aws-cf-cd-foo = dood base on some condition condDood.
  • A request to distMain with header aws-cf-cd-foo = bar but meet condition condDood

My question is, does

  • the function funcChangeFoo run first, change the header aws-cf-cd-foo = dood and the request continue to distMain
  • or, the Continous Deployment routing matches first, the request goes to distStg and the function trigger afterward?

Solution

  • Short answer: Continuous Deployment policy is not reevaluated after a viewer or request change. You cannot dynamically route requests from Production to Staging distribution with your Lambda@Edge or CloudFront functions. Only built-in features: static headers or weight.

    As you said, the Continuous Deployment feature is on top of CloudFront distribution. So its routing policy is executed before reaching the target distribution. Then, the behaviors and its related Lambda@Edge, CloudFront functions are processed.

    Continuous Deployment policy is not reevaluated after a viewer or request change. Any manipulations are done within this distribution. You can consider the Staging and Production CloudFront distributions as isolated, without communication or shared configuration.