Quickly Build an E-commerce Platform Based on Amazon Cloud Technology Serverless Service——Performance

Advantages of using Serverless to build a stand-alone website

In the traditional architecture mode, if a large e-commerce promotion is required, computing resources need to be pre-configured in advance to support high concurrent access, which will cause waste of computing resources and increase the workload of operation and maintenance. This article introduces a new way of deploying WordPress and WooCommerce in Amazon Lambda. Lambda is a serverless computing method that can run code without provisioning resources, automatically respond to code execution requests of any scale, from a dozen events per day to hundreds of thousands of events per second, and pay for computing time (in milliseconds) ), truly billing according to usage, so as to save preset resources and operation and maintenance costs. This feature of Lambda makes Lambda more and more popular, and more and more customers choose Lambda to deploy applications, including web applications. Customers familiar with Lambda may know that Lambda is triggered based on events. For web applications, API Gateway needs to be used to receive HTTP requests and convert HTTP requests into Lambda events to trigger Lambda to run.

The Amazon cloud technology developer community provides developers with global development technology resources. There are technical documents, development cases, technical columns, training videos, activities and competitions, etc. Help Chinese developers connect with the world's most cutting-edge technologies, ideas, and projects, and recommend outstanding Chinese developers or technologies to the global cloud community. If you haven't paid attention/favorite yet, please don't rush over when you see this, click here to make it your technical treasure house!

In the past, for existing web applications, it was necessary to carry out a lightweight transformation of the application code to handle Lambda events. For many e-commerce customers who use mature components like WordPress and WooCommerce, it is impossible to carry out code transformation, so they cannot take advantage of Lambda? the answer is negative. Using Lambda's new function Lambda container images and the open source component Amazon Lambda adapter allows WordPress to run in Lambda without any code modification. This solution is deployed to Lambda by packaging Lambda Adapter, WordPress, WooCommerce and other necessary plugins into containers. At the same time, this solution also uses the new function of Lambda, Function URL, to replace API Gateway, which can directly access Lambda through HTTP(s) through Function URL, thus saving the cost brought by API Gateway. The user's dynamic request is sent back to the Lambda URL through CloudFront to trigger the Lambda to run. Inside the Lambda, the Lambda Adapter receives the Lambda event and converts it into an HTTP request that WordPress can handle. This makes it possible to run WordPress in Lambda without modifying the code.

This article focuses on the Lambda container image and Lambda Function URL. For the implementation details of the Lambda Adapter, please refer to this blog.

Lambda container image

To run a container on Lambda, the container image needs to contain the runtime interface clients of the runtime API  to manage the interaction between Lambda and function code. Customers can include the runtime interface client in their images to support running on Lambda. Amazon provides a set of open source base images that can be used to create container images . These base images include runtime interface clients  . The Lambda image is read-only, but the function code can access a writable /tmp directory with 512 MB of storage. This scenario uses Docker to create the image. The Dockerfile uses the base image Amazon Linux 2 provided by Amazon, and uses bedrock to manage the installation of WordPress and plugins. Some necessary plug-ins are pre-configured in this solution, and customers can modify the configuration of bedrock to add the required plug-ins.

Lambda Function URL

You can now trigger Lambda to run by creating a Function URL that supports using HTTP(s) to access this URL. When the function of Function URL is not released, building web applications based on Lambda needs to be combined with API Gateway to receive HTTP(s) requests. However, in the scenario of deploying WooCommerce on Lambda, because WordPress is packaged into a container, only a single Lambda Function is needed. The role of API gateway is to convert HTTP requests into Lambda events, and the advanced functions provided by API Gateway, such as API Management, request authentication, etc., are not required. Therefore, with the function of Function URL, it can replace the role of API Gateway in this scenario, and will not increase the cost of Lambda, but also save the cost of API Gateway.

load test

On the basis of the previous blog, we use the WordPress plug-in Blocksy to quickly build a Starter Site, and use this Site as a test object.

This solution has been open sourced on Github, visit this [Repo] ( https://github.com/aws-samples/serverless-WooCommerce-workshop) to get the complete code.

In the test/k6 folder, this solution also provides k6 scripts for performance testing. It simulates the complete process from the user entering the homepage, selecting products, adding them to the shopping cart, updating the address, and submitting the order. For specific instructions, refer to the test/readme.md file.

main.js is used as the entry file of the test, simulating the scene where 100 users are online in the first 5 minutes, 1000 users are online in the middle 10 minutes, and 100 users are online in the last 5 minutes. Readers can modify the main.js file according to the test needs.

Because the RDS Aurora mysql instance and the Elasticahe Redis cluster preset in the default CDK template are too small, they are not suitable for testing. Modify the CDK code cdk/lib/woocommerce-stack.ts here, and use r5.4xlarge of RDS Aurora mysql. Elasticahe Redis cluster changed to r5.xlarge. Customers can also change the scale by themselves for a wider range of tests.

image.png

Update resources with the following command.

make diff
make deploy

Here we use a c5.xlarge Amazon Linux 2 EC2 for testing. And install the CloudWatch Agent to upload the indicators generated by k6 to CloudWatch for visualization. Note that EC2 needs to have permission to write to CloudWatch Metrics, here we use EC2 Role to grant permission. By creating a Role, select the hosting policy CloudWatchAgentAdminPolicy in the figure below, and bind this Role to EC2.

image.png

Use the following commands to install and configure k6 and CloudWatch Agent, and run k6 for testing.

sudo yum -y install https://dl.k6.io/rpm/repo.rpm
sudo yum -y install --nogpgcheck k6
sudo yum -y install git 
git clone https://github.com/aws-samples/serverless-woocommerce-workshop.git
cd ~/serverless-woocommerce-workshop/test/k6
sudo yum install -y amazon-cloudwatch-agent
cat << EOF > cw-statsd.json
{
    "metrics": {
        "namespace": "k6",
        "metrics_collected": {
            "statsd": {
                "service_address": ":8125",
                "metrics_collection_interval": 1,
                "metrics_aggregation_interval": 0
            }
        }
    }
}
EOF
sudo amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:./cw-statsd.json
K6_STATSD_ENABLE_TAGS=true k6 run --out statsd -e HOSTNAME=<your WooCommerce website domain name> main.js

The figure below is the statistical result of the k6 test run.

image.png

In CloudWatch's k6 metrics, you can see the number of orders completed per second and the concurrency of Lambda. It can be seen that Lambda will automatically scale as the request/order volume increases. It can be seen that Lambda can handle the high and low peaks of traffic without any operation.

image.png

Summarize

On the basis of the previous blog, this blog introduces the advantages of the serverless website building solution, and conducts a load test on WordPress based on the serverless service. It can be seen that Lambda can automatically cope with traffic peaks and lows without any operation and maintenance operations, which greatly saves operation and maintenance. maintenance cost. Readers can also modify the test script according to their own needs to conduct larger-scale performance tests.

appendix

The author of this article

image.png

Wang Qixiang  , an Amazon solution architect, is responsible for the architecture consulting and design implementation of Amazon cloud computing solutions. She has rich experience in solving customers' practical problems and is keen on the research and application of deep learning.

image.png

Xu Changyue  Amazon solution architect, responsible for Amazon-based cloud computing solution architecture consulting and design, implementation and promotion, good at software development, has rich experience in solving practical problems of customers.

Article source: https://dev.amazoncloud.cn/column/article/630b30932ecbae73705ffb63?sc_medium=regulartraffic&sc_campaign=crossplatform&sc_channel=CSDN

Guess you like

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