DEV Community

Cover image for Deploy App on AWS ECS Fargate using Github Actions

Deploy App on AWS ECS Fargate using Github Actions

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

Step 2: Create cluster in ECS

Image description

Step 3: Create a Task Definition

Image description

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

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

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

Image description

Image description

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"]


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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

  • The environment variables are set in github settings
    Image description

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

Image description

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

Image description

Top comments (1)

Collapse
 
sammy_cloud profile image
Samuel Ajisafe

This is great