Title: " E-Commerce Website Deployment- Containerization using Amazon ECR ,ECS and Terraform as IaC tool.
Tags: devops, aws, cloud, docker
Published: 2024-05-13 13:10:19

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjjhbpy2vzzw1h92cnt7.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8no20emawywdceqzbc2v.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vo7qwrmubor9yr9q5xnt.png)

**Dockerfile**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23o3vi0dkmfifph9ahep.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27l0xiswxfjbigvtdseq.png)

**Amazon ECS –** This module will create:

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru5qvobznoaf7vwb8ljv.png)




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jab1xbu79cjkdcfxbkr1.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/icparhfb91ivy39lmxkt.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilbuyc9cn08cz0dawk4c.png)

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

Run `Terraform init`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/poebaecaiffyiv16gkpo.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jev3j56c72or0zpn2gka.png)

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

Run `Terraform plan`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d3cn60aunka4489b9yq.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n172wxmkcmm6289fhn0t.png)

**Confirm Resources deployed on AWS**

**VPC and its dependencies**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngyidpkvptt1x2np70ce.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/667xwo0pca12w4f3ww7r.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60oq5mpfnstt9a5glpkz.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3sb7bry9a91et73gkm4.png)




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8m3e2pmwiijnwmlt6zkw.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9hbcy4xw1d543we4onb.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/momwj3osngwlo65a3eor.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frzukow857tdtxdimqfs.png)

**AWS ECR**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/083zot0h57hdmfzokv83.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1uhmovpyeb7rw0cf0lkw.png)

**AWS ECS**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0bxqklrjld3oysa89ej.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ze5k64hc3vzchjrjxxa.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2ypsvtj5q18ttjnm4jj.png)

**ACM and Route53**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nocjaiuptjyhv0gblpl1.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0zorrsz11e977kk1m8g.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yq9it73vf7kigz7s8zkz.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zgwxtcbyb2xraczo420.png)


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0butditt420a2zr8unhl.png)

To Clean up: Run `terraform destroy` 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvarqvfbg5yona8a6cte.png)

Thanks for Reading!!!