Implementation of an E-Commerce System on AWS in an automated way using Terraform and Ansible

Olusegun Omotunde
7 min readSep 22, 2023

--

Project description:

In this project, I worked as a Cloud Engineer using DevOps, where I created and implemented an e-Commerce MVP (Minimum Viable Product) on AWS in less than 2 hours and in an automated way using Terraform and Ansible (Infrastructure as Code — IaC).

I provisioned the infrastructure in an automated way using Terraform and Ansible to automate the configuration management process, software installation and package management of the EC2 instance. I also used Magento, PHP, MySQL, and Redis to complete this project.

I implemented the E-commerce MVP deployment project in two phases:

  • In the first phase, I will use Terraform to prepare, provision and deploy the EC2 infrastructure in my AWS environment in an automated way.
  • In the second phase, I will use Ansible to automate the software configuration management process and deploy the software stack inside the EC2 instances. The software stack for this project includes PHP, MySQL, Redis and ADOBE Magento.

Phase 1 —- Terraform to provison and deploy EC2 instance

Terraform is a tool to provision and deploy infrastructures on cloud providers like AWS using codes (Infrastructure as Code — IaC).

The steps in this phase are listed below:

  • Open AWS CLoud Shell
AWS Cloud Shell
Installing Terraform on AWS Cloud Shell
  • Install Terraform in AWS Cloud Shell using the commands below:

- sudo yum install -y yum-utils
- sudo yum-config-manager — add-repo -https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
- sudo yum -y install terraform

Terraform Installation on AWS Cloud Shell
  • Download the Terraform files on AWS Cloud Shell
    - mkdir final_project
    - cd final_project
Downloading the Terraform FIles on AWS Cloud Shell
  • Open the Terraform main files using text editor in Cloud shell “ nano vi main.tf “
  • Update my VPC information in main.tf with my default VPC from AWS
  • Update my security key in main.tf with the EC2 key code created
  • Save and exit the text editor
Updating my VPC and security key information in main.tf
  • Running Terraform to deploy the EC2 VM:

cd terraform
terraform init
terraform plan
terraform apply (enter yes)

Creating Terraform files
Initializing Terraform
Terraform apply
Terraform Ec2 VM creation
E-commerce Ec2 VM/Instance has been created
Successfully creation

PHASE 2 —- Using Ansible to Configure and Deploy Software Stack in AWS EC2

Ansible is a suite of software tools that enables infrastructure as code. It is open-source and the suite includes software provisioning, configuration management, and application deployment functionality. With Ansible, I am going to install all the softwares and libraries required by the E-commerce website.

The steps in this phase are listed below:

  • Create a Magento account on https://marketplace.magento.com/ and also create and save the public and private key from the Magento account which will be used later to deploy and run Ansible.
magento website
copying my public and private key from my magento account to a notepad
  • Create a new ssh connectivity with my new EC2 instance by going to the EC2 management console on AWS and copying the public IPv4 address. Open terminal on local mac to make a connection. In the terminal, navigate to the folder where security key for the Ec2 instance is saved, then form a remote connection using the command $ ssh ec2-user@”publicipv4-from-ec2instancecreated” -i “security-key-from-ec2-instance”.
terminal/gitbash on local computer
making a ssh connection with ec2 instance on local computer
  • After connecting to the EC2 instance Via SSH, install Ansible in the Ec2 VM using the commands below:

sudo yum-config-manager — enable epel
sudo yum install ansible -y

Installing Ansible in the EC2 VM
Ansible successful installation
  • Download Ansible playbooks inEc2 VM. Ansible Playbooks offer a repeatable, re-usable, simple configuration management and multi-machine deployment system, one that is well suited to deploying complex applications.
downloading my ansible files
ansible files are now in Ec2 vm
  • Edit and save Ansible file Parameters. Do this by navigating to the magenta directory, change the directory to group_vars and navigate to all_yml file, open it with nano text editor and change the magento domain and server_hostname by replacing it with the EC2 publicIPV4 address and also replace the repo_api_keys with the public key from the Magento account and the secret_key with the private key from the Magento account. After this is completed, run Ansible to deploy the stack of software tools for the E-commerce website using the command “ansible-playbook -i hosts.yml ansible-magento2.yml -k -vvv — become”
navigating to the magento directory
nano text editor to make changes in the all.yml file
Run Ansible to deploy the stack of tools for the e-commerce
Run Ansible to deploy the stack of tools for the e-commerce
Run Ansible to deploy the stack of tools for the e-commerce
  • Test the E-commerce website by copying and pasting the Ec2 public Ip in the browser.
  • Enter the user and password of the Magento Admin available in the file: group_vars/all.yml
E-commerce webiste
  • E-commerce website is working but some changes need to be made.
  • In the magento dashboard, use the left menu and navigate to Content-Configuration
  • Click on Edit Action for Default Store View
  • Expand HTML Head and change the Default page title to E-commerce Store
  • Expand the Header section and upload The E-commerce logo l and change the Welcome text to whatever message you want, I used “Weclome to the tab store!” and click on Save Configuration.
  • Navigate to Catalog → Products to make some changes to Products
  • Cache Refresh (Flush it)
  • To add new products, prices and quantity available: Catalog > Products > Add product > The Cloud Bootcamp T-Shirt
    Content > Pages > Edit Home Pages
    Click Content > Erase content
  • Go to the Contents/pages: Content > Pages > Edit Home Pages
    Content > Erase content
    Insert Widget > Widget type: Catalog New Products List > Insert Widget
  • Finally, add a demo product T-shirt to verify that all changes have been made: Price: 80
    Quantity: 100
    Images And Videos > Add images
    Add images under Images and Videos and click save.
E-Commerce Website

Success!

The E-commerce website is setting up to allows us to display our products as well as allow checkout” add to cart”

Technologies Used

This project leveraged many services from the Amazon AWS. I utilized terraform to deploy the AWS Ec2 infrastructure within Amazon Virtual Private Cloud (VPC). I utilized Ansible for the configuration management and to automate the installation of PHP, MySQl, Redis and Magento.

Summary

The Chief Technology Officer (CTO) of TCB asked me to quickly deploy a Minimum Viable Product (mVP) e-commerce website in AWS cloud. The mission aimed to validate whether The TCB store is a viable product before creating a far more robust website. The creation of the MVP was successful, and I used some common DevOps tools along the way.

If this site sees more traffic in the future, then I would update it to reduce redundancy by using multiple Application Zones, Edge Locations, Elastic Load Balancers, RDS databases, and other features that would add reliability, scalability and reduce latency.

--

--

Olusegun Omotunde

Data Engineer with focus on Cloud & DevOps | AWS | Microsoft Azure | Google Cloud | Oracle Cloud |