Create JMeter Golden Image using Packer on AWS
Tags: aws, performance, devops, packer
Published: 2022-01-31 16:16:07

<!-- wp:paragraph -->
<p>In the last post, we saw about <a href="https://qainsights.com/deploy-jmeter-on-aws-using-terraform/" target="_blank" rel="noreferrer noopener">deploying JMeter on AWS using HashiCorp Terraform</a>. I have also posted a <a href="https://youtu.be/FzOXaCN4WW0" target="_blank" rel="noreferrer noopener">YouTube video</a> about it. Terraform helps to create and manage the infrastructure quickly and efficiently. When you use the <a href="https://registry.terraform.io/modules/QAInsights/jmeter/aws/latest" target="_blank" rel="noreferrer noopener">JMeter module</a>, it basically installs Java, JMeter, and JMeter plugins on the fly on an EC2 instance. How about creating an image with JMeter and dependencies for multiple platforms? Enter HashiCorp Packer. In this blog post, we will see how to Create JMeter Golden Image using Packer on AWS.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="what-is-a-packer">What is a Packer?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><a href="https://www.packer.io/">Packer</a>&nbsp;is an open source tool from HashiCorp that enables you to create identical machine images for multiple platforms from a single source template.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Packer helps to collaborate between multiple teams to create, manage, and consume golden images. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="what-is-a-golden-image">What is a Golden Image?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The Golden Image is nothing but a template. It is a template for a Virtual Machine, server, desktop, or container. The other names of golden image are master image, clone image etc. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The Golden Image will have all the necessary software libraries, configuration settings and other dependencies to get started. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="prerequisites">Prerequisites</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The following are the prerequisites to create a golden image for JMeter on AWS using Packer:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Packer</li><li>AWS Console access w/ relevant privileges</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 id="getting-started">Getting Started</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3 id="packer">Packer</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Head to packer.io to install Packer based on your operating system. In this demo, I am going to use WSL for Linux.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

sudo apt-add-repository "deb &#91;arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

sudo apt-get update &amp;&amp; sudo apt-get install packer</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>To install auto-complete, enter <code>packer -autocomplete-install</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To verify the installation, enter <code>packer version</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 id="aws-cli-configure">AWS CLI Configure</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Install the AWS CLI and configure the access ID, secret key, default region, and output.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 id="packer-jmeter-repo">Packer JMeter Repo</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Clone the Packer JMeter repo into your folder. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>git clone https://github.com/QAInsights/Packer-JMeter.git</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>cd into <code>Packer-JMeter\file</code> or <code>Packer-JMeter\shell</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Before building the packer, make sure all the configurations are properly entered in <code>jmeter-amazon.pkr.hcl</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you would like to customize the region, JMeter version, JMeter plugins and more, you can make changes at <code>jmeter-amazon.pkr.hcl</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:generateblocks/button-container {"uniqueId":"cae0a534","alignment":"center","isDynamic":true,"blockVersion":2} -->
<!-- wp:generateblocks/button {"uniqueId":"844363c3","hasUrl":true,"target":true,"backgroundColor":"var(\u002d\u002dglobal-color-8)","textColor":"#ffffff","backgroundColorHover":"#222222","textColorHover":"#ffffff","paddingTop":"15","paddingRight":"20","paddingBottom":"15","paddingLeft":"20","gradientDirection":90,"gradientColorOne":"rgba(255, 255, 255, 0.1)","gradientColorTwo":"rgba(0, 0, 0, 0.30)"} -->
<a class="gb-button gb-button-844363c3 gb-button-text" href="https://github.com/QAInsights/Packer-JMeter.git" target="_blank" rel="noopener noreferrer">JMeter Packer GitHub Repo</a>
<!-- /wp:generateblocks/button -->
<!-- /wp:generateblocks/button-container -->

<!-- wp:heading -->
<h2 id="building-the-image">Building the Image</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before building the image, it is recommended to inspect the packer file. Enter <code>packer inspect jmeter-amazon.pkr.hcl</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The following would be the output.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>Packer Inspect: HCL2 mode

&gt; input-variables:

var.jmeter_cmdrunner_version: "2.2"      
var.jmeter_plugins_manager_version: "1.7"
var.jmeter_version: "5.4.3"

&gt; local-variables:


&gt; builds:

  &gt; &lt;unnamed build 0&gt;:

    sources:

      amazon-ebs.jmeter

    provisioners:

      shell

    post-processors:

      &lt;no post-processor&gt;</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>To format, enter <code>packer fmt .</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To validate, enter <code>packer validate .</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Finally, to build the image, enter <code>packer build .</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Packer will validate all the configurations, create a temporary key pair, temporary security group, temporarily spins up a <code>t2.small</code> EC2 instance to create the image.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>During the image creation process, Packer will download and install Java, JMeter, and JMeter Plugins on the EC2 instance. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Typically, it will take 3-5 minutes to create the image.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If any error occurs, it will be displayed on the terminal. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The output of this process will be AMI ID and the region. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is the partial output.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>amazon-ebs.jmeter: output will be in this color.

==&gt; amazon-ebs.jmeter: Prevalidating any provided VPC information
==&gt; amazon-ebs.jmeter: Prevalidating AMI Name: jmeter_1643499054
    amazon-ebs.jmeter: Found Image ID: ami-001089eb624938d9f
...
...
...
...
==&gt; amazon-ebs.jmeter: No volumes to clean up, skipping
==&gt; amazon-ebs.jmeter: Deleting temporary security group...
==&gt; amazon-ebs.jmeter: Deleting temporary keypair...
Build 'amazon-ebs.jmeter' finished after 3 minutes 59 seconds.

==&gt; Wait completed after 3 minutes 59 seconds

==&gt; Builds finished. The artifacts of successful builds are:
--&gt; amazon-ebs.jmeter: AMIs were created:
us-east-2: ami-095b20c4727d864cb</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Congratulations. You have created a golden image for JMeter.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="spin-up-an-ec2-instance-on-aws">Spin up an EC2 instance on AWS </h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The next step is to create an EC2 instance on AWS using this golden image. Head to AWS console and select the right region. In this case, it would be Ohio.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Navigate to <strong>EC2 &gt; AMIs</strong> section. Select the AMI, click on <strong>Launch instance from Image</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":9044,"sizeSlug":"large","linkDestination":"media"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><a href="https://qainsights.com/wp-content/uploads/2022/01/image-17.png"><img src="https://qainsights.com/wp-content/uploads/2022/01/image-17-1024x103.png" alt="Launch Instance from Image" class="wp-image-9044"/></a><figcaption>Launch Instance from Image</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Follow the wizard to configure the instance family, storage, security group, tags and more. Finally, click on <strong>Review and Launch</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="validate-the-ami">Validate the AMI</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>After the successful launch of EC2 instance, ssh into it to validate the JMeter. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Copy the public IP of the EC2 instance to SSH into the instance.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>$ ssh -i &lt;key.pem&gt; ec2-user@&lt;public-ip&gt;</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>To validate the JMeter folder, enter <code>ls</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To validate the JMeter plugins' status, enter the following commands from the home directory.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>$ sudo chmod +x apache-jmeter-5.4.3/bin/PluginsManagerCMD.sh

$ sudo apache-jmeter-5.4.3/bin/PluginsManagerCMD.sh status</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The output will display the installed JMeter plugins. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="deregister-and-delete">Deregister and Delete</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Once the validation is done, do not forget to terminate the instance.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To deregister the AMI, go to <strong>EC2 &gt; AMIs</strong>, select the AMI to deregister, then go to <strong>Actions &gt; Deregister AMI</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":9045,"sizeSlug":"full","linkDestination":"media"} -->
<div class="wp-block-image"><figure class="aligncenter size-full"><a href="https://qainsights.com/wp-content/uploads/2022/01/image-18.png"><img src="https://qainsights.com/wp-content/uploads/2022/01/image-18.png" alt="Deregister AMI" class="wp-image-9045"/></a><figcaption>Deregister AMI</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>These AMIs are created using EBS backed volume. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Amazon EBS-backed AMI – The root device for an instance launched from the AMI is an Amazon Elastic Block Store (Amazon EBS) volume created from an Amazon EBS snapshot.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For EBS-backed AMI, there are charges on instance usage, EBS volume usage, storing the AMI as an EBS snapshot. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In the Packer file, you can see the name of the AMI is <strong>amzn2-ami-kernel-5.10-hvm-*-x86_64-gp2</strong>. The cost of gp2 at this time of writing is $0.10&nbsp;per GB-month of provisioned storage.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It is critical to delete the unwanted EBS snapshots from <strong>EC2 &gt; Snapshots </strong>under <strong>Elastic Block Store</strong> section.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Select the snapshot, go to <strong>Actions &gt; Delete</strong> snapshot.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":9046,"sizeSlug":"full","linkDestination":"media"} -->
<div class="wp-block-image"><figure class="aligncenter size-full"><a href="https://qainsights.com/wp-content/uploads/2022/01/image-19.png"><img src="https://qainsights.com/wp-content/uploads/2022/01/image-19.png" alt="Delete EBS snapshot" class="wp-image-9046"/></a><figcaption>Delete EBS snapshot</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:heading -->
<h2 id="conclusion">Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Creating a golden image using Packer helps to collaborate with multiple teams, manage the image for a quick and easy deployment for performance testing. Packer helps to create images across multiple platforms, gels with the CI/CD pipeline, and is easy to learn and implement. </p>
<!-- /wp:paragraph -->