Amazon CloudFront Deployment Guide (3) - Continuous Deployment

dc603277919b5eb46c866f784a5f5c56.gif

brief introduction

In this blog post, you will learn how to use CloudFront continuous deployment (Continuous deployment) for blue-green deployment/change verification, and then according to business high availability requirements.

Continuous deployment (Continuous deployment) diagram:

00b75df562a4bb1b146361fe342f16c7.jpeg

We will divide it into several parts to explain the functions below:

  1. Concept analysis

  2. Environmental preparation

  3. deployment process

  4. Configuration verification and monitoring

  5. Precautions

01

Concept analysis

By using Amazon CloudFront's continuous deployment, you can grayscale part of the production traffic to the change configuration (Staging Distribution) to safely deploy changes to the CDN configuration.

work process:

2ea58e32837034dd4dad7301841fc4f7.jpeg

To facilitate your continuous deployment with CloudFront, the concepts involved in the deployment process are explained below:

1. Primary Distribution: Distribution that requires configuration changes.

2. Staging Distribution: During the process of enabling the continuous deployment function, CloudFront will create a Staging Distribution configuration separately, and all configuration changes will be saved in the Staging Distribution, and the Primary Distribution will not be affected before the Promote is performed.

3. Policy: The traffic policy you need to use. In continuous deployment, you can use one of two methods to grayscale traffic according to actual needs:

▪ Guide some traffic to Staging Distribution based on weight-based;

▪ Specifies that requests carrying specific request headers (Header-based) are directed to Staging Distribution.

Weight-based: Up to 15% of the production traffic can be specified to be directed to the Staging Distribution. In order to meet certain specific scenarios, some users need to access the configuration before or after the change. The percentage-based policy supports session stickness. The session stickness time can be specified, and the maximum adjustable idle TTL is 1 hour. It should be noted that in session stickness, the maximum TTL (Maximum TTL) is also 1 hour. When session stickness is enabled, no matter how much idle TTL you set, when a user's session reaches 1 hour, it will re-enter the Policy link for Weight-based selection.

39bb50a6f373ff8bacb4bb3d4d969a5d.jpeg

Header-based: The name format of the request header must start with aws-cf-cd-, such as aws-cf-cd-jwtest .

4. Promote: Send the changes saved in Staging Distribution to Primary Distribution.

02

Environmental preparation

We demonstrate the deployment process based on the overall architecture of the deployment guide (2):

4ea6cce2b6b4986ddb63d7b4ba82b43a.jpeg

The requirement is that we need to add the client IP to the custom request header True-Client-IP through CloudFront Function, and carry it back to the source. We can use the official sample code to deploy CloudFront Function. You can find the CloudFront Function entry on the left side of the CloudFront Console:

5f223750159fde01a50d30e9c919d188.jpeg

Choose to create a function

adb12efcf2628163c135dcd2d0d1ce4c.jpeg

Give your function a name and continue

2b1fda2a43653b6b1e889ba73f9a3850.jpeg

Copy and paste the official sample code into the code box, save and continue

  • Sample code:

    https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/example-function-add-true-client-ip-header.html

2ceec70bdeeb18a188bd71a33bb42f27.jpeg

Let's go to the Test tab, first debug the code logic, and check whether the output results meet expectations

9ee6627894d65b31a23ddfc8cd601e96.jpeg

In the execution result below, we can see that the execution was successful, and the request header is as expected

18af4193ac9b205d500a49d9fb50f8c1.jpeg

Next we will publish this function

1cb0c49aa92eba0c7038c568d35bf874.jpeg

As mentioned in CloudFront Deployment Guide (2) in the back-to-origin request and response header policy section, if you expect custom headers to be successfully carried back to the origin, you need to add the header information to the whitelist in the origin request policy (Origin Request Policy). Here we need to add true-client-ip to the back-to-origin policy, as shown in the following figure:

608b080ebb097f16042dad10521855ff.jpeg

At this point, you have completed the prerequisites for deploying CloudFront Function to Staging Distribution!

03

deployment process

Based on the previous small guide, we come to the CloudFront Distribution interface that requires continuous deployment. You can find the entry for creating continuous deployment under the Distribution interface:

383f80b87d3502170687883c092f9802.jpeg

After clicking Continuous Deployment, we will enter the configuration interface. In this interface, you can modify the remarks of Staging Distribution, such as marking the content of this change and so on.

61c573ee07e4b4fec95d3131426c8734.png

Going to the next step, we can start to apply the changed configuration. Here we make changes to the path pattern "/":

81347d6a87b195c2d8d3a65964e357a6.png

On the change page, apply the CloudFront function prepared in the previous steps to this behavior. Please ensure that the origin request policy applied here needs to include the true-client-ip request header used in our experiment, and the CloudFront function is applied to the correct event. According to the code documentation, the function event type is viewer request, and it is applied in the corresponding event.

  • code documentation

    https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/example-function-add-true-client-ip-header.html

ca28e69cfc6514ca3d30f18fa7ef26ce.png

After clicking Save, return to the previous page, click Next here to continue:

82be9b142a843a95b4a3bdd98429e495.png

On the next page, we can choose the grayscale traffic method. As mentioned in the preamble, continuous deployment can support two traffic grayscale methods. In order to facilitate the display of test results, here we choose the Header-Based method for configuration:

6b2bf908e19944fe0c3e91d78e6bb0bc.png

After confirming that the configuration is correct, go to the bottom of the page to create Staging Distribution

8bb20f1fd2dbad2597ae6fb30ea230c0.png

After the distribution is created, we can see that the Distribution carrying the Staging logo is created, and in the Distribution list, we can also see the corresponding Staging and Primary Distribution:

a9ff50eb6029dae94e57c5bd25000c68.png

df3c4e9fe605f11d71f9c776cb1ac815.jpeg

Enter the Primary Distribution, you can see the Staging Distribution associated with it:

4d34aa69f011cd43bbb2f4852421f1ac.jpeg

At this point, you have completed creating a continuous deployment configuration!

04

Configuration Verification/Monitoring

In this actual test, we can use the header specified in the Policy to test, and the test results are as follows:

1411c09e328cbf33485498effbecb42f.jpeg

In the above screenshot, from the curl request body carrying the specified header, we can see that the request header recorded by the echo server can already see the True Client IP request header information specified by the CloudFront function logic. At the same time, this change only affects Staging Distribution. When the specified header is not carried, the Distribution configuration logic remains unchanged, and the request header does not contain the True Client IP request header information.

In Cloudwatch metrics, you can use the ID of the Staging Distribution to query the metrics and observe the effect after deployment. It is also recommended that you create a Dashboard for comparison in Cloudwatch, so that you can more intuitively see the comparison between the request status after being directed to the Staging Distribution and the request status of the Primary Distribution. The following is an example of a Dashboard for comparison:

0307ae9fd6dd6dcd14d1e1212c65e53b.jpeg

After verifying that the test results meet expectations, we can go to the Primary Distribution page, click the Promote button, and change the change from the grayscale mode to the production environment:

10154c41da88a5878d399775089256c6.jpeg

You need to ensure that the test results of the Staging configuration meet expectations. It should be noted that once the promote is completed, the configuration cannot be rolled back. You need to ensure that the verification results are correct before proceeding with the promote operation:

c9e5b453b6a946a9a543f8fa60ef2d09.jpeg

After Promote, the Staging Distribution will be in the Disabled state. If you need to make other changes, you can continue editing on this Staging Distribution and specify the traffic scheduling Policy you need.

05

Precautions

As of now (2023/5), before using CloudFront for continuous deployment, there are some usage restrictions that require your attention. For details, please refer to the official website documentation. Here are some important precautions:

  • It is very important that the Behavior lists of Primary and Staging Distribution must be consistent, otherwise you may encounter a 403 request from Staging. If you need to create a new Path Pattern match in Behavior, you can create it in Primary first, but the configuration content is consistent with that before the change, such as cache policy/request header/response header strategy, etc. After creation, create Staging Distribution on the basis of this Primary. The changes are then applied in the Staging Distribution.

  • Not all configurations are available for continuous deployment, such as HTTP version change/WAF rule association, etc. The following are the parts that support continuous deployment: 

    1. Behavior

    2. Error pages

    3. Geographic restrictions

    4. Origins

    5. Logging

  • Each Primary Distribution can only be associated with one Staging Distribution.

  • Whether Staging Distribution is enabled or not, it will not expire. If the number of Staging Distribution exceeds the limit, you can delete the unused Staging Distribution.

  • Continuous deployment traffic scheduling is implemented based on CloudFront's internal logic (weight-based or request header-based), and will not affect the DNS resolution of the actual domain name. Therefore, the Domain Name (CloudFront.net) generated by CloudFront in Staging Distribution cannot be used. If traffic grayscale is required, please select the two policy methods provided in continuous deployment.

  • When you are creating a continuous deployment configuration, if you accidentally interrupt or exit without saving, a Staging Distribution that is not associated with any Primary Distribution will be generated (as shown in the figure below). Since the number of Staging Distributions has a maximum upper limit (20), when you misuse a useless Staging Distribution, in order not to occupy the quota, you can choose to delete the Staging Distribution to avoid occupying the quota.

6152fd89023dbf32ff4208141ec7c5ab.jpeg

Summarize

Through this article, if the configuration you need to change in the future has certain risks, such as modifying business logic, changing the source server, etc., in order to avoid business interruption or affect user experience caused by the change, using CloudFront's continuous deployment (Continuous Deployment) function will support you to make configuration changes in blue-green deployment to reduce the risks brought by configuration changes to the production environment.

Amazon cloud technology 

CloudFront deployment mini-guide series articles

Amazon CloudFront Deployment Guide (1) - Quickly build CDN content distribution:

https://aws.amazon.com/cn/blogs/china/amazon-cloudfront-deployment-handbook-part-one/

Amazon CloudFront Deployment Guide (2) - Advanced Deployment:

https://aws.amazon.com/cn/blogs/china/amazon-cloudfront-deployment-handbook-part-two/

Amazon CloudFront Deployment Guide (4) - CloudFront Function Basics and Diagnosis:

https://aws.amazon.com/cn/blogs/china/amazon-cloudfront-deployment-handbook-part-four/

Amazon CloudFront Deployment Guide (5) - Using Amazon Edge Technology to Optimize In-Game Resources Update release:

https://aws.amazon.com/cn/blogs/china/amazon-cloudfront-deployment-handbook-part-five/

Amazon CloudFront Deployment Guide (6) - Lambda@Edge Basics and Diagnosis:

https://aws.amazon.com/cn/blogs/china/amazon-cloudfront-deployment-handbook-part-six/

The author of this article

877fb37263b7bdeb444a84145706180b.jpeg

Wang Junxing

Amazon Cloud Technology Edge Product Architect, responsible for the technical promotion of Amazon Cloud Technology Edge services in China. He has many years of practical experience in the field of CDN content distribution and WAF, focusing on edge service design and experience optimization.

3601dd81d0a01a3d10f79e110569bc1e.gif

f3afd3f8558b0e9321cec04f197bf98b.gif

I heard, click the 4 buttons below

You will not encounter bugs!

d13a2a4f297344e029f5332d60330e8d.gif

Guess you like

Origin blog.csdn.net/u012365585/article/details/131863016