DEV Community

Cover image for Title: " E-Commerce Website Deployment- Containerization using Amazon ECR ,ECS and Terraform as IaC tool.
Gbenga Ojo-Samuel
Gbenga Ojo-Samuel

Posted on

Title: " E-Commerce Website Deployment- Containerization using Amazon ECR ,ECS and Terraform as IaC tool.

Introduction

In my recent post, I provided a summary of our journey thus far in implementing an e-commerce website, evolving from a basic to a more advanced architecture. If you missed the post, you could find it here:

https://www.linkedin.com/posts/samuel-ojo-oluwagbenga_in-the-last-two-weeks-we-embarked-on-a-journey-activity-7190371967135338496-FQcL?utm_source=share&utm_medium=member_desktop

In this article, we will delve into the utilization of containers in our website deployment strategy. The benefits of using containers include:

  1. Isolation: Containers provide a lightweight, isolated environment for running applications, ensuring that they operate independently without interference from other processes.

  2. Portability: With containers, we can package our application along with its dependencies into a single unit, making it easy to deploy and run consistently across different environments, from development to production.

  3. Scalability: Containers allow us to scale our application horizontally by spinning up multiple instances of the same container, enabling us to handle increased traffic and workload demands more effectively.

  4. Consistency: Containers ensure consistency between development, testing, and production environments, minimizing compatibility issues and reducing the likelihood of deployment errors.

We'll leverage AWS Elastic Container Registry (ECR) to store our Docker images, Amazon Elastic Container Service (ECS) to deploy our containerized website and Terraform as our infrastructure-as-code tool.

Before diving into the utilization of containers in our website deployment strategy, it's essential to understand the prerequisites.

  • Docker Account: A basic understanding of Docker containerization technology is required to comprehend container deployment concepts effectively. Ensure that docker daemon is running on your local machine.
  • AWS Account: Access to an AWS (Amazon Web Services) account is necessary to utilize AWS Elastic Container Registry (ECR) and Amazon Elastic Container Service (ECS) for container storage and deployment.

Step 1: Clone the Terraform Code

By cloning the Terraform code, we'll have access to the infrastructure-as-code configurations needed for our deployment process.

Clone Repository: Use the git clone command to clone the Terraform code repository to your local machine. Ensure that you have Git installed and configured on your system.

https://github.com/7hundredtech/Dynamic_website_EC2_Terraform.git

Navigate to Repository: Change directory to the folder name containerized_website.

Image description

Step 2: Reviewing the Content of the Terraform Codes
Now that we've cloned the Terraform code repository, let's take a closer look at its contents to familiarize ourselves with the infrastructure-as-code configurations.

Image description

We are introducing Amazon ECR and Amazon ECS modules into the existing Terraform codes used in the previous articles.

Amazon ECR – This module will create:

  • private repository on Amazon ECR
  • an ECR policy that we enable the AWS user account to get and put images in the repository.
  • build Docker image from the app folder.
  • push the image to the repository.

Image description

Dockerfile

Image description

Docker Provider- we will be using a docker provider with the required authentication to AWS ECR

Image description

Amazon ECS – This module will create:

  • an ECS cluster
  • a task execution role and policy
  • a task definition
  • ECS service

Image description

Image description

Step 3: Running Terraform Commands
Below main.tf file defines all the infrastructure that will be deployed after running the terraform commands

Image description

Image description

Initialize: Initialize Terraform in the project directory to download necessary plugins and modules.

Run Terraform init

Image description

Image description

Plan: Generate an execution plan to preview the changes that Terraform will make to the infrastructure

Run Terraform plan

Image description

Apply: Apply the Terraform execution plan to create infrastructure resources as needed. Respond with yes to confirm the execution plan.

Run Terraform apply

Image description

Confirm Resources deployed on AWS

VPC and its dependencies

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

AWS ECR

Image description

Image description

AWS ECS

Image description

Image description

Image description

ACM and Route53

Image description

Image description

Image description

Update the Nameservers on your domain name registrar. Am using Namecheap as its updated as shown below

Image description

Step 4: Website Test
To confirm that our containerized website is up and running, we load our custom domain on our web browser.

Image description

To Clean up: Run terraform destroy

Image description

Thanks for Reading!!!

Top comments (0)