Amazon EC2 Jenkins plugin

Launch EC2 instances as Jenkins slaves on demand

In previous Jenkins in AWS post, we looked at the steps to install and run jenkins on AWS EC2 instace. In here we will see how to launch AWS EC2 instances as Jenkins slaves using EC2 plugin.

First we need to give permission to current Jenkins EC2 instance to create additional EC2 instances.

  1. In AWS EC2 console, go to Services –> Security, Identity, & Compliance –> IAM –> Roles, then click on “Create role”.
  2. In the create role page, select “AWS Service” and EC2 respectively.
  3. Next page, select “AmazonEC2FullAccess” as the permission policy
  4. Add tags in next page if needed
  5. Give the role name as “jenkins-ec2-role” and create it placeholder
  6. Go back to the running EC2 instance dashboard and select the current instance for jenkins, under Actions –> Instance Settings –> Attach/Replace IAM Role, select the role created in the above step 5 as IAM role.

Next, security group need to be created to give the slave EC2 instances to connect to Jenkins EC2 instance.

  1. Go to Network & Security –> Security Groups –> Create Security Group
  2. Give the security group name as “sg_jenkins_slaves”, add Inbound SSH rule and select current jenkins master instances’ security group as custom source.Then create it. placeholder

Next step is to install EC2 plugin in Jenkins and configure it

  1. Go to Jenkins Dashabord –> Mange Jenkins –> Manage Plugins
  2. From the availble plugins, select Amazon EC2 plugin and install it without restart placeholder
  3. Once the plugin is installed, go to Jenkins Dashabord –> Configure Jenkins –> Cloud, click on cloud configuration
  4. Under Add a new cloud, select “Amazon EC2” and configure as shown below
    • Name: AWS Cloud
    • Choose Use EC2 instance profile to obtain credentials
    • Select Region same as jenkins EC2 instance
    • Add your aws private key under EC2 Key Pair’s Private Key
    • Click on AMIs
    • Select AMI ID same as jenkins EC2 instance
    • Instance Type: T2Micro
    • Security group names: sg_jenkins_slaves
    • Remote FS root: /home/ec2-user
    • Remote user: ec2-user
    • Labels: aws
    • Usage: Only builds maching the label
    • Idle termination time: this can be set based on your preference
    • Click Advanced…
    • Select Associate Public IP

placeholder placeholder placeholder

Once configuration is saved, you can start using aws label in Jenkins jobs to automatically launch EC2 slave instances. If the slave EC2 instance is idle for more than the Idle termination time, it will automatically terminates.