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.
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.
terraform {
source = "git::git@github.com:terraform-aws-modules/terraform-aws-alb.git?ref=v5.1.0"
}include {
path = find_in_parent_folders()
}dependencies {
paths = ["../magento-cloud", "../loadbalancer-internal-securitygroup"]
}dependency "magento-cloud" {
config_path = "../magento-cloud"
}dependency "loadbalancer-internal-securitygroup" {
config_path = "../loadbalancer-internal-securitygroup"
}###########################################################
# View all available inputs for this module:
# https://registry.terraform.io/modules/terraform-aws-modules/alb/aws/5.1.0?tab=inputs
###########################################################
inputs = {
# The resource name and Name tag of the load balancer.
# type: string
name = "WebNodes-ELB"# Controls if the ALB will log requests to S3.
# type: bool
logging_enabled = false# The security groups to attach to the load balancer. e.g. ["sg-edcd9784","sg-edcd9785"]
# type: list(string)
security_groups = [dependency.loadbalancer-internal-securitygroup.outputs.this_security_group_id]# A list of subnets to associate with the load balancer. e.g. ['subnet-1a2b3c4d','subnet-1a2b3c4e','subnet-1a2b3c4f']
# type: list(string)
subnets = dependency.magento-cloud.outputs.public_subnets# VPC id where the load balancer and other resources will be deployed.
# type: string
vpc_id = dependency.magento-cloud.outputs.vpc_id
}
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:
terraform {
source = "git::git@github.com:terraform-aws-modules/terraform-aws-autoscaling.git?ref=v3.4.0"
}include {
path = find_in_parent_folders()
}dependencies {
paths = ["../aws-data", "../magento-cloud", "../webnode-securitygroup", "../webnode-loadbalancer"]
}dependency "aws-data" {
config_path = "../aws-data"
}dependency "magento-cloud" {
config_path = "../magento-cloud"
}dependency "webnode-securitygroup" {
config_path = "../webnode-securitygroup"
}dependency "webnode-loadbalancer" {
config_path = "../webnode-loadbalancer"
}###########################################################
# View all available inputs for this module:
# https://registry.terraform.io/modules/terraform-aws-modules/autoscaling/aws/3.4.0?tab=inputs
###########################################################
inputs = {
# The number of Amazon EC2 instances that should be running in the group
# type: string
desired_capacity = "1"# Controls how health checking is done. Values are - EC2 and ELB
# type: string
health_check_type = "EC2"# The EC2 image ID to launch
# type: strin
image_id = dependency.aws-data.outputs.amazon_linux2_aws_ami_id # magento image for test "ami-04937aae676c8b679"# The size of instance to launch
# type: string
instance_type = "c6g.large"# The maximum size of the auto scale group
# type: string
max_size = "20"# The minimum size of the auto scale group
# type: string
min_size = "10"# Creates a unique name beginning with the specified prefix
# type: string
name = "WebNode-ASG"# A list of security group IDs to assign to the launch configuration
# type: list(string)
security_groups = [dependency.webnode-securitygroup.outputs.this_security_group_id]# A list of aws_alb_target_group ARNs, for use with Application Load Balancing
# type: list(string)
target_group_arns = dependency.webnode-loadbalancer.outputs.target_group_arns# A list of subnet IDs to launch resources in
# type: list(string)
vpc_zone_identifier = dependency.magento-cloud.outputs.public_subnets}
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:
terraform {
source = "git::git@github.com:terraform-aws-modules/terraform-aws-rds.git?ref=v2.14.0"
}include {
path = find_in_parent_folders()
}dependencies {
paths = ["../magento-cloud", "../mysql-rds-securitygroup"]
}dependency "magento-cloud" {
config_path = "../magento-cloud"
}dependency "mysql-rds-securitygroup" {
config_path = "../mysql-rds-securitygroup"
}###########################################################
# View all available inputs for this module:
# https://registry.terraform.io/modules/terraform-aws-modules/rds/aws/2.14.0?tab=inputs
###########################################################
inputs = {
# The allocated storage in gigabytes
# type: string
allocated_storage = "5"# The days to retain backups for
# type: number
backup_retention_period = 0# The daily time range (in UTC) during which automated backups are created if they are enabled. Example: '09:46-10:16'. Must not overlap with maintenance_window
# type: string
backup_window = ""# Name of DB subnet group. DB instance will be created in the VPC associated with the DB subnet group. If unspecified, will be created in the default VPC
# type: string
db_subnet_group_name = dependency.magento-cloud.outputs.database_subnet_group# Aurora database engine type, currently aurora, aurora-mysql or aurora-postgresql
# type: string
engine = "aurora-mysql"# Aurora database engine version.
# type: string
engine_version = "5.7.12"# The name of the RDS instance, if omitted, Terraform will assign a random, unique identifier
# type: string
identifier = "mysql"# The instance type of the RDS instance
# type: string
instance_class = "db.r6g.large"# The window to perform maintenance in. Syntax: 'ddd:hh24:mi-ddd:hh24:mi'. Eg: 'Mon:00:00-Mon:03:00'
# type: string
maintenance_window = ""# Specifies the major version of the engine that this option group should be associated with
# type: string
major_engine_version = "5.7"# Specifies if the RDS instance is multi-AZ
# type: bool
multi_az = false# Password for the master DB user. Note that this may show up in logs, and it will be stored in the state file
# type: string
password = "kjj2flgyM0Ml"# The port on which the DB accepts connections
# type: string
port = "3306"# Username for the master DB user
# type: string
username = "bass"# List of VPC security groups to associate
# type: list(string)
vpc_security_group_ids = [dependency.mysql-rds-securitygroup.outputs.this_security_group_id]}
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: