Deploy App on AWS ECS Fargate using Github Actions
Tags: ecs, githubactions, devops, fargate
Published: 2024-04-23 07:02:02

In this blog, i will show how to deploy an application on Amazon ECS using the Fargate for efficient containerized deployment and also Github actions will be used for the CI/CD.

Step 1: Create your repository on ECR( Amazon Elastic Container Registry)

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

Step 2: Create cluster in ECS

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

Step 3: Create a Task Definition

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

You can create a task role if you don't have existing role created. These are the two policies required for the role

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhb8gjy9khmoiiiumw4f.png)
 
For the container, give a name, and copy the image URL created in the ECR previously.
Add the port in which your application is accessed.

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

Step 4: Create a Service
**Note:** After creating this service it will fail because there is no image pushed to the ECR yet.
Click on the cluster created, and create service.

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

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

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

Step 5: While service is creating, configure your application github workflow. The application is packaged using Docker.

Hence the dockerfile

```
FROM node:16.20.1
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 5000
CMD ["npm","run","start"]
```
github workflow is as below

```
name: CICD

on:
  push:
    branches: [ main ]

jobs:
  build-and-deploy:
    runs-on: [ ubuntu-latest ]
    steps:
      - name: Checkout source
        uses: actions/checkout@v3
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v2
        with:
          mask-password: 'true'
          
      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          IMAGE_TAG: latest
          REPOSITORY: nodeapp
        run: |
          # Build a docker container and
          # push it to ECR so that it can
          # be deployed to ECS.
          docker build -t $ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG .
          docker push $ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG
          echo "image=$ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT    

      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: nodejs-app-task-definition.json 
          container-name: nodejs-app
          image: ${{ steps.build-image.outputs.image }}    
      - name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.task-def.outputs.task-definition }}
          service: nodejs-app-service
          cluster: DevCluster
          wait-for-service-stability: true
``` 
**Note:** 
- Create the task definition file(nodejs-app-task-definition.json) in the directory of your project. Copy the json file and paste in the file created.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6k16j7kt8xyqzl385693.png)

- The environment variables are set in github settings
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bij4hycxlzm686alof9.png)

After the image is built and pushed to ECR, the service becomes active and running.

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

Click on the task to see the configuration to see the public ip address. 

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