Coder Social home page Coder Social logo

imraantufail / tf-ecr-ecs-gh-deploy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from notharshhaa/tf-ecr-ecs-gh-deploy

0.0 0.0 0.0 59 KB

ᴄɪ/ᴄᴅ ᴡɪᴛʜ ɢɪᴛʜᴜʙ ᴀᴄᴛɪᴏɴꜱ: ᴅᴇᴘʟᴏʏɪɴɢ ᴀ ɴᴏᴅᴇ.ᴊꜱ ᴀᴘᴘ ᴛᴏ ᴀᴍᴀᴢᴏɴ ᴇʟᴀꜱᴛɪᴄ ᴄᴏɴᴛᴀɪɴᴇʀ ꜱᴇʀᴠɪᴄᴇ (ᴇᴄꜱ) ᴜꜱɪɴɢ ᴛᴇʀʀᴀꜰᴏʀᴍ

Python 23.11% HCL 70.71% Dockerfile 6.18%

tf-ecr-ecs-gh-deploy's Introduction

𝘾𝙄/𝘾𝘿 𝙬𝙞𝙩𝙝 𝙂𝙞𝙩𝙃𝙪𝙗 𝘼𝙘𝙩𝙞𝙤𝙣𝙨: 𝘿𝙚𝙥𝙡𝙤𝙮𝙞𝙣𝙜 𝙖 𝙣𝙤𝙙𝙚.𝙟𝙨 𝙖𝙥𝙥 𝙩𝙤 𝘼𝙢𝙖𝙯𝙤𝙣 𝙀𝙡𝙖𝙨𝙩𝙞𝙘 𝘾𝙤𝙣𝙩𝙖𝙞𝙣𝙚𝙧 𝙎𝙚𝙧𝙫𝙞𝙘𝙚 (𝙀𝘾𝙎) 𝙪𝙨𝙞𝙣𝙜 𝙏𝙚𝙧𝙧𝙖𝙛𝙤𝙧𝙢

In this blog post, I’ll demonstrate how to build, test & push docker images to AWS ECR (Elastic Container Registry) and run these images on AWS ECS (Elastic Container Service) using GitHub Actions.

The Goal

  • Create a simple node site
  • Create an docker image optimized for production and host it on ECR
  • Use ECS to put this image online
  • Use Terraform to create the AWS infrastructure
  • The source files are hosted on github
  • Use Github actions to automatically update the site online after a commit
  • A new docker image will be automatically generated and hosted on ECR
  • This new image will be automatically deployed on ECS

Prerequisites

Before creating GitHub Actions workflow yaml file, we need to complete the following steps:-

  1. Setup AWS CLI with appropriate credentials.
  2. Create an AWS ECR repository to store our docker images.
  3. Create an AWS ECS task definition, cluster, and service.

Getting Started

First of all, we need to build the application in our local environment. Here is my demo application link.

Now , we will go back to ECS in our AWS console. We need to copy the json file from our “Task Definition” and Paste it into our local environment under .aws directory and and then push it to our github repository.

GitHub Secretes Configuration

We need to store the AWS_ACCESS_KEY_ID & AWS_SECRET_ACCESS_KEY into the secrets of our repository on GitHub. Navigate to the “Settings” section in our GitHub repository and locate the “Secrets” section on the left-hand side. By clicking the “new repository secret” we are going to add these two secrets.

Configuring the GitHub Actions workflow

Now, it’s time to configure the GitHub Actions. GitHub Actions uses YAML syntax to define the workflow. This workflow need to be stored as a separate YAML file in our code repository, in a directory named “. github/workflows”

Before getting started with the github actions configuration I’ll try to explain a little bit about workflow and job. A workflow is a configurable automated process made up of one or more jobs that will be executed sequentially. A job consists of several steps of instructions that perform in a remote server to be executed. In this project, the workflow actions are supposed to test and build the image of our application using Dockerfile and push that image into the AWS ECR to run it on an ECS environment.

Let’s create a ci/cd pipeline workflow to test,build and push our node.js application to deploy in our AWS ECS environment. We will create a configuration file named “main.yaml” (you can choose a different name according to your preference) under “.github/workflows” in our local environment.

First of all we set a workflow name and need to trigger this workflow, whenever there is a pull or push on the main branch. Now, we need to specify the environment variable for our ECS service that we have created.

Note: Ensure that you provide your own values for all the variables in the env key of the workflow

Here in the jobs we have created a job called “test”. We also set a build matrix with “ubuntu-latest” remote environment/Runner that will test jobs under different node versions. Under “steps” we set tasks that we need to run under the “test” job. We have created another job called “deploy”, where the “deploy” job only runs when the “test” job gets passed. Finally, The following steps are performed in the workflow file, if test job gets passed and deploy job started for running:

  1. Checks-out our repository under GitHub Workspace, so our workflow can access it.
  2. Configure the AWS credentials.
  3. Login to AWS ECR.
  4. Build, Tag, and push the image to AWS ECR.
  5. Update the AWS ECS service with a new task definition.

Deployment & Result

Once we completed our workflow, we will push it to our github repository. We can check out the “actions” tab and see that a new action has started which is indicated by the yellow color indicator. Moreover, we can see the build logs where each tab shows the current task and on expanding each tab you can see its logs.

Now we have to wait until the whole process is completely finished. If we get some errors, we follow through the details for every step, so that we can solve the error.

Having automated deployments to ECS is quite easy with Github actions. Yet, it’s quite powerful as it allows us to deploy automatically every time there is a change in our code.

Yuppp, we’re done here! Feel free to reach out to me if you have any queries or doubts.

Thanks for your time!

tf-ecr-ecs-gh-deploy's People

Contributors

notharshhaa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.