Amazon EC2 Jenkins plugin

Launch EC2 instances as Jenkins agents on demand

In the previous Jenkins in AWS post, we covered installing and running Jenkins on an AWS EC2 instance. Here, weโ€™ll learn how to launch EC2 instances as Jenkins agents (slaves) on demand using the EC2 plugin.

๐Ÿ›ก๏ธ Grant Jenkins EC2 Instance Permissions

  1. Go to AWS Console โ†’ IAM โ†’ Roles โ†’ Create role.
  2. Select AWS Service and then EC2.
  3. Attach the AmazonEC2FullAccess policy.
  4. (Optional) Add tags.
  5. Name the role (e.g., jenkins-ec2-role) and create it. Create IAM Role
  6. Attach the new role to our running Jenkins EC2 instance via Actions โ†’ Instance Settings โ†’ Attach/Replace IAM Role.

๐Ÿ”’ Create Security Group for Jenkins Agents

  1. Go to Network & Security โ†’ Security Groups โ†’ Create Security Group.
  2. Name it (e.g., sg_jenkins_slaves).
  3. Add an inbound SSH rule, and set our Jenkins master instanceโ€™s security group as the source. Security Group

๐Ÿ”Œ Install and Configure the EC2 Plugin in Jenkins

  1. In Jenkins, go to Manage Jenkins โ†’ Manage Plugins.
  2. Install the Amazon EC2 plugin. Install Plugin
  3. Go to Manage Jenkins โ†’ Configure System โ†’ Cloud and add a new cloud of type Amazon EC2.
  4. Configure as follows:
    • Name: AWS Cloud
    • Use EC2 instance profile to obtain credentials
    • Region: (same as our Jenkins EC2 instance)
    • EC2 Key Pairโ€™s Private Key: (our AWS key)
    • AMI ID: (same as our 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 build jobs with label
    • Idle termination time: (set as needed)
    • Advanced: Associate Public IP

    Plugin Config 1 Plugin Config 2 Plugin Config 3

๐Ÿšฆ Use Jenkins Agents On Demand

Once saved, we can use the aws label in Jenkins jobs to automatically launch EC2 agent instances. Idle agents will terminate after the configured idle time.