6 min read

As an application developer, you must be familiar with the complexity of deploying apps to a fleet of servers with minimum down time. AWS introduced a new service called AWS Code Deploy to ease out the deployment of applications to an EC2 instance on the AWS cloud.

Before explaining the full process, I will assume that you are using AWS VPC and are having all of your EC2 instances inside VPC, and that each instance is having an IAM role.

Let’s see how we can deploy a Node.js application to AWS.

Install AWS Code Deploy Agent

The first thing you need to do is to install aws-codedeploy-agent on each machine that you want your code deployed on.

Before installing a client, please make sure that you have trust relationship for codedeploy.us-west-2.amazonaws.com and codedeploy.us-east-1.amazonaws.com added in IAM role that EC2 instance is using. Not sure what it is? Then, click on the top left dropdown with your account name in AWS console, select Security Credentials option you will be redirected to a new page, select Roles from left menu and look for IAM role that EC2 instance is using, click it and scroll to them bottom, and you will see Edit Trust Relationship button. Click this button to edit trust relationship, and make sure it looks like the following.

"Principal": {
   "Service": [

Ok we are good to install the AWS Code Deploy Agent, so make sure ruby2.0 is installed. Use the following script to install code deploy agent.

aws s3 cp s3://aws-codedeploy-us-east-1/latest/install ./install-aws-codedeploy-agent --region us-east-1
chmod +x ./install-aws-codedeploy-agent
sudo ./install-aws-codedeploy-agent auto
rm install-aws-codedeploy-agent

Ok, hopefully nothing will go wrong and agent will be installed up and running. To check if its running or not, try the following command:

sudo service codedeploy-agent status

Let’s move to the next step.

Create Code Deploy Application

Login to your AWS account. Under Deployment & Management click on Code Deploy link, on next screen click on the Get Started Now button and complete the following things:

  • Choose Custom Deployment and click the Skip Walkthrough button.
  • Create New Application; the following are steps to create an application.

    –            Application Name: display name for application you want to deploy.

    –            Deployment Group Name: this is something similar to environments like LIVE, STAGING and QA.

    –            Add Instances: you can choose Amazon EC2 instances by name group name etc. In case you are using autoscaling feature, you can add that auto scaling group too.

    –            Deployment Config: its a way to specify how we want to deploy application, whether we want to deploy one server at-a-time or half of servers at-a-time or deploy all at-once.

    –            Service Role: Choose the IAM role that has access to S3 bucket that we will use to hold code revisions.

    –            Hit the Create Application button.

Ok, we just created a Code Deploy application.

Let’s hold it here and move to our NodeJs app to get it ready for deployment.

Code Revision

Ok, you have written your app and you are ready to deploy it. The most important thing your app need is appspec.yml. This file will be used by code deploy agent to perform various steps during the deployment life cycle. In simple words the deployment process includes the following steps:

  • Stop the previous application if already deployed; if its first time then this step will not exist.
  • Update the latest code, such as copy files to the application directory.
  • Install new packages or run DB migrations.
  • Start the application.
  • Check if the application is working.
  • Rollback if something went wrong.

All above steps seem easy, but they are time consuming and painful to perform each time. Let’s see how we can perform these steps easily with AWS code deploy.

Lets say we have a following appspec.yml file in our code and also we have bin folder in an app that contain executable sh scripts to perform certain things that I will explain next. First of all take an example of appspec.yml:

version: 0.0
os: linux
- source: /
 destination: /home/ec2-user/my-app

- object: /
   pattern: "**"
   owner: ec2-user
   group: ec2-user

   - location: bin/app-stop
     timeout: 10
     runas: ec2-user

   - location: bin/install-pkgs
     timeout: 1200
     runas: ec2-user

   - location: bin/app-start
     timeout: 60
     runas: ec2-user

   - location: bin/app-validate
     timeout: 10
     runas: ec2-user

It’s a way to tell Code Deploy to copy and provide a destination of those files.

- source: /
   destination: /home/ec2-user/my-app

We can specify the permissions to be set for the source file on copy.

- object: /
   pattern: "**"
   owner: ec2-user
   group: ec2-user

Hooks are executed in an order during the Code Deploy life cycle. We have ApplicationStop, DownloadBundle, BeforeInstall, Install, AfterInstall, ApplicationStart and ValidateService hooks that all have the same syntax.

     - location: script-location
       timeout: timeout-in-seconds
       runas: user-name
  • location is the relative path from code root to script file that you want to execute.
  • timeout is the maximum time a script can run.
  • runas is an os user to run the script, and some time you may want to run a script with diff user privileges.

Lets bundle your app, exclude the unwanted files such as node_modules folder, and zip it.

I use AWS CLI to deploy my code revisions, but you can install awscli using PPI (Python Package Index).

sudo pip install awscli

I am using awscli profile that has access to s3 code revision bucket in my account. Here is code sample that can help:

   --profile simsaw-baas 
   deploy push 
   --application-name MY_CODE_DEPLOY_APP_NAME 
   --source MY_APP_BUILD.zip

Now Code Revision is published to s3 and also the same revision is registered with the Code Deploy application with the name MY_CODE_DEPLOY_APP_NAME (it will be name of the application you created earlier in the second step.)

Now go back to AWS console, Code Deploy.

Deploy Code Revision

Select your Code Deploy application from the application list show on the Code Deploy Dashboard. It will take you to the next window where you can see the published revision(s), expand the revision and click on Deploy This Revision. You will be redirected to a new window with options like application and deployment group. Choose them carefully and hit deploy. Wait for magic to happen.

Code Deploy has a another option to deploy your app from github. The process for it will be almost the same, except you need not push code revisions to S3.

About the author

Ankit Patial has a Masters in Computer Applications, and nine years of experience with custom APIs, web and desktop applications using .NET technologies, ROR and NodeJs. As a CTO with SimSaw Inc and Pink Hand Technologies, his job is to learn and help his team to implement the best practices of using Cloud Computing and JavaScript technologies.


Please enter your comment!
Please enter your name here