A CodePipeline example to deploy a NodeJS app on AWS

What is CodePipeline?

What will we build

App Look

CodeCommit

CodeBuild

  • Spins up the build server
  • Install package: zip on the server
  • Zips up the code files in the ‘webscripts’ folder
  • Uploads the files as artifact to a specified S3 bucket

CodeDeploy

  1. files: Defines what file is deployed from the input artifact and to what folder on the destination server.
  2. BeforeInstall: In this step we install any dependencies which are needed by our app to run on the server.Here we define the name of the script which will be executed on the server to install the dependencies.In this example we are installing Nodejs and npm on the server to run our nodeJS app.
  3. AfterInstall: Here we can run any other scripts or commands on the server which are pre-requisites for the final app to run.In our example we are executing npm install command to install the NodeJS packages defined in the package.json of the application.
  4. ApplicationStart: This is the final step where we run the script to start up the application.The script and commands will differ based on what application you are starting.Here we run the npm command to start up the express server in the NodeJS app.This depicts the final step of the deployment once application starts successfully.

Walk through of the Setup

Initial Preparations:

  • AWS Account: Of course we will need an AWS account. Go ahead and register for a free tier account on AWS
  • IAM User: Create an IAM user which will be used throughout this process. Provide required admin permissions to this user.Also to use the user for CodeCommit, generate the GIT credentials and note credentials.
GIT Key
  • Custom Policy and Instance profile: The instances where the deployment will happen, need to have a specific role/instance profile associated to them to provide certain permissions.In this step you have to create a custom policy and a custom role attaching the custom policy to it.Below is the policy to be associated to the instance role:
{ "Version": "2012-10-17", "Statement": [ { "Action": [ "s3:Get*", "s3:List*" ], "Effect": "Allow", "Resource": "*" } ] }
  • A service role for CodeDeploy: Create a service role which will be used by CodeDeploy to perform various deployment functions.While creating the role, select the CodeDeploy use case so that needed permissions are added to the role.
CodeDeploy Role
  • S3 Bucket: Create the S3 bucket which will be used to store the output artifacts from the CodeBuild phase.
Artifact Bucket

Pipeline setup

  • Setup EC2/Auto Scaling Group: Here we are setting up the Auto scaling group where the app will be deployed. Please follow the steps in this document to create an ASG and launch the instances.(https://docs.aws.amazon.com/autoscaling/ec2/userguide/create-asg.html). My example is specific to Ubuntu OS so make sure to select Ubuntu AMI and make sure to attach the below script as User data while creating the launch configuration.This will install the CodeDeploy agent which is needed by CodeDeploy to deploy and start the app.
#!/bin/bash
apt-get -y update
apt-get -y install ruby
apt-get -y install wget
cd /home/ubuntu
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
chmod +x ./install
./install auto
  • CodeCommit Repo: Here we create the GIT repo to check in the code and will be the trigger for the pipeline.Create the CodeCommit repository in AWS. Copy the repository URL.To push the code to this repository, checkout or download my Github repository to your local machine.Run the following command to add a remote URL to the GIT repository and point it to the CodeCommit repo created above.
git remote add neworigin <repo_url>
git add . 
git commit -m "Initial"
git push neworigin master
  • CodeBuild Project: Next we will create the CodeBuild project to perform the build tasks.Navigate to CodeBuild in AWS and create a new project.Make sure to provide the following. Rest can be kept at default values:
Source
  • CodeDeploy Application and Deployment: Now we will ready the deployment step and configure all settings to deploy on the Auto scaling group we created.Follow the below step by step to setup the deployment:
CodeDeploy Settings
  • Create the AWS CodePipeline pipeline: In this final step we build the pipeline using the earlier phases we created.Below are some needed details for each of the screens for this:
Source
Build Stage
Deploy Stage

Pipeline Execution

Login Page

Troubleshooting

Failure

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amlan Chakladar

Amlan Chakladar

Cloud architect and DevOps engineer. Love to code and develop new stuff. A nerd by nature.