<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://blog.tafaribeckford.com</link><generator>RSS for Node</generator><lastBuildDate>Fri, 13 Nov 2026 16:48:23 GMT</lastBuildDate><atom:link href="https://blog.tafaribeckford.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Working with Ansible Dynamic Inventory using AWS]]></title><description><![CDATA[Maintaining the inventory file while working with Ansible with AWS can be a difficult process owing to frequent changes in dynamic components of the…]]></description><link>https://blog.tafaribeckford.com/working-with-ansible-dynamic-inventory-using-aws</link><guid isPermaLink="true">https://blog.tafaribeckford.com/working-with-ansible-dynamic-inventory-using-aws</guid><category><![CDATA[AWS]]></category><category><![CDATA[ansible-playbook]]></category><category><![CDATA[ansible]]></category><dc:creator><![CDATA[Tafari Beckford]]></dc:creator><pubDate>Mon, 05 Jun 2023 23:00:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1685748695219/89aedcc2-b212-4a9b-8de0-98a8627ca616.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Maintaining the inventory file while working with Ansible with AWS can be a difficult process owing to frequent changes in dynamic components of the infrastructure.</p>
<p>Fortunately, there is a convenient solution called Ansible Dynamic Inventory. Dynamic inventory is an Ansible plugin that utilizes API calls to AWS to retrieve instance information during runtime. This approach provides you with the ability to manage the AWS infrastructure dynamically by obtaining up-to-date details of EC2 instances.</p>
<p>By leveraging dynamic inventory, you can automate the process of discovering and managing your AWS resources without the need for manually updating the inventory file. Instead, Ansible interacts directly with the AWS API to obtain the necessary information, allowing for more flexibility and ease of management.</p>
<p>In this tutorial, you will learn how to configure dynamic inventory on AWS using the AWS ec2 Ansible plugin.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ol>
<li><p>Ansible Installed</p>
</li>
<li><p>Python3, Pip3 and Boto3</p>
</li>
<li><p>AWS Account</p>
</li>
<li><p>SSH key pair</p>
</li>
<li><p>EC2 Instances (preferably 2 or more) tagged</p>
</li>
</ol>
<h2 id="heading-create-a-custom-inventory-file">Create a Custom Inventory File</h2>
<p>Ansible generates an inventory file, which is usually stored in <code>/etc/ansible/hosts</code>. During a playbook or command execution, Ansible uses this as the default location if a custom inventory file is not specified with the <code>-i</code> option.</p>
<p>To get started, access your home folder and create a new directory to hold your Ansible files:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> ~
mkdir ansible
</code></pre>
<ol>
<li>Move to that directory and open a new inventory file using your text editor of choice. Here, we’ll use <code>vim</code>:</li>
</ol>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> ansible
vim aws_ec2.yaml
</code></pre>
<ol>
<li>Copy the following configuration to the file. If your Ansible controller is not in AWS, authentication is handled by either specifying your access and secret key as ENV variables or inventory plugin arguments</li>
</ol>
<pre><code class="lang-yaml"><span class="hljs-attr">plugin:</span> <span class="hljs-string">amazon.aws.aws_ec2</span>
<span class="hljs-comment"># The access key for your AWS account.</span>
<span class="hljs-attr">aws_access_key:</span> <span class="hljs-string">&lt;YOUR-AWS-ACCESS-KEY-HERE&gt;</span>
<span class="hljs-comment"># The secret access key for your AWS account.</span>
<span class="hljs-attr">aws_secret_key:</span> <span class="hljs-string">&lt;YOUR-AWS-SECRET-KEY-HERE&gt;</span>

<span class="hljs-attr">regions:</span>
  <span class="hljs-bullet">-</span> <span class="hljs-string">us-east-1</span>

<span class="hljs-attr">filters:</span>
    <span class="hljs-attr">instance-state-name:</span> <span class="hljs-string">running</span>

<span class="hljs-attr">keyed_groups:</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">key:</span> <span class="hljs-string">tags</span>
    <span class="hljs-attr">prefix:</span> <span class="hljs-string">tag</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">prefix:</span> <span class="hljs-string">instance_type</span>
    <span class="hljs-attr">key:</span> <span class="hljs-string">instance_type</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">key:</span> <span class="hljs-string">placement.region</span>
    <span class="hljs-attr">prefix:</span> <span class="hljs-string">aws_region</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">prefix:</span> <span class="hljs-string">az</span>
    <span class="hljs-attr">key:</span> <span class="hljs-string">placement.availability_zone</span>
</code></pre>
<p><mark>Warning: Do not commit this file to a public git repository</mark></p>
<p>For ENV:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">export</span> AWS_ACCESS_KEY_ID=<span class="hljs-string">'YOUR_AWS_ACCESS_KEY_ID'</span>
<span class="hljs-built_in">export</span> AWS_SECRET_ACCESS_KEY=<span class="hljs-string">'YOUR_AWS_SECRET_ACCESS_KEY'</span>
</code></pre>
<h2 id="heading-edit-ansiblecfg-file">Edit <code>Ansible.cfg</code> file</h2>
<pre><code class="lang-bash">sudo vim /etc/ansible/ansible.cfg
</code></pre>
<ol>
<li>Navigate to the <code>[defaults]</code> section of the file and change the inventory parameter value</li>
</ol>
<pre><code class="lang-bash">inventory = /home/&lt;USERNAME&gt;/ansible/aws_ec2.yaml
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685998148969/22bbd8ff-cfed-4f56-bf8f-49ea12ed089e.png" alt class="image--center mx-auto" /></p>
<ol>
<li>Navigate to the <code>[inventory]</code> section of the file and change the plugin's parameter value. The following line is to enable the ec2 plugin.</li>
</ol>
<pre><code class="lang-bash">enable_plugins = aws_ec2
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685998352590/7684272b-8d31-4c6d-8221-82b7e6f03f8c.png" alt class="image--center mx-auto" /></p>
<ol>
<li><p>Execute the following command to see the listings of the ec2 instances.</p>
<pre><code class="lang-bash"> ansible-inventory --list
</code></pre>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685999250971/92522c7a-85ab-4e21-a64b-032a12fcdd57.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
<h2 id="heading-ec2-instance-grouping">EC2 Instance Grouping</h2>
<p>Tags, instance types, instance names, custom filters, and other criteria can be used to organize instances. View all supported filters and keyed groups from this <a target="_blank" href="https://docs.ansible.com/ansible/latest/collections/amazon/aws/docsite/aws_ec2_guide.html#using-dynamic-inventory-inside-playbook">page</a>.<br />The example below of keyed_groups:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">keyed_groups:</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">key:</span> <span class="hljs-string">tags</span>
    <span class="hljs-attr">prefix:</span> <span class="hljs-string">tag</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">prefix:</span> <span class="hljs-string">instance_type</span>
    <span class="hljs-attr">key:</span> <span class="hljs-string">instance_type</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">key:</span> <span class="hljs-string">placement.region</span>
    <span class="hljs-attr">prefix:</span> <span class="hljs-string">aws_region</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">prefix:</span> <span class="hljs-string">az</span>
    <span class="hljs-attr">key:</span> <span class="hljs-string">placement.availability_zone</span>
</code></pre>
<p>Execute the following command to list the ec2 dynamic inventory groups.</p>
<pre><code class="lang-yaml"><span class="hljs-string">ansible-inventory</span> <span class="hljs-string">--graph</span>
</code></pre>
<p>Instances will be grouped under the tags <code>aws_ec2</code>, <code>aws_region_us_east_1</code>, <code>az_us_east_1d</code> , <code>instanace_type_t2_micro</code> and <code>tag_Name_ubuntu_server</code> .</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685999546331/6d29897f-2e1f-4a15-91ee-f1cf69ff0bc1.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-using-dynamic-inventory-inside-playbook">Using Dynamic Inventory Inside Playbook</h2>
<p>If I want to execute an Ansible playbook on all of the servers tagged <code>ubuntu_server</code>. The sample playbook is shown below. It uses the apt module to install Nginx on all the Ubuntu servers.</p>
<ol>
<li><p>Create an ansible-playbook named <code>ec2-playbook.yaml</code> .</p>
<p> <strong>For Debian and Ubuntu instances use:</strong></p>
</li>
</ol>
<pre><code class="lang-yaml"><span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Playbook</span> <span class="hljs-string">Example</span>
  <span class="hljs-attr">gather_facts:</span> <span class="hljs-literal">false</span>
  <span class="hljs-attr">hosts:</span> <span class="hljs-string">tag_Name_ubuntu_server</span>
  <span class="hljs-attr">tasks:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Install</span> <span class="hljs-string">the</span> <span class="hljs-string">package</span> <span class="hljs-string">Nginx</span>
      <span class="hljs-attr">ansible.builtin.apt:</span>
        <span class="hljs-attr">name:</span> <span class="hljs-string">nginx</span>
        <span class="hljs-attr">state:</span> <span class="hljs-string">present</span>
</code></pre>
<p><strong>For CentOS, Amazon Linux2 and Amazon Linux instances use:</strong></p>
<pre><code class="lang-yaml"><span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Playbook</span> <span class="hljs-string">Example</span>
  <span class="hljs-attr">gather_facts:</span> <span class="hljs-literal">false</span>
  <span class="hljs-attr">hosts:</span> <span class="hljs-string">tag_Name_ubuntu_server</span>
  <span class="hljs-attr">tasks:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Install</span> <span class="hljs-string">the</span> <span class="hljs-string">package</span> <span class="hljs-string">Nginx</span>
      <span class="hljs-attr">ansible.builtin.yum:</span>
        <span class="hljs-attr">name:</span> <span class="hljs-string">nginx</span>
        <span class="hljs-attr">state:</span> <span class="hljs-string">present</span>
</code></pre>
<p>Execute the playbook with the following command:</p>
<pre><code class="lang-yaml"><span class="hljs-string">ansible-playbook</span> <span class="hljs-string">ec2-playbook.yaml</span> <span class="hljs-string">-u</span> <span class="hljs-string">&lt;ec2-instance-username&gt;</span>  <span class="hljs-string">–-private-key=&lt;keyfilename.pem&gt;</span>
</code></pre>
<p><strong>Get the default username for the AMI that you used to launch your instance:</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>AMI used to launch an instance</strong></td><td><strong>Default username</strong></td></tr>
</thead>
<tbody>
<tr>
<td>Amazon Linux2 and Amazon Linux</td><td><code>ec2-user</code></td></tr>
<tr>
<td>CentOS</td><td><code>centos</code> or <code>ec2-user</code></td></tr>
<tr>
<td>Debian</td><td><code>admin</code></td></tr>
<tr>
<td>Ubuntu</td><td><code>ubuntu</code></td></tr>
</tbody>
</table>
</div><p>The image below shows Nginx has been successfully installed on the Ubuntu servers.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1686005320627/a383567f-c99a-491f-b60c-8ba4d159bc4e.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Using Jenkins to create AWS Infrastructure with Terraform.]]></title><description><![CDATA[The finished product of this project is terraform code that builds an AWS environment with three EC2 instances. The Jenkins CI/CD Pipeline will be…]]></description><link>https://blog.tafaribeckford.com/using-jenkins-to-create-aws-infrastructure-with-terraform</link><guid isPermaLink="true">https://blog.tafaribeckford.com/using-jenkins-to-create-aws-infrastructure-with-terraform</guid><category><![CDATA[Jenkins]]></category><category><![CDATA[AWS]]></category><category><![CDATA[Terraform]]></category><category><![CDATA[terraform-cloud]]></category><category><![CDATA[ Jenkins, DevOps]]></category><dc:creator><![CDATA[Tafari Beckford]]></dc:creator><pubDate>Wed, 31 May 2023 03:42:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1685459840948/8f5785e2-7452-403c-8de9-1415848d7111.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The finished product of this project is terraform code that builds an AWS environment with three EC2 instances. The Jenkins CI/CD Pipeline will be triggered when the code is pushed to a GitHub repository via GitHub Webhook.</p>
<h2 id="heading-what-is-jenkins">What is Jenkins?</h2>
<p>Jenkins is an open-source automation server that helps facilitate the continuous integration and delivery (CI/CD) of software projects. It allows developers to automate various stages of the software development lifecycle, including building, testing, and deploying applications.</p>
<h2 id="heading-what-is-terraform">What is Terraform?</h2>
<p>Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It enables you to define and manage your infrastructure resources, such as virtual machines, networks, storage, and services, in a declarative manner. With Terraform, you can provision and manage your infrastructure across various cloud providers and on-premises environments.</p>
<h1 id="heading-prerequisites"><strong>Prerequisites</strong></h1>
<ul>
<li><p><a target="_blank" href="https://github.com/">GitHub</a> Account</p>
</li>
<li><p><a target="_blank" href="https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html">AWS CLI</a></p>
</li>
<li><p><a target="_blank" href="https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/">AWS Account</a></p>
</li>
<li><p>Install <a target="_blank" href="https://learn.hashicorp.com/tutorials/terraform/install-cli">Terraform</a></p>
</li>
<li><p>Jenkins Installed</p>
</li>
<li><p><a target="_blank" href="https://app.terraform.io/signup/account?utm_source=cloud_landing&amp;utm_content=offers_tfc">Terraform Cloud</a> Account</p>
</li>
<li><p>Install <a target="_blank" href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git">Git</a></p>
</li>
</ul>
<h2 id="heading-configure-terraform-cloud">Configure Terraform Cloud</h2>
<p><a target="_blank" href="https://cloud.hashicorp.com/products/terraform">Terraform Cloud</a> is an application that helps teams use Terraform together. It manages Terraform runs in a consistent and reliable environment, and includes easy access to shared state and secret data, access controls for approving changes to infrastructure, a private registry for sharing Terraform modules, detailed policy controls for governing the contents of Terraform configurations, and more.</p>
<ol>
<li>Sign in to <a target="_blank" href="https://app.terraform.io/session">Terraform Cloud</a></li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685385220868/ab670394-7640-4d00-8800-7dde24f12141.png" alt class="image--center mx-auto" /></p>
<p>2. Select <strong>CLI-driven workflow</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685385019206/2210acf5-33a2-4784-99f8-83d7166ba8dc.png" alt class="image--center mx-auto" /></p>
<ol>
<li><p>Name your workspace then Create workspace.</p>
</li>
<li><p>Copy the provided example code and add it to <code>backend.tf</code> to replace the current backend.</p>
<pre><code class="lang-yaml"> <span class="hljs-string">terraform</span> {
   <span class="hljs-string">cloud</span> {
     <span class="hljs-string">organization</span> <span class="hljs-string">=</span> <span class="hljs-string">"YOUR ORGANIZATION NAME"</span>

     <span class="hljs-string">workspaces</span> {
       <span class="hljs-string">name</span> <span class="hljs-string">=</span> <span class="hljs-string">"YOUR WORKSPACES NAME"</span>
     }
   }
 }
</code></pre>
</li>
</ol>
<p>Set the Execution Mode by clicking Remote and selecting <strong>Local</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685386016352/f8bd5f91-c9c2-401a-b1cb-17beeb6ee2db.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-create-terraform-cloud-token">Create Terraform Cloud Token</h3>
<ol>
<li><p>Run <code>terraform login</code> in CLI and Enter the value <code>yes</code>.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685386410934/873ad73d-f212-46ad-97d7-0b919cb25871.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Terraform will open a web browser to the tokens page for <a target="_blank" href="http://app.terraform.io">app.terraform.io</a>.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685386570419/87d8bb4e-8ebf-46a3-bec3-7a5d74f2ab63.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Enter a <strong>Description</strong> and click <strong>Generate token</strong>.</p>
</li>
<li><p>Copy down the provided token and keep it in a safe place. (You can always generate a new token if you lose the previous one)</p>
</li>
<li><p>Copy and paste the Terraform Cloud token</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685386987183/3dc83777-82e0-4d24-95a4-7a7da9d4c2bb.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Run <code>terraform init</code></p>
</li>
</ol>
<p>This ensures our Terraform code is connected to Terraform cloud, uses Terraform cloud workspaces and stores our state file remotely.</p>
<h2 id="heading-terraform-code">Terraform Code</h2>
<p>Create the following terraform files</p>
<p><code>backend.tf</code></p>
<pre><code class="lang-yaml"><span class="hljs-string">terraform</span> {
  <span class="hljs-string">cloud</span> {
    <span class="hljs-string">organization</span> <span class="hljs-string">=</span> <span class="hljs-string">" YOUR ORGANIZATION NAME "</span>

    <span class="hljs-string">workspaces</span> {
      <span class="hljs-string">name</span> <span class="hljs-string">=</span> <span class="hljs-string">" YOUR WORKSPACES NAME "</span>
    }
  }
}
</code></pre>
<p><code>provider.tf</code></p>
<pre><code class="lang-yaml"><span class="hljs-string">terraform</span> {
  <span class="hljs-string">required_providers</span> {
    <span class="hljs-string">aws</span> <span class="hljs-string">=</span> {
      <span class="hljs-string">source</span>  <span class="hljs-string">=</span> <span class="hljs-string">"hashicorp/aws"</span>
      <span class="hljs-string">version</span> <span class="hljs-string">=</span> <span class="hljs-string">"~&gt; 4.0"</span>
    }
  }
}

<span class="hljs-string">provider</span> <span class="hljs-string">"aws"</span> {
  <span class="hljs-string">region</span> <span class="hljs-string">=</span> <span class="hljs-string">"us-east-1"</span>
}
</code></pre>
<p><code>variables.tf</code></p>
<pre><code class="lang-yaml"><span class="hljs-string">variable</span> <span class="hljs-string">"instance_type"</span> {
  <span class="hljs-string">type</span>    <span class="hljs-string">=</span> <span class="hljs-string">string</span>
  <span class="hljs-string">default</span> <span class="hljs-string">=</span> <span class="hljs-string">"t2.micro"</span>
}
<span class="hljs-string">variable</span> <span class="hljs-string">"monitoring"</span> {
  <span class="hljs-string">default</span> <span class="hljs-string">=</span> <span class="hljs-literal">true</span>
}

<span class="hljs-string">variable</span> <span class="hljs-string">"tags"</span> {
  <span class="hljs-string">type</span> <span class="hljs-string">=</span> <span class="hljs-string">map(any)</span>
  <span class="hljs-string">default</span> <span class="hljs-string">=</span> {
    <span class="hljs-string">Terraform</span>   <span class="hljs-string">=</span> <span class="hljs-string">"true"</span>
    <span class="hljs-string">Environment</span> <span class="hljs-string">=</span> <span class="hljs-string">"dev"</span>
  }
}

<span class="hljs-string">data</span> <span class="hljs-string">"aws_vpc"</span> <span class="hljs-string">"selected"</span> {
  <span class="hljs-string">default</span> <span class="hljs-string">=</span> <span class="hljs-literal">true</span>
}

<span class="hljs-string">data</span> <span class="hljs-string">"aws_security_groups"</span> <span class="hljs-string">"default"</span> {

  <span class="hljs-string">filter</span> {
    <span class="hljs-string">name</span>   <span class="hljs-string">=</span> <span class="hljs-string">"group-name"</span>
    <span class="hljs-string">values</span> <span class="hljs-string">=</span> [<span class="hljs-string">"default"</span>]
  }
}
</code></pre>
<p><code>ec2.tf</code></p>
<pre><code class="lang-yaml"><span class="hljs-string">module</span> <span class="hljs-string">"ec2_instance"</span> {
  <span class="hljs-string">source</span> <span class="hljs-string">=</span> <span class="hljs-string">"terraform-aws-modules/ec2-instance/aws"</span>

  <span class="hljs-string">for_each</span> <span class="hljs-string">=</span> <span class="hljs-string">toset(</span>[<span class="hljs-string">"one"</span>, <span class="hljs-string">"two"</span>, <span class="hljs-string">"three"</span>]<span class="hljs-string">)</span>

  <span class="hljs-string">name</span> <span class="hljs-string">=</span> <span class="hljs-string">"instance-${each.key}"</span>

  <span class="hljs-string">instance_type</span>          <span class="hljs-string">=</span> <span class="hljs-string">var.instance_type</span>
  <span class="hljs-string">monitoring</span>             <span class="hljs-string">=</span> <span class="hljs-string">var.monitoring</span>
  <span class="hljs-string">vpc_security_group_ids</span> <span class="hljs-string">=</span> <span class="hljs-string">data.aws_security_groups.default.ids</span>

  <span class="hljs-string">tags</span> <span class="hljs-string">=</span> <span class="hljs-string">var.tags</span>
}
</code></pre>
<h2 id="heading-create-security-group"><strong>Create Security Group</strong></h2>
<p>Create the <strong>Jenkins</strong> Security Group with the following inbound rules:</p>
<ul>
<li><strong>Type</strong>: HTTP<br />  <strong>Port range</strong>: 8080<br />  <strong>Source</strong>: My IP</li>
</ul>
<p>Outbound Rules:</p>
<ul>
<li><strong>Type</strong>: All traffic<br />  <strong>Port range</strong>: All<br />  <strong>Destination:</strong> 0.0.0.0/0</li>
</ul>
<h2 id="heading-jenkins-configuration">Jenkins Configuration</h2>
<ol>
<li><p>Enter [Public IP]:8080 or [Public DNS]:8080 in your browser to see Jenkins setup.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685471830495/9cb93a64-3761-4a6c-b447-34a6cbe909d3.png" alt class="image--center mx-auto" /></p>
<ol>
<li><p>retrieve your administrative password by running <code>sudo cat /var/lib/jenkins/secrets/initialAdminPassword</code></p>
</li>
<li><p>Copy and then paste the result into the Unlock Jenkins field and click <strong>Continue</strong>.</p>
</li>
<li><p>Click <strong>Install suggested plugins</strong>.</p>
</li>
<li><p>Enter the required information. <strong>Save</strong> and continue to Jenkins.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685476075082/ed2c4d35-eac9-459d-9684-a6ea824935a9.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Click <strong>Manage Plugins</strong> &gt; Select <strong>Available</strong> &gt; Search <strong>Pipeline: AWS Steps</strong>, <strong>Terraform</strong> and <strong>install without restart</strong>.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685476525488/5dc9483e-02b4-4ec6-9249-ca9b6bef9140.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
</li>
<li><p>Click <strong>Manage Jenkins &gt; Global Tool Configuration</strong> then scroll to <strong>Terraform and click Add Terraform</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685476795803/3baebc25-65f7-4c7c-b310-2cf4f46487ef.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Enter any name of your choice and select Terraform version <code>Terraform 30504 linux (386)</code> and click save</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685477238344/497b3f14-5435-4008-9bc5-f2732d91295d.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-terraform-cloud-credentials"><strong>Terraform Cloud Credentials</strong></h3>
<ol>
<li><p>To get the Terraform Cloud credentials, in the terminal run <code>cat /home/&lt;YOUR-USERNAME&gt;/.terraform.d/credentials.tfrc.json</code> for Linux or open the the file <code>credentials.tfrc.json</code> with the path <code>C:\Users\&lt;YOUR-USERNAME&gt;\AppData\Roaming\terraform.d\credentials.tfrc.json</code> in windows.</p>
</li>
<li><p>Copy the output. Create a local txt file and paste the output and save.</p>
</li>
<li><p>Go back to Jenkins and <strong>add a new credential</strong>.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685482388544/91d0538b-099b-4064-9f4c-d016ac392e86.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
</li>
</ol>
<ul>
<li><p><strong>Kind</strong> = Secret file</p>
</li>
<li><p><strong>File</strong> = (Choose the file you just saved)</p>
</li>
<li><p><strong>ID</strong> = tf-creds</p>
</li>
<li><p><strong>Description</strong> = terraform cloud credentials</p>
</li>
</ul>
<p>    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685546778972/360ea543-f0e5-435d-9c42-675315800768.png" alt class="image--center mx-auto" /></p>
<ol>
<li>Click <strong>Create</strong></li>
</ol>
<h3 id="heading-github-webhooks">GitHub Webhooks</h3>
<ol>
<li><p>Go to the project GitHub repo and click <strong>Settings</strong>.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685483136057/9435a96c-597f-45ab-b4ff-2d2c2d135271.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Click <strong>Webhooks then Add Webhook</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685483325760/16fc42ed-2463-4b58-a82b-cbff046101fa.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Enter the following:</p>
<p> <strong>Payload URL</strong> = [jenkins url]/github-webhook/<br /> Please Note: Ensure the <code>/</code> is at the end.<br /> <strong>Content type</strong> = application/json</p>
<p> <strong>Which events would you like to trigger this webhook?</strong> = Just the push event</p>
</li>
<li><p>Click <strong>Add WebHook</strong></p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685483580792/23443cfe-780f-414e-93b4-aa2c6e68076f.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-create-jenkins-pipeline"><strong>Create Jenkins Pipeline</strong></h3>
<ol>
<li><p><strong>Go to the Jenkins Dashboard and click New Item.</strong></p>
</li>
<li><p><strong>Name the pipeline</strong> <code>terraform-jenkins</code> <strong>and select Pipeline. Then click OK.</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685485787561/60e5e1c8-c3e4-4394-8fa7-2456cca9d093.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Configure the pipeline<br /> 1. Build Triggers = GitHub hook trigger for GITScm polling</strong></p>
<p> <strong>2. Pipeline Definition = Pipeline script from SCM</strong></p>
<p> <strong>3. SCM = Git</strong></p>
<p> <strong>4. Repository HTTPS URL = [repo name].git</strong></p>
<p> <strong>5. Branches to build = */main</strong></p>
<p> <strong>6. Script Path = jenkinsfile</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685501661597/a4e73346-7c0c-490d-af6a-7a89cf8b4166.png" alt class="image--center mx-auto" /></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685501703501/128dada0-4c9b-42a5-bc89-2228b397c668.png" alt class="image--center mx-auto" /></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685501734421/29d2478d-420d-4b98-b81a-69a6cfa496bc.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
<h3 id="heading-jenkinsfile"><strong>JenkinsFile</strong></h3>
<p><strong>Jenkins requires a</strong> <code>Jenkinsfile</code> <strong>in the root of our code directory to create a Jenkins Pipeline.</strong></p>
<pre><code class="lang-yaml"><span class="hljs-string">pipeline</span> {
  <span class="hljs-string">agent</span> <span class="hljs-string">any</span>
  <span class="hljs-string">environment</span> {
    <span class="hljs-string">TF_IN_AUTOMATION</span> <span class="hljs-string">=</span> <span class="hljs-string">'true'</span>
    <span class="hljs-string">TF_CLI_CONFIG_FILE</span> <span class="hljs-string">=</span> <span class="hljs-string">credentials('tf-creds')</span>
  }
<span class="hljs-string">tools</span> {
  <span class="hljs-string">terraform</span> <span class="hljs-string">'terraform'</span>
}

<span class="hljs-string">stages</span> {
    <span class="hljs-string">stage('Init')</span> {
      <span class="hljs-string">steps</span> {
        <span class="hljs-string">sh</span> <span class="hljs-string">'terraform init -no-color'</span>
      }
    }
    <span class="hljs-string">stage('Plan')</span> {
      <span class="hljs-string">steps</span> {
        <span class="hljs-string">sh</span> <span class="hljs-string">'terraform plan -no-color'</span>
      }
    }
    <span class="hljs-string">stage('Apply')</span> {
      <span class="hljs-string">steps</span> {
        <span class="hljs-string">sh</span> <span class="hljs-string">'terraform apply -auto-approve -no-color'</span>
      }
    }
}
}
</code></pre>
<p>Push your code to GitHub and the build process will be triggered because of the GitHub webhook we created earlier.</p>
<p>The image below shows a successful build along with the instances running in AWS<strong>.</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685502054145/e355a9c7-1cda-470a-aea3-0eb9a6af2175.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685502149261/0213e315-e1d2-4aa6-9a50-8bef0f1a4315.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-destroy-the-infrastructure-using-jenkins-build-parameters"><strong>Destroy the Infrastructure using Jenkins Build Parameters</strong></h2>
<p>Our intention with build parameters is to provide an option for the users to select which “Action” they wish to perform. Depending on the selection, the pipeline will either run the “apply” command or “destroy” the infrastructure.</p>
<ol>
<li><p>Go to: <strong>Dashboard &gt;terraform-jenkins&gt; Configuration.</strong> In the general section, click on the checkbox stating, <strong>“This project is parameterized.”</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685502851340/3d08b6d0-0dc6-4f1f-94bc-113e20210a32.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Select the <strong>Choice Parameter</strong> and fill in the form as shown below. The name represents the “variable” name we will use in our pipeline script to replace the variable with the selected value.</p>
<p> Each choice is listed on the next line; here, we have two choices – apply and destroy. These are potential values.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685503284092/4919776e-d4d4-4ad5-87d9-addb72d9a254.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Edit the</strong> <code>jenkinsfile</code> <strong>to use the action variable in the “ Apply” stage.</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685504440344/964d5a8a-59ac-44ad-a100-859d449aee61.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Click on Build with Parameters</strong> then Select <strong>destroy</strong> and click on <strong>Build</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685503439948/d747064d-a73a-4602-93de-e24e18683386.png" alt class="image--center mx-auto" /></p>
<p> The image below shows the resources being destroyed along with confirmation of the instances terminated in AWS.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685504099572/4dcd5f49-5421-4d1e-bf19-4c5761347239.png" alt class="image--center mx-auto" /></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685504122866/c8216af7-bfc2-4356-805a-b702615ce5f7.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Elastic Kubernetes Service (EKS) Ingress routing with Nginx for Grafana and Prometheus.]]></title><description><![CDATA[In this article, you will learn how to use Ingress-NGINX Controller for Kubernetes to route traffic to Grafana and Prometheus respectively. In Kubernetes…]]></description><link>https://blog.tafaribeckford.com/kubernetes-ingress-with-nginx-to-route-kube-prometheus-stack</link><guid isPermaLink="true">https://blog.tafaribeckford.com/kubernetes-ingress-with-nginx-to-route-kube-prometheus-stack</guid><category><![CDATA[AWS]]></category><category><![CDATA[Kubernetes]]></category><category><![CDATA[nginx ingress]]></category><category><![CDATA[ingress]]></category><category><![CDATA[Grafana]]></category><dc:creator><![CDATA[Tafari Beckford]]></dc:creator><pubDate>Sat, 27 May 2023 20:33:57 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1685306396486/d8360bb5-0f93-4bee-b40e-2f4607620d8d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, you will learn how to use Ingress-NGINX Controller for Kubernetes to route traffic to Grafana and Prometheus respectively.</p>
<h2 id="heading-what-is-an-ingress">What is an Ingress?</h2>
<p>In Kubernetes, an Ingress is an API object that manages external access to services within a cluster. It provides a way to route incoming traffic to different services based on the requested host or path. Essentially, an Ingress acts as a traffic controller for incoming requests.</p>
<p>An Ingress resource defines a set of rules that determine how inbound connections should be routed to the appropriate backend services. It allows you to configure rules for HTTP and HTTPS traffic, including specifying hostnames, paths, and backend services.</p>
<h2 id="heading-ingress-vs-load-balancer">Ingress vs Load Balancer</h2>
<p>A Load Balancer is responsible for distributing traffic at the network or transport layer, while Ingress provides routing and load balancing capabilities at the application layer. Load Balancer is typically provided as an external service by cloud providers, while Ingress is an API object in Kubernetes that requires an Ingress controller to function. Both are important for managing external access to services in a Kubernetes cluster, but they serve different purposes and operate at different layers of the network stack.</p>
<h3 id="heading-pre-requisites">Pre-requisites</h3>
<ul>
<li><p>AWS Account.</p>
</li>
<li><p>An EKS(Elastic Kubernetes Service) cluster.</p>
</li>
<li><p>A Kubectl installation with your Kubernetes cluster from the step above is configured as the primary cluster.</p>
</li>
<li><p>The Helm CLI.</p>
</li>
</ul>
<h2 id="heading-1-install-and-configure-nginx-ingress-controller">1. Install <strong>and Configure Nginx Ingress Controller</strong></h2>
<p>Install Nginx Ingress in your cluster using Helm, add the <code>ingress-nginx</code> repository to Helm, then run <code>helm repo update</code> . After we have added the repository we can deploy using the chart <code>ingress-nginx/ingress-nginx</code> .</p>
<pre><code class="lang-bash">helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
</code></pre>
<p>Override helm chart default values, and create a file called <code>nginx-values.yaml</code> in a directory called <code>nginx</code> with the following.</p>
<pre><code class="lang-yaml"><span class="hljs-attr">controller:</span>
  <span class="hljs-attr">setAsDefaultIngress:</span> <span class="hljs-literal">true</span>
  <span class="hljs-attr">service:</span>
    <span class="hljs-attr">annotations:</span>
      <span class="hljs-attr">service.beta.kubernetes.io/aws-load-balancer-backend-protocol:</span> <span class="hljs-string">tcp</span>
      <span class="hljs-attr">service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled:</span> <span class="hljs-string">'true'</span>
      <span class="hljs-attr">service.beta.kubernetes.io/aws-load-balancer-type:</span> <span class="hljs-string">nlb</span>
</code></pre>
<ul>
<li><p><a target="_blank" href="http://service.beta.kubernetes.io/aws-load-balancer-backend-protocol"><code>service.beta.kubernetes.io/aws-load-balancer-backend-protocol</code></a><code>: tcp</code> This annotation specifies the backend protocol for the NLB. In this case, it is set to TCP, indicating that the traffic between the NLB and the pods behind it should use the TCP protocol.</p>
</li>
<li><p><a target="_blank" href="http://service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled"><code>service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled</code></a><code>: 'true'</code> This annotation enables cross-zone load balancing for the NLB. When set to <code>true</code>, the NLB can distribute traffic evenly across all available zones in the specified target group, even if the pods or instances are located in different availability zones.</p>
</li>
<li><p><a target="_blank" href="http://service.beta.kubernetes.io/aws-load-balancer-type"><code>service.beta.kubernetes.io/aws-load-balancer-type</code></a><code>: nlb</code> This annotation sets the type of load balancer to be created as an NLB. An NLB is a Layer 4 (TCP/UDP) load balancer provided by AWS that operates at the network level and can handle large-scale traffic.  </p>
</li>
</ul>
<p>    The following command installs the nginx chart with the release name <code>ingress-nginx</code>:</p>
<pre><code class="lang-bash">helm install ingress-nginx ingress-nginx/ingress-nginx -f nginx/nginx-values.yaml
</code></pre>
<h3 id="heading-1a-validate-nginx-is-running">1a. Validate NGINX is running</h3>
<pre><code class="lang-yaml"><span class="hljs-string">kubectl</span> <span class="hljs-string">get</span> <span class="hljs-string">pods</span>
</code></pre>
<p>Output:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685236813095/3a85c0d7-2f01-4f18-bc22-f7e3413c7183.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-yaml"><span class="hljs-string">kubectl</span> <span class="hljs-string">get</span> <span class="hljs-string">services</span>
</code></pre>
<p>Output:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685237216532/1e4ef366-8706-4d7d-8953-56c19db95a0f.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-2-install-and-configure-kube-prometheus-stack">2. Install and Configure Kube-Prometheus-Stack</h2>
<p>kube-Prometheus-stack collects Kubernetes manifests, Grafana dashboards, and Prometheus rules combined with documentation and scripts to provide easy-to-operate end-to-end Kubernetes cluster monitoring with Prometheus using the Prometheus Operator.</p>
<p>Install kube-prometheus-stack in your cluster using Helm, add the <code>prometheus-community</code> repository to Helm, then run <code>helm repo update</code> . After we have added the repository we can deploy using the chart <code>prometheus-community/kube-prometheus-stack</code> .</p>
<pre><code class="lang-yaml"><span class="hljs-string">helm</span> <span class="hljs-string">repo</span> <span class="hljs-string">add</span> <span class="hljs-string">prometheus-community</span> <span class="hljs-string">https://prometheus-community.github.io/helm-charts</span>
<span class="hljs-string">helm</span> <span class="hljs-string">repo</span> <span class="hljs-string">update</span>
</code></pre>
<p>The following command installs the kube-prometheus-stack chart with the release name <code>kube</code>:</p>
<pre><code class="lang-yaml"><span class="hljs-string">helm</span> <span class="hljs-string">install</span> <span class="hljs-string">kube</span> <span class="hljs-string">prometheus-community/kube-prometheus-stack</span>
</code></pre>
<h3 id="heading-2a-validate-kube-prometheus-stack-is-running">2a. Validate kube-prometheus-stack is running</h3>
<pre><code class="lang-yaml"><span class="hljs-string">kubectl</span> <span class="hljs-string">get</span> <span class="hljs-string">pods</span> <span class="hljs-string">-l</span> <span class="hljs-string">release=kube</span>
</code></pre>
<p>Output:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685245423731/01ba1048-b482-4b25-9f10-bdb7c6177c5a.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-yaml"><span class="hljs-string">kubectl</span> <span class="hljs-string">get</span> <span class="hljs-string">services</span> <span class="hljs-string">-l</span> <span class="hljs-string">release=kube</span>
</code></pre>
<p>Output:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685245647747/308c0bb6-0479-447d-95aa-42bc503f3177.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-3-creating-a-kubernetes-ingress">3. Creating a Kubernetes Ingress</h2>
<p>Now that the ingress controller and kube-prometheus-stack are running in the cluster, we can expose the services using ingress.</p>
<p>The annotation <a target="_blank" href="http://kubernetes.io/ingress.class"><code>kubernetes.io/ingress.class</code></a><code>: nginx</code> is used to specify the Ingress controller class to be used for a Kubernetes Ingress resource.</p>
<p>Create an ingress named <code>ingress.yaml</code> with the following:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">apiVersion:</span> <span class="hljs-string">networking.k8s.io/v1</span>
<span class="hljs-attr">kind:</span> <span class="hljs-string">Ingress</span>
<span class="hljs-attr">metadata:</span>
  <span class="hljs-attr">namespace:</span> <span class="hljs-string">default</span>
  <span class="hljs-attr">annotations:</span> 
  <span class="hljs-attr">kubernetes.io/ingress.class:</span> <span class="hljs-string">nginx</span>
  <span class="hljs-attr">name:</span> <span class="hljs-string">kube-stack-ingress</span>
  <span class="hljs-attr">labels:</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">kube-stack-ingress</span>

<span class="hljs-attr">spec:</span>
  <span class="hljs-attr">rules:</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">host:</span> <span class="hljs-string">grafana.tafaribeckford.com</span> <span class="hljs-comment">#Use your own domain</span>
    <span class="hljs-attr">http:</span>
      <span class="hljs-attr">paths:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">pathType:</span> <span class="hljs-string">Prefix</span>
        <span class="hljs-attr">path:</span> <span class="hljs-string">"/"</span>
        <span class="hljs-attr">backend:</span>
          <span class="hljs-attr">service:</span>
            <span class="hljs-attr">name:</span> <span class="hljs-string">kube-grafana</span> 
            <span class="hljs-attr">port:</span> 
              <span class="hljs-attr">number:</span> <span class="hljs-number">80</span>

  <span class="hljs-bullet">-</span> <span class="hljs-attr">host:</span> <span class="hljs-string">prom.tafaribeckford.com</span> <span class="hljs-comment">#Use your own domain</span>
    <span class="hljs-attr">http:</span>
      <span class="hljs-attr">paths:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">pathType:</span> <span class="hljs-string">Prefix</span>
        <span class="hljs-attr">path:</span> <span class="hljs-string">"/"</span>
        <span class="hljs-attr">backend:</span>
          <span class="hljs-attr">service:</span>
            <span class="hljs-attr">name:</span> <span class="hljs-string">kube-kube-prometheus-stack-prometheus</span> 
            <span class="hljs-attr">port:</span> 
              <span class="hljs-attr">number:</span> <span class="hljs-number">9090</span>
</code></pre>
<p>Create the resource</p>
<pre><code class="lang-yaml"><span class="hljs-string">kubectl</span> <span class="hljs-string">apply</span> <span class="hljs-string">-f</span> <span class="hljs-string">ingress.yaml</span>
</code></pre>
<p>Perfect! Let’s check that it’s working</p>
<p>Grafana Dashboard:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685305171965/a5d0d1d3-e39c-4ca5-b865-92789c911e4b.png" alt class="image--center mx-auto" /></p>
<p>Prometheus Dashboard:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685305352577/7a59229a-73a1-4903-b695-b9f69c50e3c0.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-summary">Summary</h2>
<p>A Kubernetes Ingress provides a reliable method for making your services accessible from outside the cluster. It simplifies the management of routing rules by consolidating them into a single resource and offers flexible configuration options for these rules.</p>
]]></content:encoded></item><item><title><![CDATA[Create Jenkins AMI using Packer and Ansible Playbook.]]></title><description><![CDATA[In this article, you will learn how to create a Jenkins AMI using Hashicorp Packer and Ansible Playbook. Packer is an open-source tool for creating…]]></description><link>https://blog.tafaribeckford.com/create-jenkins-ami-using-packer-and-ansible-playbook</link><guid isPermaLink="true">https://blog.tafaribeckford.com/create-jenkins-ami-using-packer-and-ansible-playbook</guid><category><![CDATA[Devops]]></category><category><![CDATA[hashicorp packer]]></category><category><![CDATA[Jenkins]]></category><category><![CDATA[AWS]]></category><category><![CDATA[ansible]]></category><dc:creator><![CDATA[Tafari Beckford]]></dc:creator><pubDate>Fri, 19 May 2023 01:27:30 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1685308173826/58a9cdc5-75a5-4dea-9b10-8479b7cf446f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, you will learn how to create a Jenkins AMI using Hashicorp Packer and Ansible Playbook.</p>
<h2 id="heading-what-is-packer">What is Packer?</h2>
<p>Packer is an open-source tool for creating identical machine images for multiple platforms from a single source configuration. Packer is lightweight, runs on every major operating system, and is highly performant, creating machine images for multiple platforms in parallel.</p>
<h2 id="heading-what-is-ansible">What is Ansible?</h2>
<p>Ansible is an open-source automation tool designed for IT infrastructure provisioning, configuration management, and application deployment. It allows you to automate repetitive tasks, orchestrate complex workflows, and manage infrastructure as code.</p>
<p>At its core, Ansible uses a simple and human-readable language called YAML (Yet Another Markup Language) to define automation tasks and configuration files. These files, called "playbooks," describe the desired state of a system and the steps needed to achieve that state.</p>
<h2 id="heading-prerequisites">Prerequisites:</h2>
<ul>
<li><p>AWS Account</p>
</li>
<li><p>Packer</p>
</li>
</ul>
<p>Ansible</p>
<p>Notes:</p>
<ul>
<li><p>I will be using the default VPC in my AWS Account. If you're using a custom VPC configure it accordingly to ensure your network is correct.</p>
</li>
<li><p>Red Hat Enterprise Linux (RHEL) will be used as the source AMI for Packer.</p>
</li>
<li><p>The code for the project can be found on my <a target="_blank" href="https://github.com/TafariBeckford/Jenkins-Ansible.git">Github</a>.</p>
</li>
</ul>
<h2 id="heading-aws">AWS</h2>
<h3 id="heading-create-security-group">Create Security Group</h3>
<p>Create the <strong>Jenkins</strong> Security Group with the following inbound rules:</p>
<ul>
<li><p><strong>Type</strong>: HTTP<br />  <strong>Port range</strong>: 8080<br />  <strong>Source</strong>: My IP</p>
</li>
<li><p><strong>Type</strong>: SSH<br />  <strong>Port range</strong>: 22<br />  <strong>Source</strong>: My IP</p>
</li>
</ul>
<h2 id="heading-ansible">Ansible</h2>
<h3 id="heading-create-playbook-in-a-folder-named-ansible">Create playbook in a folder named "ansible"</h3>
<p>This playbook sets up the Jenkins repository, installs OpenJDK 11 JRE, and installs Jenkins on the specified hosts using the yum package manager</p>
<ol>
<li><code>jenkins-playbook.yaml</code></li>
</ol>
<pre><code class="lang-yaml"><span class="hljs-meta">---</span>
<span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Install</span> <span class="hljs-string">Jenkins</span>
  <span class="hljs-attr">hosts:</span> <span class="hljs-string">all</span>
  <span class="hljs-attr">become:</span> <span class="hljs-literal">true</span>

  <span class="hljs-attr">tasks:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Add</span> <span class="hljs-string">Jenkins</span> <span class="hljs-string">repository</span>
      <span class="hljs-attr">yum_repository:</span>
        <span class="hljs-attr">name:</span> <span class="hljs-string">jenkins</span>
        <span class="hljs-attr">description:</span> <span class="hljs-string">Jenkins</span> <span class="hljs-string">Repository</span>
        <span class="hljs-attr">baseurl:</span> <span class="hljs-string">https://pkg.jenkins.io/redhat-stable</span>
        <span class="hljs-attr">gpgcheck:</span> <span class="hljs-literal">yes</span>
        <span class="hljs-attr">gpgkey:</span> <span class="hljs-string">https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key</span>
        <span class="hljs-attr">enabled:</span> <span class="hljs-literal">yes</span>

    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Download</span> <span class="hljs-string">Jenkins</span> <span class="hljs-string">repository</span> <span class="hljs-string">key</span>
      <span class="hljs-attr">get_url:</span>
        <span class="hljs-attr">url:</span> <span class="hljs-string">https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key</span>
        <span class="hljs-attr">dest:</span> <span class="hljs-string">/etc/pki/rpm-gpg/jenkins.io-2023.key</span>


    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Install</span> <span class="hljs-string">OpenJDK</span> <span class="hljs-number">11</span> <span class="hljs-string">JRE</span>
      <span class="hljs-attr">ansible.builtin.yum:</span>
        <span class="hljs-attr">name:</span> 
         <span class="hljs-bullet">-</span> <span class="hljs-string">fontconfig</span> 
         <span class="hljs-bullet">-</span> <span class="hljs-string">java-11-openjdk-devel</span>
        <span class="hljs-attr">state:</span> <span class="hljs-string">present</span>
        <span class="hljs-attr">update_cache:</span> <span class="hljs-literal">yes</span>

    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Install</span> <span class="hljs-string">Jenkins</span>
      <span class="hljs-attr">ansible.builtin.yum:</span>
        <span class="hljs-attr">name:</span> <span class="hljs-string">jenkins</span>
        <span class="hljs-attr">state:</span> <span class="hljs-string">latest</span>
        <span class="hljs-attr">update_cache:</span> <span class="hljs-literal">yes</span>
</code></pre>
<h2 id="heading-bash-script">Bash Script</h2>
<p>Create a <code>scripts</code> directory with the following <code>jenkins-start.sh</code> script:</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/bin/bash</span>
sudo systemctl <span class="hljs-built_in">enable</span> jenkins
sudo systemctl start jenkins
</code></pre>
<h2 id="heading-packer">Packer</h2>
<h3 id="heading-create-packer-files-in-a-packer-directory">Create packer files in a <code>packer</code> directory</h3>
<ol>
<li><code>variables.pkr.hcl</code></li>
</ol>
<pre><code class="lang-ini">variable "ami_name" {
  <span class="hljs-attr">default</span> = <span class="hljs-string">"Jenkins-AMI"</span>
}

variable "source_ami" {
  <span class="hljs-attr">default</span> = <span class="hljs-string">"ami-026ebd4cfe2c043b2"</span>
}

variable "instance_type" {
  <span class="hljs-attr">default</span> = <span class="hljs-string">"t3.medium"</span>
}

variable "region" {
  <span class="hljs-attr">default</span> = <span class="hljs-string">"us-east-1"</span>
}

variable "ssh_username" {
  <span class="hljs-attr">default</span> = <span class="hljs-string">"ec2-user"</span>
}

variable "security_group_id" {
  <span class="hljs-attr">default</span> = <span class="hljs-string">"sg-0022ddab3a95e94fc"</span>
}
</code></pre>
<ol>
<li><code>jenkins.pkr.hcl</code></li>
</ol>
<pre><code class="lang-ini">source "amazon-ebs" "jenkins" {
  <span class="hljs-attr">ami_name</span>          = var.ami_name
  <span class="hljs-attr">instance_type</span>     = var.instance_type
  <span class="hljs-attr">region</span>            = var.region
  <span class="hljs-attr">source_ami</span>        = var.source_ami
  <span class="hljs-attr">ssh_username</span>      = var.ssh_username
  <span class="hljs-attr">security_group_id</span> = var.security_group_id

  <span class="hljs-attr">tags</span> = {
    <span class="hljs-attr">"Name"</span> = <span class="hljs-string">"Jenkins-Ansible"</span>
  }
}

build {

  <span class="hljs-attr">sources</span> = [<span class="hljs-string">"source.amazon-ebs.jenkins"</span>]

  provisioner "ansible" {

    <span class="hljs-attr">playbook_file</span> = <span class="hljs-string">"../playbook/jenkins-playbook.yaml"</span>

  }

  provisioner "file" {
    <span class="hljs-attr">source</span> = <span class="hljs-string">"../scripts/jenkins-start.sh"</span>

    <span class="hljs-attr">destination</span> = <span class="hljs-string">"/tmp/jenkins-start.sh"</span>

  }

  provisioner "shell" {

    <span class="hljs-attr">inline</span> = [
      <span class="hljs-string">"sudo chmod +x /tmp/jenkins-start.sh"</span>,
      <span class="hljs-string">"sudo mv /tmp/jenkins-start.sh   /var/lib/cloud/scripts/per-instance/"</span>,
    ]

  }

  post-processor "manifest" {}

}
</code></pre>
<p>The file provisioner uploads files to machines built by Packer. The recommended usage of the file provisioner is to use it to upload files, and then use the shell provisioner to move them to the proper place, set permissions, etc.</p>
<p>The script is then made executable followed by placing them in the directories under <code>/var/lib/cloud/scripts/per-instance</code>. Scripts within this directory are run when a new instance is first booted.</p>
<ol>
<li><p>Validate Packer files using the command <code>packer validate .</code> with validates both files simultaneously.</p>
</li>
<li><p>Create AMI using the command <code>packer build .</code></p>
</li>
</ol>
<p>The image below shows the ongoing build process:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685634553480/18462ef0-75a4-4ffe-b62c-a2e3172dfdc7.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-completion">Completion</h2>
<p>Upon completion of the build process, your AMI will be stored in your AWS account along with a snapshot.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685634726705/10e63dbb-9c05-419b-afec-a7173a0eeb48.png" alt class="image--center mx-auto" /></p>
<p>Congrats you have created a Jenkins AMI !!!!!!!!</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExYmY3ZWFhY2Q3MTRlNTExOTUzYTVhOGEwMjk3MWRiNzU2MDc5ODFkNyZlcD12MV9pbnRlcm5hbF9naWZzX2dpZklkJmN0PWc/OqFvTdkCiDtAoDdq6p/giphy.gif">https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExYmY3ZWFhY2Q3MTRlNTExOTUzYTVhOGEwMjk3MWRiNzU2MDc5ODFkNyZlcD12MV9pbnRlcm5hbF9naWZzX2dpZklkJmN0PWc/OqFvTdkCiDtAoDdq6p/giphy.gif</a></div>
]]></content:encoded></item><item><title><![CDATA[Securing AWS credentials with AWS-Vault]]></title><description><![CDATA[Protecting your AWS account access keys is critical for protecting your company's cloud infrastructure. Managing several accounts, on the other hand, can…]]></description><link>https://blog.tafaribeckford.com/securing-aws-credentials-with-aws-vault</link><guid isPermaLink="true">https://blog.tafaribeckford.com/securing-aws-credentials-with-aws-vault</guid><category><![CDATA[Devops]]></category><category><![CDATA[AWS]]></category><dc:creator><![CDATA[Tafari Beckford]]></dc:creator><pubDate>Wed, 17 May 2023 02:44:01 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1685065257317/5b1eb144-e7d2-4347-a10d-a74f63b365d5.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Protecting your AWS account access keys is critical for protecting your company's cloud infrastructure. Managing several accounts, on the other hand, can be complicated and time-consuming. aws-vault secures the storage and management of access keys for many AWS accounts.</p>
<h2 id="heading-what-is-aws-vault">What is AWS-Vault?</h2>
<p>AWS Vault is a tool to securely store and access AWS credentials in a development environment.</p>
<p>AWS Vault stores IAM credentials in your operating system's secure keystore and then generates temporary credentials from those to expose to your shell and applications. It's designed to be complementary to the AWS CLI tools and is aware of your profiles and configuration in <code>~/.aws/config</code>.</p>
<p>The AWS-vault program generates temporary credentials using Amazon's STS service via the GetSessionToken or AssumeRole API calls. Because these temporary credentials expire in a short period, the risk of credentials leaking is decreased.</p>
<h2 id="heading-the-problem">The Problem</h2>
<p>The <code>~/.aws/credentials</code> file is typically located in the home directory of a user on a Unix-like system (such as Linux or macOS) or at <code>C:\Users\USERNAME\.aws\credentials</code> on Windows and is used by the AWS Command Line Interface (CLI). The file contains sensitive information, including AWS access keys and secret access keys, which provide access to your AWS resources. Personally, for me, I have never been the biggest supporter of <code>~/.aws/credentials</code> because of its vulnerability to potentially malicious actors.</p>
<h2 id="heading-solution">Solution</h2>
<h3 id="heading-install-aws-vault">Install AWS-Vault</h3>
<ul>
<li><p>By downloading the latest <a target="_blank" href="https://github.com/99designs/aws-vault/releases">release</a></p>
</li>
<li><p>on Windows with <a target="_blank" href="https://chocolatey.org/packages/aws-vault">Chocolatey</a>: <code>choco install aws-vault</code></p>
</li>
<li><p>on macOS with <a target="_blank" href="https://formulae.brew.sh/cask/aws-vault">Homebrew Cask</a>: <code>brew install --cask aws-vault</code></p>
</li>
<li><p>on Linux with <a target="_blank" href="https://formulae.brew.sh/formula/aws-vault">Homebrew on Linux</a>: <code>brew install aws-vault</code></p>
</li>
</ul>
<h3 id="heading-aws-vault-backend">AWS-Vault Backend</h3>
<p>The backends in which credentials are stored include:</p>
<ul>
<li><p><a target="_blank" href="https://support.apple.com/en-au/guide/keychain-access/welcome/mac">macOS Keychain</a></p>
</li>
<li><p><a target="_blank" href="https://support.microsoft.com/en-au/help/4026814/windows-accessing-credential-manager">Windows Credential Manager</a></p>
</li>
<li><p>Secret Service (<a target="_blank" href="https://wiki.gnome.org/Projects/GnomeKeyring">Gnome Keyring</a>, <a target="_blank" href="https://kde.org/applications/system/org.kde.kwalletmanager5">KWallet</a>)</p>
</li>
<li><p><a target="_blank" href="https://kde.org/applications/system/org.kde.kwalletmanager5">KWallet</a></p>
</li>
<li><p><a target="_blank" href="https://www.passwordstore.org/">Pass</a></p>
</li>
<li><p>Encrypted file</p>
</li>
</ul>
<p>NB - Gnome Keyring and KWallet can be used on Windows when using WSL.</p>
<h3 id="heading-configure-aws-vault">Configure AWS-Vault</h3>
<p>Add AWS region in <code>~/.aws/config</code> before adding a profile.</p>
<pre><code class="lang-yaml">[<span class="hljs-string">default</span>]
<span class="hljs-string">region</span> <span class="hljs-string">=</span> <span class="hljs-string">us-east-1</span>
</code></pre>
<p>Add AWS-Vault profile</p>
<pre><code class="lang-yaml"><span class="hljs-comment"># Store AWS credentials for the "jonsmith" profile</span>
<span class="hljs-string">$</span> <span class="hljs-string">aws-vault</span> <span class="hljs-string">add</span> <span class="hljs-string">jonsmith</span>
<span class="hljs-attr">Enter Access Key Id:</span> <span class="hljs-string">ABDCDEFDASDASF</span>
<span class="hljs-attr">Enter Secret Key:</span> <span class="hljs-string">%%%</span>

<span class="hljs-comment"># Execute a command (using temporary credentials)</span>
<span class="hljs-string">$</span> <span class="hljs-string">aws-vault</span> <span class="hljs-string">exec</span> <span class="hljs-string">jonsmith</span> <span class="hljs-string">--</span> <span class="hljs-string">aws</span> <span class="hljs-string">s3</span> <span class="hljs-string">ls</span>
<span class="hljs-string">bucket_1</span>
<span class="hljs-string">bucket_2</span>

<span class="hljs-comment"># open a browser window and login to the AWS Console</span>
<span class="hljs-string">$</span> <span class="hljs-string">aws-vault</span> <span class="hljs-string">login</span> <span class="hljs-string">jonsmith</span>

<span class="hljs-comment"># List credentials</span>
<span class="hljs-string">$</span> <span class="hljs-string">aws-vault</span> <span class="hljs-string">list</span>
<span class="hljs-string">Profile</span>                  <span class="hljs-string">Credentials</span>              <span class="hljs-string">Sessions</span>
<span class="hljs-string">=======</span>                  <span class="hljs-string">===========</span>              <span class="hljs-string">========</span>
<span class="hljs-string">jonsmith</span>                 <span class="hljs-string">jonsmith</span>                 <span class="hljs-string">sts.GetSessionToken:46m29s</span>

<span class="hljs-comment"># Start a subshell with temporary credentials</span>
<span class="hljs-string">$</span> <span class="hljs-string">aws-vault</span> <span class="hljs-string">exec</span> <span class="hljs-string">jonsmith</span>
<span class="hljs-string">Starting</span> <span class="hljs-string">subshell</span> <span class="hljs-string">/bin/zsh,</span> <span class="hljs-string">use</span> <span class="hljs-string">`exit`</span> <span class="hljs-string">to</span> <span class="hljs-string">exit</span> <span class="hljs-string">the</span> <span class="hljs-string">subshell</span>
<span class="hljs-string">$</span> <span class="hljs-string">aws</span> <span class="hljs-string">s3</span> <span class="hljs-string">ls</span>
<span class="hljs-string">bucket_1</span>
<span class="hljs-string">bucket_2</span>
</code></pre>
<h3 id="heading-aws-vault-with-roles-and-mfa">AWS-Vault with Roles and MFA</h3>
<p>Using roles and multi-factor authentication (MFA) are important best practices when working with AWS to enhance security. As such you would need to create an IAM User and Roles in addition to setting up a MFA device whether virtual or physical.</p>
<p>The diagram below depicts a typical setup of AWS Organization.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1684977354241/aacbbc85-34e3-4f2e-9b63-440898ab04f9.png" alt="AWS Organization" class="image--right mx-auto mr-0" /></p>
<pre><code class="lang-yaml">[<span class="hljs-string">default</span>]
<span class="hljs-string">region</span> <span class="hljs-string">=</span> <span class="hljs-string">us-east-1</span>

[<span class="hljs-string">profile</span> <span class="hljs-string">jonsmith</span>]
<span class="hljs-string">mfa_serial</span> <span class="hljs-string">=</span> <span class="hljs-string">arn:aws:iam::111111111111:mfa/jonsmith</span>
<span class="hljs-string">region</span> <span class="hljs-string">=</span> <span class="hljs-string">us-east-1</span>

[<span class="hljs-string">profile</span> <span class="hljs-string">Development-Role</span>]
<span class="hljs-string">source_profile</span> <span class="hljs-string">=</span> <span class="hljs-string">jonsmith</span>
<span class="hljs-string">region</span> <span class="hljs-string">=</span> <span class="hljs-string">us-east-1</span>
<span class="hljs-string">role_arn</span> <span class="hljs-string">=</span> <span class="hljs-string">arn:aws:iam::22222222222:role/Development</span>
<span class="hljs-string">mfa_serial</span> <span class="hljs-string">=</span> <span class="hljs-string">arn:aws:iam::111111111111:mfa/jonsmith</span>

[<span class="hljs-string">profile</span> <span class="hljs-string">Staging-Role</span>]
<span class="hljs-string">source_profile</span> <span class="hljs-string">=</span> <span class="hljs-string">jonsmith</span>
<span class="hljs-string">region</span> <span class="hljs-string">=</span> <span class="hljs-string">us-east-1</span>
<span class="hljs-string">role_arn</span> <span class="hljs-string">=</span> <span class="hljs-string">arn:aws:iam::22222222222:role/Staging</span>
<span class="hljs-string">mfa_serial</span> <span class="hljs-string">=</span> <span class="hljs-string">arn:aws:iam::111111111111:mfa/jonsmith</span>

[<span class="hljs-string">profile</span> <span class="hljs-string">DevOps-Role</span>]
<span class="hljs-string">source_profile</span> <span class="hljs-string">=</span> <span class="hljs-string">jonsmith</span>
<span class="hljs-string">region</span> <span class="hljs-string">=</span> <span class="hljs-string">us-east-1</span>
<span class="hljs-string">role_arn</span> <span class="hljs-string">=</span> <span class="hljs-string">arn:aws:iam::333333333333:role/DevOps</span>
<span class="hljs-string">mfa_serial</span> <span class="hljs-string">=</span> <span class="hljs-string">arn:aws:iam::111111111111:mfa/jonsmith</span>

[<span class="hljs-string">profile</span> <span class="hljs-string">Security-Role</span>]
<span class="hljs-string">source_profile</span> <span class="hljs-string">=</span> <span class="hljs-string">jonsmith</span>
<span class="hljs-string">region</span> <span class="hljs-string">=</span> <span class="hljs-string">us-east-1</span>
<span class="hljs-string">role_arn</span> <span class="hljs-string">=</span> <span class="hljs-string">arn:aws:iam::333333333333:role/Security</span>
<span class="hljs-string">mfa_serial</span> <span class="hljs-string">=</span> <span class="hljs-string">arn:aws:iam::111111111111:mfa/jonsmith</span>
</code></pre>
<p>Refer to the AWS-Vault official repo for additional info:</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://github.com/99designs/aws-vault">https://github.com/99designs/aws-vault</a></div>
]]></content:encoded></item></channel></rss>