Image Blog Running Magento on Zend Server Cluster
March 22, 2017

How to Use an AWS CloudFormation Template and Zend Server to Autoscale Magento

AWS
Zend Server

Using an AWS CloudFormation template and Zend Server can help you automatically scale Magento. This is important for many reasons. When you use Magento to support an online store, it's important to ensure maximum scalability. That's because any interruptions to high availability and fast performance can negatively impact your revenue.

For this walkthrough, we'll focus on how to complete this set up on AWS, but you can use the same basic steps for other deployment options.

Overview: AWS CloudFormation Template + Zend Server

To achieve our goal, here are the steps we'll take:

  1. Subscribe to Zend Server on AWS Marketplace and download the CloudFormation template.
  2. Deploy CloudFormation stack from the template downloaded. After deploying stack, we'll have Zend Server Cluster that will automatically scale depending on load on the nodes.
  3. Deploy Magento on Zend Server Cluster.

This is the final step after which we will have Magento running on high available and auto scaled cluster.

Prerequisites

Since we are going to run cluster on AWS you will need AWS account. Additionally it is expected that you know basic AWS terms like VPC, EC2 Instance, RDS Instance, etc. Additionally you have to be familiar with basic CloudFormation terms. Additionally I expect you to have a VPC for deployment (you can use default VPC if you want) and EC2 key pair ready for use.

Optional: Use an AWS CloudFormation Template Generator

You can use an AWS CloudFormation template generator to get started. Zend has a CloudFormation template generator that has some additional functionality like adding additional RDS server or ElastiCache service to template.

Or Use an AWS CloudFormation Template

CloudFormation allows allocation of AWS resources as described by template files which utilize JSON format. CloudFormation operates with stacks of resources where each stack is created using a template.

AWS Marketplace has basic template that can be used to spin cluster.

Since we are going to deploy sample Magento web application, we'll use same RDS instance that Zend Server uses for storing cluster information. However, I strongly recommend to setup separate RDS instance for Magento database when deploying real production environment (either by adding another RDS in CloudFormation template or by setting it up manually).

You can find list of Zend Server products on AWS Marketplace. Zend Server is available on Ubuntu Server 14.04 LTS or RHEL 7 Operating System. Additionally there are three Zend Server editions - Developer, Professional, and Enterprise (note that Developer Edition does not have clustering capabilities and is intended for development and testing use only). There are also two versions of PHP supported - 5.6 and 7.0. For the purpose of our demonstration we will use PHP 7.0 - Zend Server Professional Edition (Ubuntu).

How to Use a CloudFormation Template With a Zend Server Cluster

Here are the steps to deploy Zend Server Cluster on AWS:

  1. Login to your AWS account.
  2. Open PHP 7.0 – Zend Server Professional Edition (Ubuntu) or other Zend Server product page on AWS Marketplace (note the Developer Edition has no cluster support).
  3. Click yellow “Continue” button in the right column.
    Zend Magento 1
  4. Ensure that “Manual Launch” tab is selected.
  5. Click yellow button “Accept Software Terms” on the right column. If you do not have such button, then you already have subscription to Zend Server product and should skip to step 7.
    Zend Magento 2
  6. You will be presented “Thank you” page with information and next steps. Read them and click blue button “Return to Product Page”. You will be taken back to launch page (reminder – ensure that “Manual Launch” tab is selected).
    Image Blog Zend Magento3
  7. In “Region” section, select region in which you want to launch your cluster.
  8. In “Deployment Options” select “Zend Server Cluster” (you can click “view” link next to it to see description schematic diagram of what Zend Server Cluster consists of)
  9. Click yellow “Launch with CloudFormation Console” button. If the button is grey and you see a message “Subscription is not ready. You will be able to launch in a few minutes.” just wait a few minutes (from what I know it takes something like 5 minutes until subscription is ready).
    Image Blog Zend Magento 4
  10. You will be taken to CloudFormation Create Stack wizard on AWS Console. URL for template is already prefilled, so all you need to do at this step is click “Next”.
    Zend Magento 5
  11. Next you have to fill all parameters that are required by the template. Please read carefully descriptions of parameters. A few notes:
    · Subnets – you must specify at least 2 subnets in different Availability Zones.
    · KeyName – existing EC2 key pair to allow SSH connections to instances of cluster.
    · ClusterPassword – password to access Zend Server UI, must be at least 4 characters long.
    · DatabasePassword – password for admin user on RDS instance, must be at least 8 characters long.
    · Minimum and maximum size of auto scaling group – specifies limits to scale number of nodes. Take into account that CloudFormation template spins single server separately from auto scaling group. We want our cluster to automatically scale between 2 and 10 nodes as needed, therefore minimum must be set to 1 and maximum to 9.
    · SSHLocation – IP address range from which SSH connections are allowed to nodes of cluster. For example w.x.y.z/32 to specify single IP address. 0.0.0.0/0 to allow SSH from anywhere.
  12. Once all parameters are filled, click “Next”.
    Image Blog Zend Magento 6
  13. Options allow you to customize some CloudFormation allocation parameters. It is safe to leave defaults. Just click “Next” to go on.
  14. Final step is “Review”. Here you will be presented with summary. One important note here is to ensure that checkbox “I acknowledge that AWS CloudFormation might create IAM resources” is checked. Then click “Create” button.
    ​​Zend Magento 7
  15. All that is left now is to wait till CloudFormation sets up Zend Server Cluster for you. It will take a few minutes.
    ​​Zend Magento 8
  16. Once cluster is ready, click on stack and open “Outputs” tab. There you will find DB credentials, URL of Load Balancer, and URL of Zend Server UI – you will need those to deploy Magento in next section.
    Zend Magento 9

Deploying Magento

We are going to deploy Magento 2.1.2 which is the latest version of Magento available as of writing of this blog. We are going to deploy Magento using Zend Deployment which simplifies deployment of PHP applications on Zend Server and provides additional features like customizable hooks for different stages of deployment and rollbacks.

Zend Deployment deploys Zend Deployment Package files, which is basically a zip archives with some predefined structure. You can read more about Zend Deployment here.

Zend Server ships with a few demo ZPK (Zend Application Package) applications including Magento. Unfortunately Magento ZPK that is shipped with Zend Server 9.0 is Magento 1.9.1.0. Although next version of Zend Server will have new Magento version, it is not ready yet. So for this article I developed my own Magento ZPK which utilizes Magento CLI in hook scripts to automate deployment. ZPK is available freely here. Please feel free to use this package as starting point for your customizations as needed.

    Steps to deploy Magento on Zend Server Cluster:

    1. Open Zend Server UI (use link in "ZendServerUI" output of CloudFormation stack deployment) and login with password you specified during the CloudFormation template deployment.
    2. In Zend Server select in menu on the left "Applications" > "Manage Apps".
    3. Click on "Deploy application" button on top of the page. Application deployment wizard will be shown.
      Zend Magento 10
    4. Click "Choose File" button and select Magento ZPK you downloaded.
      Zend Magento 11
    5. Wait till ZPK is uploaded and then click "Next" button.
    6. Next step is "Application Details" where you can customize path under which application is deployed and Virtual Host. Deploying Magento to root of Virtual Host on AWS can lead to unwanted result because ELB expects our nodes to return HTTP 200 when request is made to / on server, while Magento will return 302. This will lead to a situation where ELB will consider all nodes of cluster unavailable and return error. This can be avoided by adding health check file to Magento ZPK and modifying ELB configuration to check against this file. But this is outside of scope of this article. Instead we will simply deploy Magento under "magento" path. For this just enter "magento" in path and click "Next" to continue. all values at this step at their defaults and just click "Next".
      Zend Magento 12
    7. Next step is "License Agreement". You have to agree to Magento License Agreement by setting checkbox "I have read and agree to the license agreement" and clicking "Next".
    8. Next step is "Prerequisites Validation" that validates that PHP configuration and extension requirements are met. If validation is not passed, you will have to go to "PHP" > "Extensions" and check PHP and it's extensions configuration there (Tip: to easily find needed directive on "PHP Extensions" page, you can use search box located in top right corner). Once validation is passed, click on "Next" button.
    9. Now you have to fill in all parameters of Magento ZPK. For this demo use database parameters that are available on CloudFormation outputs tab. Other than that, please read descriptions of parameters carefully. Once all parameters are filled, click "Next" to continue to next step.
    10. Last step before deployment shows you everything for a review before starting deployment. Click "Next" to start deploying Magento application.
      Zend Magento 13
    11. Wizard will now close and we are back to list of applications, but this time it's not empty. Once deployment is finished the application will appear as deployed. This means that we can take ELB URL from CloudFormation output, append "magento" to it and see our shop up and running.

    Zend Server Cloud Solutions