> For the complete documentation index, see [llms.txt](https://tkssharma-devops.gitbook.io/devops-training/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tkssharma-devops.gitbook.io/devops-training/devops-01-continuous-integration/continuous-integration-and-continuous-delivery/ci-cd-using-jenkins-ci/jenkins-ci-cd-with-aws/jenkins-ci-with-ec2.md).

# Jenkins CI with EC2

Lets push code to EC2 Remote Server from Jenkins Build Server &#x20;

![linux server talking to jenkins server](/files/-LjjebTOWspfO_cC33ne)

## Connecting with EC2 from Jenkins

**If you want to run commands on the AWS EC2 Instance tool using Jenkins, this blog can help you to connect your self-hosted Jenkins with AWS EC2 Instance, using the SSH remote host plugin provided by Jenkins.**

### Pre-requisites

1. One Up & Running AWS – EC2 Instance
2. One UP  & Running Jenkins Server
3. AWS EC2 Public IP Address
4. AWS EC2 Pem File

#### Step 1

* Go to: Jenkins -> Manage Jenkins -> Manage Plugins -> Available Tab
* Search For ‘**SSH Plugin**‘
* Download and Install that Plugin

The plugin shall appear in the installed tab.

![](https://www.thesunflowerlab.com/wp-content/uploads/2018/01/Screen-Shot-2018-01-29-at-8.07.23-PM.png)

#### Step 2

* Go to: Jenkins -> Manage Jenkins -> Configure System

Now “SSH remote hosts” option will appear on this page.

![](https://www.thesunflowerlab.com/wp-content/uploads/2018/01/Screen-Shot-2018-01-29-at-8.09.42-PM.png)

* “Add” button will appear in the SSH remote hosts section.
* This plugin can connect multiple EC2 Instances.
* Add button will ask for a number of parameters as described in the image above.

#### Parameter Values

* Hostname: Public IP of EC2 Instance or any Domain name of EC2 Instance
* Port: 22 ‘Because 22 Port is for SSH Connections’
* pty, serverAliveInterval, timeout: put the value as described in above image
* Need to **Add Credentials** by clicking Add button next to Credentials Drop Down.

#### Step 3

![](https://www.thesunflowerlab.com/wp-content/uploads/2018/01/Screen-Shot-2018-01-29-at-8.23.04-PM.png)

* Add Credentials Popup will appear as explained in the image above.<br>

#### Parameter Values

* Kind: SSH Username with Private Key
* Scope: Global
* Username: ec2-user ‘or Any Username of your AWS EC2 Instance’
* Private Key: Select ‘Enter directly’. Here we will add Pem file
  * Key: Open Pem file with text -> Copy Content -> Paste here.
* Passphrase: leave it blank
* ID: leave it blank
* Description: Put any Name for your Connection.

#### Step 4

* Back to SSH remote hosts in Manage Jenkins -> Configure System.
* Select the credentials that you created recently.
* Then hit ‘Check Connection’ Button.

![](https://www.thesunflowerlab.com/wp-content/uploads/2018/01/Screen-Shot-2018-01-29-at-8.33.39-PM-2.png)

## Publish Code to EC2

> **Step 1** : Configure correctly a jenkins job ready to build your app in remote ec2 machine.

* Install this plugin in your jenkins platform : [Publish Over SSH Plugin](https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin)
* Using this plugin add **new remote server** under **Publish over SSH** section in **Manage Jenkins >> Configure System** option.

![](/files/-LjtLy0UaHyncYXICBKh)

* Now create some jenkins job. Then, in build section add a step called : **Send files or execute commands over SSH**

![configure git repo](/files/-LjtMG-XF3ghoGwR0-Vj)

![add build now](/files/-LjtMIO7zC0zLhklGAnf)

![enter image description here](https://i.stack.imgur.com/SQmSX.png)

![](/files/-LjtLdiXcX4wjzeH34SV)
