Russ Does Tech

Building Cloud and Cybersecurity skills

Automating the implementation of an E-Commerce System on AWS using Terraform and Ansible

In another project based in a real-world scenario, I worked as Cloud Engineer utilizing DevOps methodologies, where I created and implemented an e-Commerce MVP (Minimum Viable Product) on AWS in under 2 hours by employing automation using Terraform and Ansible (Infrastructure as Code — IaC).

I provisioned the infrastructure in an automated fashion using Terraform to automate the infrastructure deployment & configuration, and Ansible to automate the installation of the software stack on the EC2 instance. The software stack consisted of Adobe Magento, PHP, MySQL, and Redis.

To prepare for the project, I first created a Magento account on marketplace.magento.com, and I then signed in and created a Magento2 access key.

In the AWS Management Console, I launched the AWS Cloud Shell, and ran the commands to install the yum-utils, add the hashicorp repo, and install Terraform.

Next, I downloaded and unzipped the Terraform sample files and updated the main.tf Terraform file with the existing VPC ID and SSH key name. I then ran the following Terraform commands to the deploy the ‘ecommerce1’ EC2 instance and the ‘allow_ssh_http’ security group :

> terraform init
> terraform plan
> terraform apply

Once completed, I confirmed the resources were up and running using the Amazon Management Console.

EC2 Instances
Security Groups

The next step was to SSH into the ‘ecommerce1’ EC2 instance, and install Ansible.

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

I then downloaded and unzipped the sample Ansible playbooks, and updated the magento_domain and server_hostname values in the group_vars/all.yml file with the public IPv4 address of the EC2 instance, and repo_api_key and repo_secret_key with the public and private values from the Magento2 access key that I created during the preparation phase.

I then ran the ansible-playbook command to deploy the software stack of Magento, PHP, MySQL, and Redis for the e-commerce website.

Once Ansible completed, I input the IPv4 address of the EC2 instance into a new browser tab, and confirmed that the e-commerce website loaded successfully, as shown below.

E-commerce website Home page

I then logged into the administrator portal of the e-commerce site, and made some updates to the page such as adding a logo, some welcome text, a sample product with images, and a ‘New Products’ widget.

E-commerce website Products page

After making the updates I reloaded the e-commerce website and confirmed that the site displayed the logo, welcome message, and the new product, and that I was able to add the product to the cart successfully.

E-commerce website Home page displaying new products

To remove the deployed resources after the MVP had been demonstrated to the stakeholders, I re-installed Terraform in AWS Cloud Shell using the same commands used previously, and then issued the ‘terraform destroy’ command. I then confirmed in the AWS Management console that the EC2 instance and the associated security group had been removed.

This project demonstrated how powerful the automation capabilities provided by Terraform and Ansible in combination with AWS truly are. In the space of a few hours a complex test environment can be deployed and tested. This enables businesses to experiment with new technologies at a very low price of entry, and then tear down the environment with no on-going expense, as opposed to deploying similar resources in physical data center, and being left with the hardware whether or not the project goes live. I really enjoyed this project, and look forward to working on more projects with AWS, Terraform, Ansible, and other automation tools.