Deploying your App to AWS Fargate and Configuring CI/CD in one CloudFormation Template

Mahmoud Almasri
Knawat Ninja
5 min readNov 30, 2021

--

If you have an application and you want to deploy your application to AWS Fargate, this template is that all you want.

let’s first define our services:

CloudFormation: AWS CloudFormation lets you model, provision, and manage AWS and third-party resources by treating infrastructure as code.

AWS ECS: Amazon Elastic Container Service is a fully managed container orchestration service that makes it easy for you to deploy, manage, and scale containerized applications.

AWS Fargate: is a serverless, pay-as-you-go compute engine that lets you focus on building applications without managing servers.

Introduction:

First, if you need to run your application on AWS Fargate you have to make sure your application Dockerfile to containerize your application because AWS Fargate is a technology to run your application your container on AWS ECS.

The advantage of using AWS Fargate is you don’t have to configure, provision, scale a cluster of VMs to run your containers, the cluster resources are defined by the AWS Fargate while creating the Task.

AWS ECS Task is where you can define your container configuration such as:

  • The docker image to use in your container.
  • The resources of the container in the task.
  • The network configuration of the container.
  • The restart policy of the container.
  • If you need to run a command when the container start.
  • Volumes of the container.
  • IAM.

And you can define multiple containers in one task definition.

Amazon ECS Service allows you to run and maintain a specified number of instances of a task definition simultaneously in an Amazon ECS cluster. If any of your tasks should fail or stop for any reason, the Amazon ECS service scheduler launches another instance of your task definition to replace it in order to maintain the desired number of tasks in the service.

In addition to maintaining the desired number of tasks in your service, you can optionally run your service behind a load balancer. The load balancer distributes traffic across the tasks that are associated with the service.

So, to deploy your app to AWS Fargate you have to configure:
1. AWS ECS Cluster

2. AWS ECS Task.

3. AWS ECS Service.

4. AWS Loadbalancer.

5. VPC.

6. SSL Certificate for your APP to run HTTPS.

And to if you need to enable the CI/CD you need also to configure:
1. AWS ECR.

2. AWS Pipeline.

3. Build.

4. Deploy.

5. The connection with your Github repo.

But with the Template that I will explain, you just need to use it with your own parameters and configuration, it is simple.

To let this article more practical we will use a demo application, it’s a simple Nodejs application and simple to install, it’s a Strapi CMS application.

Strapi CMS is a headless CMS, open-source, and it’s 100% javascript.

The Test Application:

You can see the full code of the app in this repository.

it’s a new Strapi project, I just install it and run it locally with one command:

Then I created a Dockerfile to containerize the application:

if you want to start the application locally, just type:

then if you go to http://localhost:1337/admin

Strapi start page

How to use the CloudFormation Template:

You can download the full code of the template from this repository.

First: Connect your GitHub account to AWS

  • Go to AWS Developer Tools > Settings > Connections > Create connection
AWS Developer Tools Page

Second: Upload the CloudFormation Template to AWS S3 Bucket

Create a bucket to store the template file, then download the template from the GitHub repo and upload it to your S3 Bucket.

AWS S3 Bucket Template file

and get your S3 URL of the file (aws-autostart-pipeline.yaml)

AWS S3 Copy URL

You will get the URL like that

Third: Create the CloudFormation Stack

Go to CloudFormation > Create Stack > With new Resources

AWS CloudFormation Create Stack

Next,

Stack Configuratio1

Stack name: any name for your stack

GitHub Repo: like Mhm0ud/strapi-example

Branch: the branch that you want to deploy

Stack Configuration 2

Connection ARN: the connection of your Github account that you configured in step number 1.

Domain Configuration: you should host your DNS in AWS Route53.

Domain Name: like www.domain.xyz, api.domain.xyz

Environment variables file: if you need to store your environment variable file in S3, you can do it and add the configuration here.

Container Port: what is the port that you are going to start your app with, we defined in the Dockerfile previously as 1337.

Stack Configuration 3

TemplateBucket: the name of the bucket where you store the template file.

Next > Next

Stack Configuration 4

check the acknowledges and finish.

Now, the stack will create all the resources that we described before.

Stack Creating Resources in Progress

After it finishes, you will see that all resources (ECS Cluster, Task, Service, and Pipeline) and now you can visit your domain name.

Also, the pipeline that was created will deploy a new version of your application with every push to the branch you specified before.

--

--