How to Installing Magento 2 on AWS Cloud using Terraform

In this article, I will show how to Installing Magento 2 on AWS using Terraform.

Yegor Shytikov
Geek Culture
7 min readApr 28, 2021

--

Magento 2 Cloud Terraform

What AWS resources required for Magento :

First, we need a Magento Load balancer to divide the traffic between the instances :

Magento Load Balancer components

A load balancer serves as the single point of contact for clients. The load balancer distributes incoming application traffic across multiple targets, such as Magento EC2 instances. This increases the availability of your application. You add one or more listeners to your load balancer.

An Application Load Balancer functions at the application layer, the seventh layer of the Open Systems Interconnection (OSI) model. After the load balancer receives a request, it evaluates the listener rules in priority order to determine which rule to apply and then selects a target from the target group (Web/Admin) for the rule action. You can configure listener rules to route requests to different target groups based on the content of the application traffic. Routing is performed independently for each target group, even when a target is registered with multiple target groups. You can configure the routing algorithm used at the target group level. The default routing algorithm is round robin; alternatively, you can specify the least outstanding requests routing algorithm.

You can add and remove targets from your load balancer as your needs change, without disrupting the overall flow of requests to your application. AWS Elastic Load Balancing scales your load balancer as traffic to your application changes over time. Elastic Load Balancing can scale to the vast majority of workloads automatically.

You can configure health checks, which are used to monitor the health of the registered targets so that the load balancer can send requests only to the healthy targets.

Auto Scaling group with Magento instances.

Amazon EC2 Auto Scaling helps you ensure that you have the correct number of Magento instances available to handle the load. You can specify the minimum number of Magento instances in each Auto Scaling group, and Amazon EC2 Auto Scaling ensures that your group never goes below this size. You can specify the maximum number of instances in each Auto Scaling group, and Amazon EC2 Auto Scaling ensures that your group never goes above this size. If you specify the desired capacity, either when you create the group or at any time thereafter, Amazon EC2 Auto Scaling ensures that your group has this many instances. If you specify scaling policies, then Amazon EC2 Auto Scaling can launch or terminate instances as demand on your application increases or decreases.

For example, the following Auto Scaling group has a minimum size of one instance, the desired capacity of two instances, and a maximum size of four instances. The scaling policies that you define adjust the number of instances, within your minimum and a maximum number of instances, based on the criteria that you specify.

We will use EC2 Instance (Server) with c6g.medium instance type with EBS (SSD Disk) Volume with General Purpose 3

How to create Magento 2 Auto Scaling using terraform:

Database to store Magento persistent data.

As a Magento database, we will use AWS RDS AURORA DB.

Amazon Aurora (Aurora) is a fully managed relational database engine that’s compatible with MySQL . You already know how MySQL and PostgreSQL combine the speed and reliability of high-end commercial databases with the simplicity and cost-effectiveness of open-source databases. The code, tools, and applications you use today with your existing MySQL databases can be used with Aurora. With some workloads, Aurora can deliver up to five times the throughput of MySQL without requiring changes to most of your existing applications.

Aurora includes a high-performance storage subsystem. Its MySQL-compatible database engines are customized to take advantage of that fast distributed storage. The underlying storage grows automatically as needed. An Aurora cluster volume can grow to a maximum size of 128 tebibytes (TiB). Aurora also automates and standardizes database clustering and replication, which are typically among the most challenging aspects of database configuration and administration.

Aurora is part of the managed database service Amazon Relational Database Service (Amazon RDS). Amazon RDS is a web service that makes it easier to set up, operate, and scale a relational database in the cloud. If you are not already familiar with Amazon RDS.

The following points illustrate how Aurora relates to the standard MySQL engines available in Amazon RDS:

  • Aurora takes advantage of the familiar Amazon Relational Database Service (Amazon RDS) features for management and administration. Aurora uses the Amazon RDS AWS Management Console interface, AWS CLI commands, and API operations to handle routine database tasks such as provisioning, patching, backup, recovery, failure detection, and repair.
  • Aurora management operations typically involve entire clusters of database servers that are synchronized through replication, instead of individual database instances. The automatic clustering, replication, and storage allocation make it simple and cost-effective to set up, operate, and scale your largest MySQL and PostgreSQL deployments.
  • You can bring data from Amazon RDS for MySQL and Amazon RDS for PostgreSQL into Aurora by creating and restoring snapshots, or by setting up one-way replication. You can use push-button migration tools to convert your existing Amazon RDS for MySQL and Amazon RDS for PostgreSQL applications to Aurora.

We will use RDS Aurora DB Instance with 20 GB storage db.r6g.micro instance class with enabled backup and maintenance mode.

Terraform code will be like this:

Full code You Can Check Here:

There is a small Video on how to provision Magento with Terraform :

Magento installation on Centos and AWS Linux:

Use this repo:

--

--

Yegor Shytikov
Yegor Shytikov

Written by Yegor Shytikov

True Stories about Magento 2. Melting down metal server infrastructure into cloud solutions.