Deploy using Github Actions on AWS Elastic BeanStalk

Anjali Verma
JavaScript in Plain English
5 min readJul 4, 2020

--

In this article we’d be going through the process of deploying Node application on AWS Elastic BeanStalk using Github Actions.

All this while I’ve been using TravisCI for my deployments, but when I got introduced to Github Actions, I feel there is no turning back. Github Actions make deployment of your web application super simple ❤.

What is Elastic BeanStalk?

AWS Elastic Beanstalk is an orchestration service that abstracts away some of hardware resources and details (e.g. setting up AWS components and containers), while still allowing the developer a range of choices when it comes to OS and programming language. Elastic Beanstalk employs Auto Scaling and Elastic Load Balancing to scale and balance workloads. It provides tools in the form of Amazon CloudWatch to monitor the health of deployed applications. It also provides capacity provisioning due to its reliance on AWS S3 and EC2.

All you have to do is upload your application code, and Elastic Beanstalk takes care of the deployment, load balancing, and capacity provisioning. With a single click, you can start all the necessary application servers running. There is no charge for the Elastic Beanstalk service itself, just for the AWS resources you actually use.

What are Github Actions?

GitHub Actions give you a possibility to automate your software development workflows directly from the GitHub repository. In other words, you will be able to initiate the deployment of your application from the same place where your code is stored.

Another great feature of Github Actions is that, workflows can run in Linux, macOS, Windows, and containers on GitHub-hosted machines. Elastic Beanstalk web apps are fully supported by Github Actions, so you can deploy your app directly from the repository.

The pipeline that we’re trying to achieve in this article is:

CI/CD Pipeline

With Actions in place, you can initiate functions such as seamlessly building and deploying Docker containers. When you combine Actions into workflows, events can trigger a complex series of processes to run in parallel sequence, enabling more responsive architectures.

Automate your workflow from idea to production.

Getting Started

Elastic Beanstalk will automatically create an EC2 instance for us when we create our app, this is where our Node.js server will be hosted.

Steps to setup Elastic BeanStalk environment:

  1. For setting up Elastic BeanStalk environment, I’d recommend you to head over the step by step guide to configure the EB environment till step #9, you can skip Load balancer step or watch this video by AWS.
  2. To improve performance, you can configure the proxy server to serve static files (for example, HTML or images) from a set of directories inside your web application.To statistically deploy your Node application on EB, you need to add .ebextensions/deploy.config file:

Follow this for more information to serve your files statically.

Once you setup your Beanstalk environment, you could also monitor the health of your environment after every build in the EB console:

Steps to build your project:

  1. Install the dependency to serve your Node application statically.
npm i serve --save

2. In the production folder of your Node application, make sure to change startscript in package.jsonfile.

"scripts": {
"start": "serve -s build",
}

Replace server.js by your main server file if you named it something else. This is so Elastic Beanstalk knows which file to use to launch your server.

Steps to create your Github Actions workflow:

  1. Go to your GitHub repository and click the Actions tab button.

2. Click on the Set up a workflow yourself button, on the right corner, as shown in the screenshot below.

3. In the editor paste the code snippet below and click on the Start commit button on the right corner. Add some commit message and click on Commit new file button. I’ve added comments to explain each step of this workflow.

Here, push and pull request eventsare triggering this workflow, but we can have it triggered on various other events too.

4. Open your project in the IDE on your machine and pull the updated code from the master branch.

5. Now, go to the Settings tab of your repository and on the side menu, select Secrets. Secrets are your private values like AWS secret key and access key which should not be directly present in your code.

6. Click on Add a new secret button and add your AWS secret keys and access keys with recommended name as AWS_ACCESS_KEY and AWS_SECRET_KEY.

If you configured everything successfully, your workflow should look like:

Output of the pipeline

And that’s it, it’s this simple to create your own CI/CD pipeline with Github Actions for your Node projects. All you need to create is one workflow file to deploy your Node projects on cloud.

You can find the workflow and EB config files here:

--

--