top of page
  • Writer's pictureTaylor Etheredge

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

Updated: Nov 10, 2023

In another project based in a real-world scenario, I worked as 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.


Here is a diagram of the services and technologies used in this scenario:

The first thing to get started is to install Terraform inside the AWS Cloud Shell so that the infrastructure can be deployed. Run the following commands:

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

Now that Terraform is installed in our environment we can initialize the environment with:

terraform init

Here are the Terraform files that were used to deploy the infrastructure on AWS:


main.tf


variable "vpc_id" {
    default = "default-vpc"
}

variable "key_name" {
    default = "ssh-key"
}

resource "aws_instance" "ecommerce1" {
  ami           = "ami-032930428bf1abbff"
  instance_type = "t3a.large"
  key_name = var.key_name
  vpc_security_group_ids = [aws_security_group.allow_ssh_http.id]
  associate_public_ip_address = true

  tags = {
    Name = "ecommerce1"
    ambiente = "bootcamp"
  }
}

resource "aws_security_group" "allow_ssh_http" {
  name        = "allow_ssh_http"
  description = "Allow SSH and HTTP traffic on EC2 instance"
  vpc_id      = var.vpc_id

  ingress {
    description = "SSH to EC2"
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    description = "HTTP to EC2"
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "allow_ssh_http"
  }
}

You will need to replace the variables that are defined, such as vpc_id and key_name, with your correct values in order for the plan to run.


provider.tf


provider "aws" {
    region = "us-east-1"
}

Replace the region with the correct AWS region you want to deploy to.


Now run the following command to apply the infrastructure in AWS.

terraform apply

This command will build out the EC2 instances as well as the security group to allow SSH access into the instances and allow all traffic out of the instances.


Next we need to connect to the EC2 instance over SSH and then install Ansible with the following commands:

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

So now I pulled in our Ansible configuration files by running:

wget https://tcb-bootcamps.s3.amazonaws.com/bootcamp-aws/en/final-project-ansible-magento2.zip
unzip final-project-ansible-magento2.zip

Now I then had to edit and set the group_vars accordingly to my environment, such as the Magento repo keys, Magento domain and the server hostname. Once that is done I ran the following command to deploy the software and application:

ansible-playbook -i hosts.yml ansible-magento2.yml -k -vvv --become

This command will run the playbook with the hosts specified in our case the localhost, in verbose mode and have sudo privileges.


At this point you can now go the site at http://<EC2_PUBLIC_IP>. This is just a test site to see how quickly I could get this up and running on the AWS infrastructure. You will need to implement a signed certificate if you want this site to go live.


Here is a final diagram of what the architecture looked liked:


Hopefully you learned something new today with Ansible and Terraform and how to use those two technologies to deploy to the AWS infrastructure. Thank you for reading and stay tuned.


25 views0 comments
bottom of page