<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sushant Patil</title>
    <description>The latest articles on DEV Community by Sushant Patil (@sushant_patil).</description>
    <link>https://dev.to/sushant_patil</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1269614%2Fd2bd362a-b94e-4b72-a931-977ba32d4b88.jpg</url>
      <title>DEV Community: Sushant Patil</title>
      <link>https://dev.to/sushant_patil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sushant_patil"/>
    <language>en</language>
    <item>
      <title>Smart Cost Optimization: Leveraging Jenkins Slaves on AWS EC2 Spot Instances with the EC2 Plugin</title>
      <dc:creator>Sushant Patil</dc:creator>
      <pubDate>Fri, 02 Feb 2024 14:51:37 +0000</pubDate>
      <link>https://dev.to/sushant_patil/maximizing-cost-efficiency-in-jenkins-builds-through-dynamic-ec2-spot-instances-2ejf</link>
      <guid>https://dev.to/sushant_patil/maximizing-cost-efficiency-in-jenkins-builds-through-dynamic-ec2-spot-instances-2ejf</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction:&lt;/strong&gt;&lt;br&gt;
Jenkins, a widely-used automation server, facilitates the automation of building, testing, and deploying applications. In this blog post, we'll explore an innovative approach using Jenkins EC2 Plugin to provision and terminate spot instances dynamically, optimizing costs and addressing the challenges associated with idle slave instances.&lt;/p&gt;

&lt;h2&gt;
  
  
  Perquisites:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Setting up Jenkins Master&lt;/strong&gt;&lt;br&gt;
Ensure you have a server with Jenkins installed, which will act as the master. If not, follow the official Jenkins installation guide.&lt;br&gt;
&lt;a href="https://www.jenkins.io/doc/book/installing/linux/" rel="noopener noreferrer"&gt;Jenkins installation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Creating a Custom AMI&lt;/strong&gt;&lt;br&gt;
To expedite the EC2 instance provisioning process, create a customized AMI with the required tools and dependencies pre-installed. This ensures a faster setup and avoids delays in the build process. &lt;strong&gt;Note: (Java is must in the ami)&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Advantages:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Cost Savings:&lt;/strong&gt;&lt;br&gt;
Leveraging spot instances allows for significant cost savings compared to on-demand instances, making it a cost-effective solution for intermittent build processes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Storage Efficiency:&lt;/strong&gt;&lt;br&gt;
Dynamic provisioning eliminates the need for maintaining idle instances, reducing storage costs and optimizing resource utilization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fresh EC2 Instances:&lt;/strong&gt;&lt;br&gt;
With every build, a fresh EC2 instance is provisioned, ensuring a clean and consistent environment for testing and deployment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS Marketplace AMIs:&lt;/strong&gt;&lt;br&gt;
Access AWS Marketplace to explore and utilize pre-configured AMIs, minimizing the effort required to manage and update instances manually.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Disadvantages:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Provisioning Time:&lt;/strong&gt;&lt;br&gt;
The process of spinning up EC2 instances may introduce a delay of a few minutes, impacting the overall build time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AMI Preparation:&lt;/strong&gt;&lt;br&gt;
Creating a custom AMI with the necessary tools requires upfront effort, and any changes to the environment necessitate updating the AMI.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Steps to setup and configure EC2 Spot instances&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Installing EC2 Plugin&lt;/strong&gt;&lt;br&gt;
Install the Jenkins EC2 Plugin on the master. This plugin enables dynamic provisioning of EC2 instances as build slaves. Navigate to Jenkins Dashboard -&amp;gt; Manage Jenkins -&amp;gt; Manage Plugins -&amp;gt; Available tab, search for "EC2" and install the plugin.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88zk93fsaf68kuo5sbnf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88zk93fsaf68kuo5sbnf.png" alt="Jenkins Plugin ec2 plugin"&gt;&lt;/a&gt;&lt;br&gt;
**&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Configuring EC2 Plugin&lt;/strong&gt;&lt;br&gt;
Navigate to the "Manage Jenkins" section and select the "Cloud" option to configure slave settings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvqtjfblkhlov2low83y1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvqtjfblkhlov2low83y1.png" alt="jenkins cloud "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0e6yfx60jwavwuc2g30.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0e6yfx60jwavwuc2g30.png" alt="jenkins new cloud"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk03xax42a0od7aw6by6m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk03xax42a0od7aw6by6m.png" alt="plugin profile info"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Setting Bid price&lt;/strong&gt;&lt;br&gt;
To prevent immediate spot termination and avoid pending status due to a low bid price, include an additional value when setting the bid price. This will help ensure the stability of your spot slave instances.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frz4m0k5ekf57bljb587r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frz4m0k5ekf57bljb587r.png" alt="setting bid price"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Setting ami id and instance type&lt;/strong&gt;&lt;br&gt;
In this step, include the AMI ID of your customized AMI, or alternatively, utilize AWS Marketplace for this purpose.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmylmrvr2vpnrnlxflfb0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmylmrvr2vpnrnlxflfb0.png" alt="instance type"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Setting label and termination time&lt;/strong&gt;&lt;br&gt;
The crucial step is to set an idle termination time, which ensures the termination of spot instances if they remain idle for more than 10 minutes. Additionally, labeling is important to schedule workload on specific slaves; otherwise, it may run on the master.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3e2sidmwtzr1sxgv6bp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3e2sidmwtzr1sxgv6bp.png" alt="labeling"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: sample Jenkins pipeline&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pipeline {
    agent {
        label 'jenkins-slave'
    }
    stages {
        stage("print") {
            steps {
                echo "hello"
            }
        }
    }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
  </channel>
</rss>
