How to deploy your NodeJs app on Amazon Elastic Beanstalk (AWS EB) with CircleCI — Short Tutorial

Vygandas Pliasas
7 min readAug 7, 2018

--

I want to share how to implement Continuous Integration process for NodeJs application that uses Express (or whatever) to Amazon AWS.

There’re some options to choose — whether it is based in EC2 or Elastic Beanstalk or Lambda or probably other. Lets see what Amazon itself says about these three products.

Amazon Elastic Beanstalk

AWS Elastic Beanstalk is an easy-to-use service for deploying and scaling web applications and services developed with Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker on familiar servers such as Apache, Nginx, Passenger, and IIS.

You can simply upload your code and Elastic Beanstalk automatically handles the deployment, from capacity provisioning, load balancing, auto-scaling to application health monitoring. At the same time, you retain full control over the AWS resources powering your application and can access the underlying resources at any time.

There is no additional charge for Elastic Beanstalk — you pay only for the AWS resources needed to store and run your applications.

Amazon EC2

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers.

Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction. It provides you with complete control of your computing resources and lets you run on Amazon’s proven computing environment. Amazon EC2 reduces the time required to obtain and boot new server instances to minutes, allowing you to quickly scale capacity, both up and down, as your computing requirements change. Amazon EC2 changes the economics of computing by allowing you to pay only for capacity that you actually use. Amazon EC2 provides developers the tools to build failure resilient applications and isolate them from common failure scenarios.

Amazon Lambda

AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume — there is no charge when your code is not running.

With Lambda, you can run code for virtually any type of application or backend service — all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.

These product descriptions sounds quite close to each other. I found that using Elastic Beanstalk was very easy to start. They say that even on their front door 🙂

To start using any service you need to have an account. ⁉️

Just go and create it. It’s pretty straightforward. I’ll need to enter your credit card information in the billing page so you’d be able to use various services.

Amazon EB

To be able to deploy our application we need to create application environment on EB. It has tons of configurations and seems to be a good choice for most of apps. For instance you can enable load balancer, notifications service, databases and so on.

Once you’re logged in you’ll see something like this:

You can use search field if you don’t see a product

After you click on Elastic Beanstalk you’ll be redirected to its management console. There you’ll be able to create various environments for your apps. This time I’m talking about NodeJs but I think this wont be a big difference from other languages.

Hit Create New Application.

When you’ll be creating a new environment it will ask you some questions. Just read labels and hints and fill everything you need.

TIP: By default it chose Node 7.11 version so my app with ES6 failed to build. Edit after creation or right away Node version to be the latest.

Your EB environment should look kinda like this

Dumb Node app

We are not discussing how to make actual node app here so lets just assume that our app has one app.js file which does something, well most likely has some web requests handling capabilities and doesn’t quit instantly after starting it.

How to deploy manually

It’s simple. Compress your all files EXCEPT node_modules and .env (if there’s such) to ZIP and upload it. Don’t include your node modules because your app will fail. EB installs things automatically according to your package.json.

This is where you can upload you zipped app

It’s nice feature for testing purposes because you don’t need to setup other things in order to see whether this is working or not. Lets continue on setting up our CI.

Identity and Access Management — IAM

AWS Identity and Access Management (IAM) enables you to manage access to AWS services and resources securely. Using IAM, you can create and manage AWS users and groups, and use permissions to allow and deny their access to AWS resources.

IAM is a feature of your AWS account offered at no additional charge. You will be charged only for use of other AWS services by your users.

To get started using IAM, or if you have already registered with AWS, go to the AWS Management Console and get started with these IAM Best Practices.

In short — this is where you’ll create user, key and secret token for your CI to be able to deploy.

After signing in you’ll see similar sidebar. You should choose Users. When creating a user you’ll be able to assign a group. Group holds various permissions to various Amazon products.

Users list

Click on add button and fill all required fields. Check that this is Programmatic access.

In the groups modal you must add permission that is required for accessing EB. It’s called AWSElasticBeanstalkFullAccess. Without this you won’t be able to deploy. Same applies for S3 too.

Programmatic access

After that

Create a group with AWSElasticBeanstalkFullAccess policy selected

And in the result you’ll get your KEY and SECRET.

Key and secret

That’s it for this step.

Project on Github or Bitbucket

You can choose what GIT management service you like most. I chose Bitbucket. Selection was simple — GitHub is cool but it doesn’t have free private repositories and Bitbucket does.

You need to push all your code to one of these and that’s it for this step too.

.elasticbeanstalk

This is EB CLI config for deployments. Add this .elasticbeanstalk FOLDER in your project root. Create config.yml inside it.

This image should explain where are all these parameters located:

CI setup

You can choose what CI you like best. I’ve chosen CircleCI. Reason — CircleCI supports Bitbucket. That’s it. You can use Travis for example but I’m talking about CircleCI further so you’ll need to hunt down configurations yourself 😉

Create account and connect your Bitbucket to CircleCI. This is quite straightforward so I don’t explain much here.

Best part is that build is initiated when you push to git. You don’t need to do anything manually!

Just try to select your repository and initiate build. Something is wrong???

CircleCI requires configuration file in your project so it would know what to do. File should be located in folder called .circleci (that is placed in project root) and that file must be called config.yml. So it’s .circleci/config.yml.

It should look something like this:

Read carefully all lines and you’ll get what it does. I’ve adde somme supplementary comments there too.

In order eb deploy to be able to actually deploy something we need to provide AWS user KEY and SECRET that we’ve created earlier.

Add AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY with their corresponding values in Environment Variables section. Just click gear icon near your repository in CircleCI.

Edit your repository settings in CircleCI

This should work. If you’re missing something think this — does all services have required parameters and values set?

Node ENV variables

If you were using .env in your project, you can add these values in your AWS EB environment, Software configuration section.

.ebignore

This is something like .gitignore but intended only for EB. Add node_modules in it.

Final thoughts

That’s not that hard after you do it for the first time. CI is cool thing and can do a lot of useful stuff for you.

I hope you’ve learned something new. If you have questions — leave a comment. Cheers!

--

--