Setup Jenkins CI/CD pipeline to deploy Node.js to AWS EC2 [Part 1]
CI/CD pipeline is helps to automate the deployment of Node.js application into the server. Jenkins is CI/CD tool which creates a jobs to maintain the pipeline and automate the deployment process.
In this project we are deploying Node.js application into EC2 instance using Jenkins CI/CD pipeline. Firstly we need to create an EC2 instance in AWS.
Search for EC2 service in AWS search. And select the EC2 service in the list.
In the EC2 dashboard, click on the Launch Instance button to create an EC2 Instance.
First step is to create Amazon Machine Image. There are several different options choosing an image. For this project we are choosing Linux as our OS for the EC2 instance.
Next choosing an Instance Type. We have selected the t2.micro (Free tier eligible). Which is enough for our application.
Leave the configure instance, add storage and add tags as default. Move to the Change security group step. In this section add two more rules. First add HTTP type with Source as Anywhere. Second add All traffic rule which gives us access to all the ports in the EC2 instance. You can change these rules based on the permission you require for this Instance. Currently we have given permission to anyone with this EC2 details.
Next step is to review the Instance and click on Launch. It will open a dialog box to Select/Create a new key pair. Enter name for the key pair and click on download key pair. Store the key pair in a secured location in your local computer. This key pair will later be used to connect to the EC2 instance using ssh. Once you download the key pair then click on Launch Instance.
Now if you go to the instances page in the EC2 dashboard. You can see the newly created EC2 instance.
Next click on the newly created instance. It will give you all the detail on the of AWS instance. Next click on the Connect button.
It will give different way to connect to this EC2 instance. In our use case, we are using SSH to connect the instance with MACOS terminal. Screen will show the instructions on how to connect to the Instance. Make sure locate your previously located key pair in your current directory to use the command. Before using the SSH command. Give Read permissions to the key pair .pem file. Use the below command to give read permissions to the key pair.
chmod 400 <filename>.pem
Now we have successfully created and configured the EC2 instance. Now the next step is to connect to the instance and install Jenkins.
Run the following command to ensure all the current packages are up-to date.
sudo yum update
Next add the Jenkins repo to the Yum repos list. To install the jenkins.
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
Import the key file from Jenkins to enable installation from the package.
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.keysudo yum upgrade
Install Java using the following command.
sudo amazon-linux-extras install java-openjdk11 -y
You can check if the Java is installed or not using the below command.
java -version
Next add the epel package from the amazon-linux-extras. This will install the daemonize.x86_64 package.
sudo amazon-linux-extras install epel
Install the Git in the EC2 instance
sudo yum install git
Next install node in the EC2 instance. To install node first we need to install node version manager (nvm).
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
Next activate the nvm
. ~/.nvm/nvm.sh
Next use nvm to install the node
nvm install node
Next to check if the node version is installed or not. Check the version.
node -v
For deployment of the code we will be using docker. So to install use the below command.
sudo yum install docker -y
Start the docker service
sudo service docker start
Give permission to docker to access the docker from the jenkins
sudo chmod 666 /var/run/docker.sock
Install the Jenkins
sudo yum install jenkins -y
Enable the Jenkins service to start at boot
sudo systemctl enable jenkins
Start Jenkins as a service
sudo systemctl start jenkins
Check the status of the Jenkins service
sudo systemctl status jenkins
Next we need to Configure the Jenkins. You can access the Jenkins using the public DNS url at port 8080.
<Public IPv4 DNS address>:8080
You can locate the Administrator password using the below command in your instance. Paste the password key into the above screen and continue creating a user.
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Next click on the Install Suggested Plugins to configure the Standard Jenkins installation.
After the plugins installation. It prompt to create a first admin user. Enter details to create User.
After completing the setup to create the User. You will redirected to the Jenkins dashboard.
In the next part, we will work on create a jobs to implement the CI/CD pipeline for the Node.js project.