Continuous Deployment of Website on an AWS Fargate

Prerequisite:

We have to have a website in php/html with the database connection in php format. Here we have used html for website designing the form which contain the registration of the student and in the form we have provided the action of the php which contain basic the configuration of the database which is required to store the information.

And most important of having AWS Account to deploy the website and host using AWS Fargate and github credentials.

Structure of website files:

Services Used:

1. AWS CodeCommit: to commit the files of php and other for deployment

2. AWS CodeBuild: to build the files which is stored in CodeCommit

3. AWS CodePipeline: to automatedly deploy website

4. AWS ECR: repository to store image

5. AWS Fargate: to creating cluster of the image

6. AWS RDS: for database storage

Following are the steps to implement

1. Push Website code on AWS:

before pushing the files we have to create Codecommit repository

Now we have to clone repository to our local machine using AWS CLI

>> git config –global user.name “<USERNAME>”

>> git config –global user.email “<EMAIL>”

>> git clone <HTTPS URL>

>> cd php

Copy the above mentioned files in the current directory

>> git add .

>> git commit -m “initial commit”

>> git push

While pushing you will be asked to enter the git credentials which you can generate in IAM Console under your IAM User

2. Create Project in CodeBuild

Project Configuration

>> Project name: php-docker

Source

>> Source Provider: AWS CodeCommit

>> Repository: php

>> Reference Type: Branch

>> Branch: master

Environment

>> Environment Image: Managed Image

>> Operating system: Ubuntu

>> Runtime: Standard

>> Image: Choose any

>> Image version: by default

>> Environment type: Linux

>> Check the privilege checkbox

>> Service Role: New service role

Buildspec

>> Build Specification: use a buildspec file

Now click on Create Build Project

We have to add 2 more policies to IAM role which we have created during Build Project and it will give access to RDS and ECR Registry, they are

1. AmazonRDSFullAccess

2. AmazonEC2ContainerRegistryFullAccess

3. Create Repository in ECR

we have to create repository in ECR to push images

4. Create cluster in AWS Fargate

Now we will create cluster using AWS Fargate which will create serverless-platform on which we can deploy our website.

>> Open ECS console and click on Get Started

Step 1: Container and Task

Container Definition

>> Select Custom and click on configure

>> Container Name: Docker-Website

>> Image:<Copy URL of ECR Repository>

>> Memory Limit: Soft Limit: 512

>> Port Mapping: 80

>> Click on Update

Click on Next

Step 2: Service

>> LoadBalancer Type: Application Load Balancer

Click on Next

Step 3: Cluster

>> Cluster Name: Docker-Website-Cluster

Click on Next

Review and Click on Create

5. Create RDS Database

Click on Create Database

Choose a database creation method: Standard create

>> Engine Option: MySQL

>>Template: Free-tier

Setting

>> DB Instance Identifier: phpfargate

>> Master User Name: admin

>> Master Password: ******

>> Confirm Password: ******

DB Instance Type

>> Burstable Classes: db.t2.micro

Connectivity

>> VPC: Choose vpc which we have created in Cluster

And leave rest by default and click on Create Database

>> Copy EndPoint of RDS in $host parameter in db_config.php file

6. Create AWS CodePipeline

Step 1: Choose Pipeline Setting

>> Pipeline Name: Webapp-Pipeline

>> Service role: New service role

Click on Next

Step 2: Add Source Stage

>> Source Provider: AWS CodeCommit

>> Repository Name: php

>> Branch Name: master

Click on Next

Step 3: Add Build Stage

>> Build Provider: AWS CodeBuild

>> Project Name: php-docker

Click on Next

>>Skip Deploy stage and click on Next

>> Review and Click on Create Pipeline

Output:

Now we can access our website from cluster

>> Cluster -> Task -> Select Task and Under Network Section Copy Public IP Address

And the database will look like as follows:

Conclusion

From the above architecture which has been designed with continuous deployment and also used the resources which are optimized as we have used AWS Fargate to provide all the underlying provision resources and also handles cluster optimization which leads to cost optimization factor.