© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
P. P. DingareCI/CD Pipeline Using Jenkins Unleashedhttps://doi.org/10.1007/978-1-4842-7508-5_18

18. Integrating Jenkins with AWS

Pranoday Dingare1  
(1)
Pune, Maharashtra, India
 

Nowadays applications are deployed on machines available on the cloud instead of from in-premise machines. An understanding of cloud platforms like AWS (Amazon Web Services) and Azure is vital to the success of the DevOps professional.

This chapter talks about using Jenkins to deploy a web application on a machine (EC2 Instance) available on an AWS cloud and then run the Selenium tests on this web application.

Understanding an EC2 Instance on AWS

An EC2 instance is a virtual server in Amazon Web Services terminology. EC2 stands for Elastic Compute Cloud. It is a web service where an AWS subscriber can request and provision a compute server in the AWS cloud. Users can rent virtual server use per hour and use it to deploy software applications.

The instance will be charged per hour with different rates based on the type of instance chosen. You can create instances based on your CPU and memory requirements and use them as long as you need them. You can terminate the instance when it is no longer used and save on costs.

Creating an EC2 Instance on AWS

Before you can involve Jenkins in deploying web applications on an EC2 instance, you need to create one EC2 instance on AWS. Use the steps in the following sections to do just that.

Step 1: Sign Up on AWS

Go to the https://aws.amazon.com/account/sign-up link and click the Create a Free Account button, which will show the Sign Up for AWS page.

Fill in the required details and click the Continue (step 1 of 5) button.

Fill in the required details again. Click the Continue (step 2 of 5) button, which will show the Billing Information page. Enter the required payment details on the Billing Information page.

Select the Text Message radio button, enter your mobile number, and click Continue (step 4 of 5). After clicking this button, you will receive an SMS text on your mobile. Enter this code on the appropriate page.

Click the Continue (step 5 of 5) button, which will open the Select a Support Plan page. Click the Complete Sign Up button. Click the Go to AWS Management Console button. Select the Role and Interest dropdowns.

Finally, click the Submit button.

Step 2: Sign in to AWS

Click the Sign in to the Console button. Enter an email address in the Root User Email Address field and click the Next button.

Enter the CAPTCHA in the field and click the Submit button.

Enter the password you selected during the signup process in the Password field.

Click the Sign in button. Click the All Services link to open all available services. Then click the EC2 link, which is the first option in the Compute section.

Step 3: Create an EC2 Instance

Scroll down the page and click the Launch Instance dropdown; then click the Launch Instance option.

To create an EC2 instance with Windows OS, type Windows and press the Enter key, which will show the available EC2 instances with Windows OS.

While creating an EC2 instance, you have options like t2micro, t2medium, t2 large, etc., which differ in the kind of diskspace, processor speed, and RAM they offer. You need to select a type based on your diskspace, RAM, and processor speed requirements. Note that a few types of EC2 instances are free, like t2micro, but others are paid. In this chapter, select the first entry, which is free.

Click the Review and Launch button. Next, click the Launch button.

You will see the Select an Existing Key Pair or Create a New Key Pair page. From this page, you can create an authentication key pair to access the EC2 instance.

From the Choose an Existing Key Pair dropdown, select the Create a New Key Pair option. Enter a name in the Key Pair Name field. I entered MyEC2Instance in this field.

Click the Download Key pair button. The MyEC2Instance.pem file will be downloaded. Click the Launch Instances button. Then scroll down the page to find the View Instances button; click it.

After clicking that button, the Instances page will open.

Step 4: Start an EC2 Instance

Click the checkbox shown in the first column for instance entry and click the Actions button to open the dropdown. Click the Connect dropdown option. Click the RDP Client tab. Click the Get Password link.

Click the Browse button and select the private key (MyEC2Instance.pem) file.

After you click the Open button from the File dialog, the private key contents will be shown in the text field. Click the Decrypt Password button. It will generate a password below the Password: label. Copy the password by clicking the button provided to the right of the generated password.

Click the Download Remote Desktop File button. Double-click the downloaded remote desktop file and click the Connect button.

Paste the password you copied inside the Password field and click the OK button.

Click the Yes button, which will open the RDP to EC2 instance you launched, shown in Figure 18-1.

A desktop page with Recycle Bin, and shortcuts of E C 2 Feedback and E C 2 Microsoft Windows Guide. On the bottom part, laid horizontal is the taskbar, with Internet Explorer and File Explorer pinned.

Figure 18-1

Desktop of an EC2 instance

Configuring the EC2 Instance to Deploy the Calculator Web Application

Step 1: Install the IIS Web Server

You need to install the IIS web server so that you can deploy the Calculator web application in it. Follow these steps to install the IIS web server on server-type Windows machines.

Use the Window key to open the Windows menu search and type Server Manager. Select the Server Manager menu entry, which will open the Server Manager Dashboard.

Click the Add Roles and Features link and then click the Next button.

Keep the Role-Based or Feature-Based Installation radio button selected and click the Next button, which will open the Select Destination server window. Click the Next button.

Select the Web Server IIS checkbox, which will open the Add Features That Are Required for Web Server (IIS) window.

Click the Add Features button, which will open the Select Server Roles window.

Click the Next button to open the Select Features window.

Click the Next button to open the Web Server Role (IIS) window.

Click the Next button again, which will open the Select Role Services window.

Click the Next button a final time to open the Confirm Installation Selections window.

Click the Install button. Wait until the installation completes. Once installation is completed, click the Close button.

Step 2: Configure the IIS Web Server

Create a blank directory named DeployedCalculatorApp on C:.

Now you’ll create a web alias (a virtual directory) in IIS which will point to this directory. Go to the Windows menu and select the Internet Information Services (IIS) Manager option, which will open the Internet Information Services (IIS) Manager window.

Open the tree shown in the IIS console and right-click Default Web Site as shown in Figure 18-2.

A desktop page with an open folder titled, Internet Information Services Manager, is maximized. It starts with a menu bar, a navigation pane on both the left and right sides and a right-click menu on the left. On the center page is the Default Web Site Home of available files and programs. And on the bottom is a taskbar with multiple programs open.

Figure 18-2

The menu opens after clicking the Default Web Site

Select the Add Virtual Directory menu option to open the Add Virtual Directory window. Fill in the alias name as CalculatorWebApp in the Alias field and enter C:DeployedCalculatorApp in the Physical Path field, as shown in Figure 18-3. Click the OK button to create a virtual directory inside the default website.

A dialog box titled Add Virtual Directory. It has 2 inputs to fill out, which are Alias and Physical path. Below are 2 boxed buttons, which are Connect as and Test settings. On the bottom right corner are boxed buttons OK and cancel.

Figure 18-3

The details filled in to create an alias in the default website

Step 3: Configure the SSH Connection Between the Local Computer and the EC2 Instance

As part of the Calculator web application’s deployment process, you need to copy Calculator.html from your local computer to the EC2 instance inside the directory C:DeployedCalculatorApp (to which your virtual directory in IIS is pointing). To copy this file from your local computer to an EC2 instance, you need to establish an SSH connection with the EC2 instance. Follow these steps to configure the required settings:
  1. 1.

    Generate an SSH key pair on a local computer: Use the ssh-keygen -t rsa command to generate an SSH public-private key pair.

     
This will generate public and private key files on the local computer in the ${CURRENT_USER}.ssh folder.
  1. 2.

    Install the OpenSSH Server on an EC2 instance: On the EC2 instance, choose the Windows Start ➤ Settings menu to open the Settings page.

     
  2. 3.

    Click the Apps option, which will open the Apps & Features window.

     
  3. 4.

    Click the Manage Optional Features link.

     
  4. 5.

    Click the Add a Feature button.

     
  5. 6.

    Locate the OpenSSH Server option in the list.

     
  6. 7.

    Click the Install button.

     
  7. 8.

    Create an authorized_keys file on the EC2 instance and paste the public key into it: Go to the EC2 instance, open the command prompt from the C:usersadministrator directory, and run the mkdir .ssh command to create the .ssh folder.

     
  8. 9.

    From the command prompt, enter into the .ssh folder using the cd command and create a blank file named authorized_keys. Do this by running the following command:

     
fsutil file createnew authorized_keys 0
This will create the authorized_keys file in the .ssh folder.
  1. 10.

    Open this file and paste the public key you generated from the key pair.

     
  2. 11.

    Save the changes.

     
  3. 12.

    Set the permissions of the authorized_keys file to the Administrator user: Right-click the authorized_keys file and click the Properties menu.

     
  4. 13.

    Click the Security tab.

     
  5. 14.

    Click the Advanced button.

     
  6. 15.

    Click the Disable Inheritance button.

     
  7. 16.

    Select the Convert Inherited Permissions Into Explicit Permissions on this Object option.

     
  8. 17.

    Click the OK button.

     
  9. 18.

    Click the OK button from the authorized_keys Properties window.

     
  10. 19.

    Modify the sshd_config file on the EC2 instance: Go to C:ProgramDatassh.

    Note If the ProgramData folder is not shown, make hidden files/folders visible from the View menu.

     
  11. 20.

    Open the sshd_config file, scroll down, and comment out the last two lines by adding # to the start of these lines, as shown in Figure 18-4.

     

A desktop page with an open folder of a Remote Desktop Connection is maximized. It starts with a menu bar and a navigation pane on the left side. On the center is a text file named s s h d config, consisting of codes, with a boxed one at the bottom part for Match Group administrators.

Figure 18-4

The last two lines commented out from the sshd_config file

  1. 21.

    Save the file. Go to the Services window by choosing the Windows Start ➤ Services menu and restarting the Open SSH Server service.

     
  2. 22.

    Allow the SSH connection between the local computer and the EC2 instance: Before you can connect to the EC2 instance using SSH, you need to allow this connection by creating an inbound rule in the EC2 instance settings on AWS.

     
  3. 23.

    Go to the AWS console and click the Instances link inside the Instances section on the left side.

     
This will open the Instances page.
  1. 24.

    Click the checkbox shown for the running instance

     
  2. 25.

    Click the Security tab.

     
  3. 26.

    Click the link shown under the security groups label inside the Security tab.

     
  4. 27.

    Click the Edit Inbound Rules button.

     
  5. 28.

    After clicking this button, you will see the Edit Inbound Rules page.

     
  6. 29.

    Click the Add Rule button.

     
  7. 30.

    Select SSH in the Type column dropdown for the newly created entry.

     
  8. 31.

    Specify 0.0.0.0/0 in the Source column.

     
  9. 32.

    Click the Save Rules button.

     

This rule will be added to the existing rules in the security group.

This rule will allow the SSH connection to be made with the EC2 instance using port 22 (the default SSH port).
  1. 33.

    Test the SSH connection between the local computer and the EC2 instance: Now you have all the necessary configurations in place. Let’s test the connectivity using SSH between the local computer and the EC2 instance by running the following command:

     
ssh <UserName>@<EC2Instance_PublicIPV4DNS>
The command I am using is as follows:
  1. 34.

    You can get the publicIPv4DNS from the Details page of the EC2 instance.

     
  2. 35.

    I executed the following command from the command prompt:

     

This will ask, Are You Sure You Want to Continue Connecting (Yes/No/[Fingerprint])? Prompt. Choose Yes and press Enter.

You should now be able to successfully connect to the EC2 instance through SSH.

Copying the Calculator.html File to the EC2 Instance

Before you create a Jenkins job that will deploy the CalculatorWebApplication by copying Calculator.html to the EC2 instance, you need to test the copying functionality by running the scp command.

You need to use the following command for this purpose:
scp -i /directory/to/privatekey /your/local/file/to/copy [email protected]:path/to/file

I want to copy the Calculator.html file to this folder:

D:JenkinsBookExamplesCalcualtorWebApplicationsrcmainwebapp to C:DeployedCalculatorApp on the EC2 instance. Therefore, my command is as follows:
scp –I
C:Usersmagicuser.sshid_rsa  D:JenkinsBookExamplesCalcualtorWebApplicationsrcmainwebappCalculator.html [email protected]
pute.amazonaws.com:C:DeployedCalculatorApp

After running this command from the command prompt, the Calculator.html file should be copied successfully to the EC2 instance inside the C:DeployedCalculatorApp folder.

Accessing Calculator.html Copied to the EC2 Instance from a Browser on a Local Computer

Let’s now access the Calculator.html file from the browser on the local computer. Before you access the EC2 instance using the HTTP URL, you need to create an inbound rule to allow this connection. Follow the same steps you performed previously to create an inbound rule for SSH. The only change is that you need to select HTTP in the Type dropdown.

Go to the EC2 instance, choose the Windows Start ➤Services menu, and restart the World Wide Web Publishing service. Open a browser on your local computer and access this URL:

http://<EC2InstancePublicIP4DNS>/CalculatorWebApp/Calculator.html

I used this URL:

http://ec2-3-141-17-90.us-east-2.compute.amazonaws.com/CalculatorWebApp/Calculator.html

Creating a Jenkins Job to Deploy the Calculator Web Application on an EC2 instance

I created a free-style job called BuildAndDeployCalculatorWebApplicationOnEC2Instance. This job contains an Execute Windows batch command build step using the scp command shown here:
scp -i C:Usersmagicuser.sshid_rsa  srcmainwebappCalculator.html [email protected]:C:DeployedCalculatorApp

We have already discussed this command. This job will pull changes from the CalculatorWebApplication SCM. Using the scp command, it will copy Calculator.html from the workspace directory to the Amazon EC2 instance inside the C:DeployedCalculatorApp folder.

After the build step is complete, this job will call the downstream job TestCalculatorWebApplication, which will start the Selenium tests. This job is mentioned in the Projects to Build field in the Post-Build Actions section. Click the Save button.

Changing the Calculator Application URL in Selenium Framework

The Selenium framework accesses the Calculator.html file from your local computer. Now you want that framework to access the Calculator.html file deployed on the EC2 instance and run tests on it.

You need to change the ApplicationURL environment variable in EnvVars.csv to include the EC2 instance’s calculator.html URL.

Commit and push this change to the Selenium framework repository. I pushed the change to my framework repository:
  • gitlab.com/Pranoday/JenkinsBookSeleniumPythonFramework.git

This is where the Selenium framework and tests will be pulled and executed.

Running the BuildAndDeployCalculatorWebApplication OnEC2Instance Job

Let’s run the BuildAndDeployCalculatorWebApplicationOnEC2Instance job by clicking the clock sign on the Jenkins dashboard. Click the Build button.

You can see the console log of the build of the BuildAndDeployCalculatorWebApplicationOnEC2Instance job.

After completing its tasks, it will trigger the TestCalculatorWebApplication job.

This job will start testing the Calculator web application, which its upstream job has deployed on the EC2 instance.

Summary

This chapter explained what an EC2 instance is and how to create one. Then you installed the IIS web server on an EC2 instance and created a web alias to deploy the Calculator web application. You configured an SSH public key on the EC2 instance so that it can be connected over SSH. You also learned how to create inbound rules in the Security group attached to an EC2 instance to be able to connect it to HTTP and SSH. Finally, you created a Jenkins job which pulled the Calculator application and copied the Calculator.html file on the EC2 instance using the scp command. As of now, you have interacted with Jenkins through its UI. In the next chapter, you learn how to control Jenkins using its CLI (command-line interface).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.248.159