Deploying Amazon EC2 in Jenkins

Adefemi Afuwpe
4 min readJun 24, 2019
Jenkins+AWS

Have you ever wondered how you could launch your new Instance from Jenkins and give it some basic commands that you would love to see run as your instance gets launched? Well, this article will walk you through how you can do that.

The first thing you will need to do is to start your Jenkins, input your credentials and click on sign in

Jenkins Homepage

After that you will need to install the Amazon EC2 plug-in either from https://wiki.jenkins.io/display/JENKINS/Amazon+EC2+Plugin or from the plug in manager by clicling on Manage Jenkins from the left side of the pane then click on Manage plugins then click on Available and search for Amazon EC2 plugin and click on install

Amazon EC2 plugin

Once this is successfully installed, you need to log in to your AWS account and set some policies that allows Jenkins launch EC2 instance. So log in to your AWS console, click on services and type in IAM and hit Enter , Lets create a new policy that we will be using, click on Policies then click on Create Policy, click on JSON and paste the following policies

{
“Version”: “2012–10–17”,
“Statement”: [
{
“Sid”: “VisualEditor0”,
“Effect”: “Allow”,
“Action”: [
“ec2:DescribeInstances”,
“ec2:TerminateInstances”,
“ec2:RequestSpotInstances”,
“ec2:DeleteTags”,
“ec2:CreateTags”,
“ec2:DescribeRegions”,
“ec2:RunInstances”,
“ec2:DescribeSpotInstanceRequests”,
“ec2:StopInstances”,
“ec2:DescribeSecurityGroups”,
“ec2:GetConsoleOutput”,
“ec2:DescribeSpotPriceHistory”,
“ec2:DescribeImages”,
“ec2:CancelSpotInstanceRequests”,
“iam:PassRole”,
“ec2:StartInstances”,
“ec2:DescribeAvailabilityZones”,
“ec2:DescribeSubnets”,
“ec2:DescribeKeyPairs”
],
“Resource”: “*”
}
]
}

and on the Visual Editor Tab search for IAM and add the policy as well and click on Review Policy, Give your policy a name and click on Create Policy.

Welldone, we have created two policies, now let's add the policy to a user, i will advice you create a new User for this article, Click on Add User and then give it a user name, on the Access type, check Programmatic Access and then click on Next permission, click on the Attach Existing Policy Directly tab and search for the policy you created, add a tag, click on Review and Click on Create User. You need to click on download .csv to save your secret access key because that is the only time you are going to see it.

AWS Access Key ID and Secret Access Key

Right about now we are half way done, let go back to Jenkins, click on Manage JEnkins and click on Configure System scroll down till you find a drop down: Add a New Cloud

Adding Amazon EC2

Now, let’s do some setting for our EC2, first type in the same name you gave your AWS User, For the Amazon EC2 credentials, click on Add and then select Jenkins, on Kind search for AWS Credentials and type in the Access Key ID and Access Secret Key, give it a Name and click on Add, to get an EC2 Key Pair’s private Key, you’ll need to sign in to you AWS account or create one using SSH, its an RSA private key. To create one using AWS, follow this link https://docs.aws.amazon.com/cli/latest/reference/ec2/create-key-pair.html, https://www.cloudbees.com/blog/setting-jenkins-ec2-slaves or https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent Copy and paste the RSA Key generated and click on Test Connection

Test Connection for EC2 Credentials

On the AMI page, give your AMI a name, copy and paste one of the free tier AMI ID and click on Check AMI to be sure the AMI selected is available, you should get a response like the one below

Check AMI

I will be using a T2 Micro because it’s free and for my init script, i just want to print out Hello World once the EC2 starts, you can install any components you’d want to launch with your EC2. Input a name for your Labels (eg: awscloud)

Settings for EC2

Click on Apply and Click on Save. Create a new Job, for me the Job am creating send a notification to a Slack Channel and then Launch an EC2

pipeline {
agent { label ‘awscloud’ }

stages {
stage(‘Slack Message’) {
steps {
slackSend channel: ‘#job-info’,
color: ‘good’,
message: “*${currentBuild.currentResult}:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}”

}
}
}
}

And there you have it click on Build now and watch Your Instance gets Created

EC2 Instance Success

You can as well check you Jenkins SysLog for any success output like the one below

Jenkins System Log

And there you have it, you’ve successfully launched an EC2 Instance right from Jenkins

--

--