Setup CI/CD for your Nodejs Application with Bitbucket Pipeline to AWS EC2 with CodeDeploy
By: Kadek Pradnyana
Tags: Continuous Deployment, Nodejs, Continuous Delivery, Bitbucket, Continuous Integration

Make sure the role you added are as below:

AmazonEC2RoleforAWSCodeDeploy

AmazonS3FullAccess

AWSCodeDeployFullAccess

AWSCodeDeployRole

AWSCodeDeployDeployerAccess

When finish, next you must add the IAM Role to your EC2 instance. Don’t forget to reboot it to get the side effects.

Then create an access key. Enter the IAM service, on the menu navigation, select Users then select your user name. Then enter the Security credentials tab and select Create access key. After successfully creating the access key, don’t forget to save the access key in a secret place.

The next step is to create IAM Groups. Still on the IAM service, select the Groups menu on the left navigation pane. Click Create New Group and give it a name you want. Then click Next Step and add the following policy:

AmazonS3FullAccess

AWSCodeDeployFullAccess

Then click Next Step, you will be on the review group page, click Create Group button. When finished, the results will be as shown below:

Groups page with permissions tab active

Next we will add existing users to the group we created earlier. You can do this by moving to the Users tab (still on the group detail page) and adding users by clicking the Add Users to Group button.

Application Name, Deployment Config & Deployment Group (Code Deploy service)

CodeDeploy is a service that will communicate with Pipeline later. There are 2 modules that must be prepared, namely Application and Deployment Group. First, we go to the CodeDeploy service. Click Create Application then enter the desired application name and on the Compute Platform menu select EC2 / On-premises.

Create Application on CodeDeploy service

After the application created, scroll down and click the Create Deployment Group menu. Follow the steps in the following image:

Give a name on Deployment Group and Service Role

For service roles, please select the IAM Role that we created at the first time.

Environment Configuration, choose EC2 Instances

In the Environment configuration section, check Amazon EC2 instances and select EC2 based on the current tags on your instance.

Deployment SEtting section, choose CodeDeployDefault.OneAtATime

In the Deployment settings section, select the CodeDeployDefault.OneAtATime option.

Load balancer section, uncheck Enable load balancing

In the Load balancer session, uncheck the Enable load balancing option.

S3 Bucket

S3 will be used by CodeDeploy to save Revisions. And this is the final stage of configuration on AWS. Go to the S3 service. Then Create Bucket and give it a name according to the project you created.

Application Name, Deployment Group Name, and S3 Bucket Name will be added on Btbucket Pipeline environment.

CodeDeploy Agent

The final step is to install CodeDeploy agent on the EC2 instance. As I discussed earlier, the CodeDeploy agent is tasked with running commands in the pipeline then CodeDeploy agent will give a response to the pipeline whether the command we run is in the status of success or failure.

Please login to EC2 with SSH then install CodeDeploy agent with the command below.

Command install CodeDeploy agent on ubuntu