<?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://kunalbhat.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 19:56:45 GMT</lastBuildDate><atom:link href="https://kunalbhat.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Provisioning  AWS Infrastructure & Deployment with Terraform and GitHub Actions]]></title><description><![CDATA[Introduction:
In a previous blog post, we covered the fundamentals of Terraform and Infrastructure as Code (IaC) Check here. Building upon that foundation, this blog will delve into advanced concepts by showcasing a real-world project: Automated AWS ...]]></description><link>https://kunalbhat.hashnode.dev/provisioning-aws-infrastructure-deployment-with-terraform-and-github-actions</link><guid isPermaLink="true">https://kunalbhat.hashnode.dev/provisioning-aws-infrastructure-deployment-with-terraform-and-github-actions</guid><category><![CDATA[AWS]]></category><category><![CDATA[Terraform]]></category><category><![CDATA[GitHub]]></category><category><![CDATA[github-actions]]></category><category><![CDATA[Load Balancing]]></category><category><![CDATA[cicd]]></category><dc:creator><![CDATA[Kunal Bhat]]></dc:creator><pubDate>Tue, 23 Jan 2024 08:36:52 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1705948073743/ee59b8b3-920e-42ee-a9d9-d156eb14a285.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction">Introduction:</h3>
<p>In a previous blog post, we covered the fundamentals of Terraform and Infrastructure as Code (IaC) <a target="_blank" href="https://hashnode.com/post/clp57evco000109l67hra4fgw">Check here</a>. Building upon that foundation, this blog will delve into advanced concepts by showcasing a real-world project: Automated AWS Infrastructure &amp; Deployment. We'll explore how to leverage Terraform for provisioning AWS resources and integrate GitHub Actions for seamless automation.</p>
<h3 id="heading-prerequisites">Prerequisites:</h3>
<p>Before diving into the Automated AWS Infrastructure &amp; Deployment with Terraform and GitHub Actions project, ensure you have the following prerequisites in place:</p>
<ol>
<li><p><strong>AWS Account:</strong></p>
<ul>
<li>Ensure you have an AWS account. If you don't have one, you can create it <a target="_blank" href="https://aws.amazon.com/">here</a>.</li>
</ul>
</li>
<li><p><strong>AWS CLI:</strong></p>
<ul>
<li>Install the AWS Command Line Interface (CLI) on your local machine. You can download it from the <a target="_blank" href="https://aws.amazon.com/cli/">official AWS CLI page</a>.</li>
</ul>
</li>
<li><p><strong>AWS Access Key and Secret Access Key:</strong></p>
<ul>
<li>Set up an AWS Access Key ID and Secret Access Key. These credentials are required for programmatic access to your AWS account. Follow the AWS documentation on <a target="_blank" href="https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys">Managing Access Keys</a> to create and manage these credentials securely.</li>
</ul>
</li>
<li><p><strong>Terraform Installed:</strong></p>
<ul>
<li>Install Terraform on your local machine. You can download Terraform from the <a target="_blank" href="https://www.terraform.io/downloads.html">official Terraform website</a>. Ensure that the Terraform binary is added to your system's <code>PATH</code>.</li>
</ul>
</li>
</ol>
<p>Once you have these prerequisites in place, you'll be ready to explore the project, automate AWS infrastructure, and leverage GitHub Actions.</p>
<h3 id="heading-project-overview">Project Overview:</h3>
<p>The project aims to showcase the automation of AWS infrastructure deployment using Terraform and GitHub Actions. The primary goals include demonstrating a modular, scalable, and highly available architecture for hosting web applications on AWS.</p>
<ul>
<li><p><strong>Infrastructure Definition:</strong> Utilizing Terraform to define and manage the complete AWS infrastructure.</p>
</li>
<li><p><strong>Continuous Deployment:</strong> Implementing GitHub Actions for a streamlined CI/CD pipeline, automating testing and deployment processes.</p>
</li>
</ul>
<h3 id="heading-project-structure">Project Structure:</h3>
<p>The project's architecture employs a modular and automated approach, utilizing Terraform and GitHub Actions to orchestrate the deployment of a scalable and highly available AWS infrastructure. Each component plays a vital role in achieving the project's goals of consistency, efficiency, and scalability.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1705935559063/a7fe4e2b-6ba3-4341-b95d-715b3d2d5c14.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-terraform-workflow">Terraform Workflow:</h3>
<p>In the Automated AWS Infrastructure &amp; Deployment project, the Terraform configurations follow a modular structure to enhance organization, reusability, and maintainability. Each infrastructure component, such as EC2 instances, Virtual Private Cloud (VPC), Application Load Balancer (ALB), and Security Groups (SG), is defined in separate Terraform modules. These modules are then composed in the <a target="_blank" href="http://main.tf"><code>main.tf</code></a> file to create a comprehensive and modular infrastructure. Let's explore the main components:</p>
<h3 id="heading-1-terraform-modules">1<strong>.</strong> Terraform Modules<strong>:</strong></h3>
<h4 id="heading-ec2-module-modulesec2">- EC2 Module (<code>modules/ec2</code>):</h4>
<ul>
<li><p>Defines the configuration for EC2 instances hosting the web application.</p>
</li>
<li><p>Utilizes parameters for customization, allowing flexibility in instance types, AMIs, and other settings.</p>
</li>
</ul>
<h4 id="heading-vpc-module-modulesvpc">- VPC Module (<code>modules/vpc</code>):</h4>
<ul>
<li><p>Configures the Virtual Private Cloud (VPC) with private and public subnets for enhanced network isolation.</p>
</li>
<li><p>Parameters enable customization of CIDR blocks, availability zones, and other VPC settings.</p>
</li>
</ul>
<h4 id="heading-alb-module-modulesalb">- ALB Module (<code>modules/alb</code>):</h4>
<ul>
<li><p>Defines the Application Load Balancer (ALB) to distribute traffic across EC2 instances.</p>
</li>
<li><p>Parameters support customization of listener rules, Target groups and other load balancer settings.</p>
</li>
</ul>
<h4 id="heading-sg-module-modulessg">- SG Module (<code>modules/sg</code>):</h4>
<ul>
<li><p>Configures Security Groups (SG) to control inbound and outbound traffic for resources.</p>
</li>
<li><p>Customizable parameters allow for specific rules, port configurations, and other security settings.</p>
</li>
</ul>
<h3 id="heading-2-main-configuration-maintfhttpmaintf">2. Main Configuration (<a target="_blank" href="http://main.tf"><code>main.tf</code></a>):</h3>
<p>In the <a target="_blank" href="http://main.tf"><code>main.tf</code></a> file, these modules are composed to define the overall infrastructure. This file acts as the orchestrator, pulling in the individual modules and specifying their interconnections.</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">module</span> <span class="hljs-string">"VPC"</span> {
  source       = <span class="hljs-string">"./modules/vpc"</span>
  subnet_names = var.subnet_names
}
<span class="hljs-keyword">module</span> <span class="hljs-string">"security_group"</span> {
  source = <span class="hljs-string">"./modules/sg"</span>
  vpc_id = <span class="hljs-keyword">module</span>.VPC.vpc_id
}
<span class="hljs-keyword">module</span> <span class="hljs-string">"EC2"</span> {
  source    = <span class="hljs-string">"./modules/ec2"</span>
  sg_id     = <span class="hljs-keyword">module</span>.security_group.sg_id <span class="hljs-comment">// variable = module.&lt;module_name&gt;.&lt;output_name&gt;</span>
  subnet_id = <span class="hljs-keyword">module</span>.VPC.subnet_id
}
<span class="hljs-keyword">module</span> <span class="hljs-string">"alb"</span> {
  source = <span class="hljs-string">"./modules/alb"</span>
  sg_id = <span class="hljs-keyword">module</span>.security_group.sg_id
  subnet_id = <span class="hljs-keyword">module</span>.VPC.subnet_id  
  vpc_id = <span class="hljs-keyword">module</span>.VPC.vpc_id
  instance_ids = <span class="hljs-keyword">module</span>.EC2.instance_ids
}


# outputs
output <span class="hljs-string">"sg_id"</span> {
  value = <span class="hljs-keyword">module</span>.security_group.sg_id
}
output <span class="hljs-string">"ec2_public_ip"</span> {
  value = <span class="hljs-keyword">module</span>.EC2.aws_instance_public_ip
}
output <span class="hljs-string">"ec2_ids"</span> {
  value = <span class="hljs-keyword">module</span>.EC2.instance_ids
}
output <span class="hljs-string">"user_data"</span> {
  value = <span class="hljs-keyword">module</span>.EC2.user_data
}
</code></pre>
<p><strong>Explore Full Code:</strong></p>
<p>Note that this only the main file(<code>main.tf)</code>. For the complete Terraform project code, including modules and additional configurations, please visit this <a target="_blank" href="https://github.com/KunalBhat55/Terraform-Automate">GitHub repository.</a></p>
<h3 id="heading-benefits-of-modular-approach"><strong>Benefits of Modular Approach:</strong></h3>
<ul>
<li><p><strong>Organization:</strong> Clear separation of concerns for each component.</p>
</li>
<li><p><strong>Reusability:</strong> Modules can be reused in various projects or environments.</p>
</li>
<li><p><strong>Customization:</strong> Parameters allow for easy customization without modifying module internals.</p>
</li>
<li><p><strong>Maintainability:</strong> Simplifies maintenance by isolating updates within specific modules.</p>
</li>
</ul>
<h3 id="heading-github-actions-workflow">GitHub Actions Workflow:</h3>
<p>Before delving into the GitHub Actions workflow for your Automated AWS Infrastructure &amp; Deployment project, ensure the following prerequisites are set up:</p>
<ol>
<li><p><strong>GitHub Repository Structure:</strong></p>
<ul>
<li>Create a dedicated folder <code>.github/workflows</code>, in your GitHub repository to store your GitHub Actions workflow files (<code>YAML</code> file). This helps maintain a clean and organized repository structure and github for checking workflow files.</li>
</ul>
</li>
<li><p><strong>GitHub Secrets for AWS Credentials:</strong></p>
</li>
</ol>
<p>Add the necessary AWS credentials as secrets in your GitHub repository. This includes <code>AWS_ACCESS_KEY_ID</code>, <code>AWS_SECRET_ACCESS_KEY</code>, and <code>AWS_REGION</code>. Secrets provide a secure way to store sensitive information and are used.</p>
<p>**<br />Setting Up Secrets in GitHub**</p>
<p>To securely store sensitive information, such as AWS credentials, GitHub provides a feature called secrets. Here's a brief guide on how to set up secrets in your GitHub repository:</p>
<p><strong>1. Navigate to Your Repository:</strong></p>
<p>Go to your GitHub repository, and in the top navigation bar, click on "Settings."</p>
<p><strong>2. Access Secrets:</strong></p>
<p>In the left sidebar, find and click on "Secrets" under the "Settings" section.</p>
<p><strong>3. Add a New Secret:</strong></p>
<p>Click on the "New repository secret" button.</p>
<p><strong>4. Enter Secret Details:</strong></p>
<ul>
<li><p><strong>Name:</strong> Enter a name for your secret (e.g., <code>AWS_ACCESS_KEY_ID</code>).</p>
</li>
<li><p><strong>Value:</strong> Input the corresponding secret value (e.g., your AWS Access Key ID).</p>
</li>
</ul>
<p><strong>5. Save the Secret:</strong></p>
<p>Click on the "Add secret" button to save the secret. Repeat the process for additional secrets.</p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">terraform-deploy-cicd</span>
<span class="hljs-attr">run-name:</span> <span class="hljs-string">${{github.actor}}-terraform-deploy-cicd</span> 
<span class="hljs-comment"># github.actor refers to the username of the user or the name of the </span>
<span class="hljs-comment"># GitHub Actions bot that triggered the workflow. </span>
<span class="hljs-comment"># It represents the entity that initiated the event</span>
<span class="hljs-attr">on:</span>
  <span class="hljs-attr">push:</span>
    <span class="hljs-attr">branches:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">main</span>
    <span class="hljs-attr">paths-ignore:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">'README.md'</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">'LICENSE'</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">'**.md'</span>
<span class="hljs-attr">env:</span>
  <span class="hljs-attr">AWS_ACCESS_KEY_ID:</span> <span class="hljs-string">${{secrets.AWS_ACCESS_KEY_ID}}</span>
  <span class="hljs-attr">AWS_SECRET_ACCESS_KEY:</span> <span class="hljs-string">${{secrets.AWS_SECRET_ACCESS_KEY}}</span>
  <span class="hljs-attr">AWS_REGION:</span> <span class="hljs-string">${{secrets.AWS_REGION}}</span>

<span class="hljs-attr">jobs:</span>
  <span class="hljs-attr">deploy:</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">Terraform</span> <span class="hljs-string">Deployment</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
    <span class="hljs-attr">steps:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">name: Step 1:</span> <span class="hljs-string">Checkout</span> <span class="hljs-string">Code</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v2</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name: Step 2:</span> <span class="hljs-string">Configure</span> <span class="hljs-string">AWS</span> <span class="hljs-string">Credentials</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">aws-actions/configure-aws-credentials@v1</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-attr">aws-access-key-id:</span> <span class="hljs-string">${{secrets.AWS_ACCESS_KEY_ID}}</span>
          <span class="hljs-attr">aws-secret-access-key:</span> <span class="hljs-string">${{secrets.AWS_SECRET_ACCESS_KEY}}</span>
          <span class="hljs-attr">aws-region:</span> <span class="hljs-string">${{secrets.AWS_REGION}}</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name: Step 3:</span> <span class="hljs-string">Terraform</span> <span class="hljs-string">Setup</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">hashicorp/setup-terraform@v3</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-attr">terraform_wrapper:</span> <span class="hljs-literal">false</span> 

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name: Step 4:</span> <span class="hljs-string">Terraform</span> <span class="hljs-string">Init</span>
        <span class="hljs-attr">run:</span> <span class="hljs-string">terraform</span> <span class="hljs-string">init</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name: Step 5:</span> <span class="hljs-string">Terraform</span> <span class="hljs-string">Validate</span>
        <span class="hljs-attr">run:</span> <span class="hljs-string">terraform</span> <span class="hljs-string">validate</span> 

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name: Step 6:</span> <span class="hljs-string">Terraform</span> <span class="hljs-string">Apply</span>
        <span class="hljs-attr">run:</span> <span class="hljs-string">|
          TF_LOG=DEBUG
          terraform apply -auto-approve
</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">name: Step 7:</span> <span class="hljs-string">Terraform</span> <span class="hljs-string">Destroy</span>
        <span class="hljs-attr">run:</span> <span class="hljs-string">|
          TF_LOG=DEBUG
          terraform destroy -auto-approve</span>
</code></pre>
<h3 id="heading-steps">Steps:</h3>
<h4 id="heading-step-1-checkout-code">Step 1: Checkout Code</h4>
<ul>
<li>Uses GitHub Actions' built-in <code>actions/checkout</code> to fetch the Terraform code from the repository.</li>
</ul>
<h4 id="heading-step-2-configure-aws-credentials">Step 2: Configure AWS Credentials</h4>
<ul>
<li>Utilizes the <code>aws-actions/configure-aws-credentials</code> action to securely configure AWS credentials using GitHub Secrets.</li>
</ul>
<h4 id="heading-step-3-terraform-setup">Step 3: Terraform Setup</h4>
<ul>
<li>Leverages <code>hashicorp/setup-terraform</code> action to set up the correct version of Terraform in the GitHub Actions environment, ensuring compatibility.</li>
</ul>
<h4 id="heading-step-4-terraform-init">Step 4: Terraform Init</h4>
<ul>
<li>Executes <code>terraform init</code> to initialize the working directory and download necessary Terraform providers.</li>
</ul>
<h4 id="heading-step-5-terraform-validate">Step 5: Terraform Validate</h4>
<ul>
<li>Performs <code>terraform validate</code> to check Terraform configurations for syntax errors and potential issues.</li>
</ul>
<h4 id="heading-step-6-terraform-apply">Step 6: Terraform Apply</h4>
<ul>
<li>Initiates <code>terraform apply -auto-approve</code> to automatically apply infrastructure changes. Detailed logs are captured using <code>TF_LOG=DEBUG</code>.</li>
</ul>
<h4 id="heading-step-7-terraform-destroy">Step 7: Terraform Destroy</h4>
<ul>
<li>Executes <code>terraform destroy -auto-approve</code> to clean up the infrastructure, providing a complete deployment and destruction cycle.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1705944417124/855bfc24-c41b-49b4-85db-35a7eb8bc36a.png" alt class="image--center mx-auto" /></p>
<p><em>After you create the infrastructure remember to destroy the resources by running or directly adding</em><code>terraform destroy</code><em>command to destroy the created resources to not incur any charges.</em></p>
<h3 id="heading-overview-of-deployment">Overview of Deployment:</h3>
<h3 id="heading-1-ec2-instances"><strong>1. EC2 Instances</strong></h3>
<p><em>The EC2 module deploys instances hosting the web application, leveraging Amazon Linux 2 and nginx.</em></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1705943624165/4c7cda82-21cf-442e-ac74-e7bb4aaf4c1e.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-2-application-load-balancer-alb"><strong>2. Application Load Balancer (ALB)</strong></h3>
<p><em>The ALB module ensures high availability by evenly distributing traffic across multiple EC2 instances.</em></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1705943851844/3b3b7cf4-9fd7-4ac3-ada1-86c7b9dc578f.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-4-terraform-orchestrated-deployment"><strong>4. Terraform Orchestrated Deployment</strong></h3>
<p><em>This is the deployed site on both the ec2 instances after installing a nginx server by passing a shell script to a</em><code>user_data</code><em>attribute of ec2 instance resource in terraform alternatively you can also install or created files after installing nginx on instance.</em></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1705944105628/851364d4-ae6c-4936-ac12-2a4e28caea59.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1705944126031/5bebb5f6-8609-4560-a64f-69d8787edd47.png" alt class="image--center mx-auto" /></p>
<p>As depicted in the images above, observe how the Application Load Balancer efficiently <strong>distributes</strong> incoming traffic across <strong>both instances</strong>, ensuring optimal load balancing and high availability for the hosted application.</p>
<p>This <strong>dynamic distribution</strong> mechanism plays a crucial role in enhancing the <strong>performance</strong> and reliability of the infrastructure by effectively managing the workload among the deployed instances.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, the Automated AWS Infrastructure &amp; Deployment project, driven by Terraform and GitHub Actions, exemplifies the efficiency and scalability achieved through Infrastructure as Code (IaC). The modular Terraform structure, coupled with a streamlined GitHub Actions workflow, ensures a consistent and automated deployment pipeline.</p>
<p>For a hands-on experience, explore the complete Terraform project on <a target="_blank" href="https://github.com/KunalBhat55/Terraform-Automate">GitHub</a>. Happy coding!</p>
<h3 id="heading-next-steps">Next Steps:</h3>
<p>Encourage readers to explore the project on GitHub, experiment with the provided code, and adapt it to their own use cases. Mention any related resources, documentation, or additional topics they might find beneficial and do checkout my other <a target="_blank" href="https://kunalbhat.hashnode.dev/">blogs</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Unleashing the Power of Terraform: A Journey into Infrastructure as Code]]></title><description><![CDATA[Introduction
In the rapidly evolving landscape of cloud computing, managing infrastructure efficiently has become a critical aspect of software development. Enter Terraform, a revolutionary Infrastructure as Code (IaC) tool that empowers developers t...]]></description><link>https://kunalbhat.hashnode.dev/unleashing-the-power-of-terraform-a-journey-into-infrastructure-as-code</link><guid isPermaLink="true">https://kunalbhat.hashnode.dev/unleashing-the-power-of-terraform-a-journey-into-infrastructure-as-code</guid><category><![CDATA[Infrastructure as code]]></category><category><![CDATA[AWS]]></category><category><![CDATA[cloudformation]]></category><category><![CDATA[Terraform]]></category><category><![CDATA[#HCL]]></category><dc:creator><![CDATA[Kunal Bhat]]></dc:creator><pubDate>Sun, 19 Nov 2023 08:16:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1700381661878/d9154fa3-586e-4f54-99c3-d52bf50769c5.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>In the rapidly evolving landscape of cloud computing, managing infrastructure efficiently has become a critical aspect of software development. Enter Terraform, a revolutionary Infrastructure as Code (IaC) tool that empowers developers to provision and manage infrastructure in a declarative and version-controlled manner. In this blog, we will embark on a journey to unravel the core concepts of Terraform, exploring its inner workings, state management, and the role of providers.</p>
<h2 id="heading-what-is-terraform">What is Terraform?</h2>
<p>At its core, Terraform is an open-source IaC tool developed by HashiCorp. It enables developers to define and provision infrastructure using a declarative configuration language. This means you describe the desired state of your infrastructure, and Terraform takes care of bringing it to life.</p>
<h2 id="heading-infrastructure-as-code-iac">Infrastructure as Code (IaC)</h2>
<p>IaC is a paradigm shift in infrastructure management. Instead of manually configuring servers and resources, IaC allows developers to codify infrastructure using scripts. This approach brings numerous benefits, including repeatability, version control, and collaboration.</p>
<p>Terraform embraces IaC by allowing users to define infrastructure in HashiCorp Configuration Language (HCL), a simple and human-readable language. With IaC, infrastructure becomes more predictable, scalable, and manageable and quite resembles javascript objects or JSON.</p>
<h2 id="heading-core-concepts-of-terraform">Core Concepts of Terraform</h2>
<h3 id="heading-1-providers">1. <strong>Providers:</strong></h3>
<p>At the heart of Terraform are providers. Providers define and manage the lifecycle of resources, such as virtual machines, networks, or databases. Providers interact with APIs of various cloud or on-premises services.</p>
<p><em>In this blog, we will consider AWS as an example but you can choose from multiple provider plugins from the Terraform</em> <a target="_blank" href="https://registry.terraform.io/browse/providers"><em>site</em></a><em>.</em></p>
<pre><code class="lang-javascript">terraform { <span class="hljs-comment">// terraform block defines the version of terraform to be used and the backend configuration</span>
 <span class="hljs-comment">// local backend configuration</span>
  backend <span class="hljs-string">"local"</span> {
    path = <span class="hljs-string">"terraform.tfstate"</span>
  }
  required_providers {
    aws = {
      source  = <span class="hljs-string">"hashicorp/aws"</span> <span class="hljs-comment">//  location of the provider plugin.</span>
      version = <span class="hljs-string">"~&gt; 5.0"</span>        <span class="hljs-comment">//  Terraform registry path and 5+ version</span>
    }  

  }
}
</code></pre>
<p>In this example, we declare an AWS provider and specify the region for resource provisioning.</p>
<ol>
<li><p><strong>terraform block:</strong> This block defines the Terraform version to be used. In this case, it's not explicitly set, so it will use the default version installed on your system.</p>
</li>
<li><p><strong>backend "local" {…}:</strong> This block defines a local Terraform backend, which stores the Terraform state file on the local filesystem. The path specified is <code>terraform.tfstate</code> and it maintains the state file and keeps track of the desired state.</p>
</li>
<li><p><strong>required_providers {aws = {…}:</strong> This block defines the required provider plugin for AWS. The source is set to <code>hashicorp/aws</code>, which indicates that the plugin should be downloaded from the HashiCorp registry. The version is set to "~&gt; 5.0", which means that Terraform will use the latest version of the plugin that is greater than or equal to 5.0.</p>
</li>
</ol>
<h3 id="heading-2-resources">2. <strong>Resources:</strong></h3>
<p>Resources are the building blocks of your infrastructure. They represent the cloud components you want to create, such as instances, storage, or databases. Terraform uses resource configurations to understand what infrastructure to create.</p>
<pre><code class="lang-javascript">resource <span class="hljs-string">"aws_instance"</span> <span class="hljs-string">"Terraform-instance"</span> {
  ami           = <span class="hljs-string">"ami-0c55b159cbfafe1f0"</span>
  instance_type = <span class="hljs-string">"t2.micro"</span>
  tags = {
    Name = <span class="hljs-string">"terraformGettingStarted"</span>
  }
  count = <span class="hljs-number">2</span>
}
</code></pre>
<p>Here, we define an AWS instance with a specific Amazon Machine Image (AMI) and instance type with a count of 2 instances and tags for reference. The resource type and resource name both act as a unique ID for the resource for identification.</p>
<h3 id="heading-3-variables">3. <strong>Variables:</strong></h3>
<p>Variables provide a way to parameterize your configurations. They allow you to reuse and customize configurations for different environments or use cases.</p>
<pre><code class="lang-json">variable <span class="hljs-string">"instance_type"</span> {
  description = <span class="hljs-attr">"The type of EC2 instance"</span>
  default     = <span class="hljs-attr">"t2.micro"</span>
}
</code></pre>
<p>This example creates a variable for the instance type, allowing flexibility in configuration.</p>
<h3 id="heading-4-modules">4. <strong>Modules:</strong></h3>
<p>Modules enable code reuse and maintainability. They encapsulate a set of resources and configurations, allowing you to organize and share infrastructure code.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span> <span class="hljs-string">"web_server"</span> {
  source = <span class="hljs-string">"./modules/web_server"</span>

  instance_count = <span class="hljs-number">3</span>
}
</code></pre>
<p>Here, we use a module to create multiple web server instances with a shared configuration.</p>
<h2 id="heading-terraform-state">Terraform State</h2>
<p>Terraform maintains a state file that records the current state of your infrastructure. This state is crucial for understanding what Terraform has provisioned and for making informed decisions about changes.</p>
<p>State files can be stored remotely or locally, depending on your configuration. Terraform uses the state to plan and apply changes, ensuring that the desired and actual states align.</p>
<h2 id="heading-terraform-workflow">Terraform Workflow</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1700380144841/f38677a8-9800-4925-af36-6bc0dccf4246.png" alt class="image--center mx-auto" /></p>
<ol>
<li><p>The user writes a Terraform configuration file, which defines the desired state of the infrastructure.</p>
</li>
<li><p>The user runs the <code>terraform plan</code> command to generate a plan of the changes that will be made if the configuration file is applied.</p>
</li>
<li><p>The user reviews the plan and, if they are satisfied, runs the <code>terraform apply</code> command to apply the changes.</p>
<p> Terraform will then create, update, or delete resources as needed to match the desired state defined in the configuration file.</p>
</li>
</ol>
<h2 id="heading-example-screenshot">Example Screenshot</h2>
<p><em>Here is an example from the above configuration.</em></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1700379008746/64f70e48-4161-4572-962d-a19d107ef4cf.png" alt="rfrf" class="image--center mx-auto" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Terraform's power lies in its simplicity, flexibility, and scalability. By embracing IaC principles, developers can efficiently manage infrastructure, automate repetitive tasks, and collaborate seamlessly. Understanding core concepts like providers, resources, variables, and modules is key to harnessing the full potential of Terraform.</p>
<p>In subsequent blog posts, we'll dive deeper into advanced Terraform topics, exploring best practices, integration with CI/CD pipelines, and tackling real-world infrastructure challenges. Until then, happy Terraforming!</p>
]]></content:encoded></item><item><title><![CDATA[Serverless With AWS Lambda]]></title><description><![CDATA[Introduction
In the ever-evolving landscape of cloud computing, serverless technology has emerged as a game-changer. Among the leading platforms that offer serverless capabilities, Amazon Web Services (AWS) has been a pioneer with its Lambda service....]]></description><link>https://kunalbhat.hashnode.dev/serverless-with-aws-lambda</link><guid isPermaLink="true">https://kunalbhat.hashnode.dev/serverless-with-aws-lambda</guid><category><![CDATA[AWS]]></category><category><![CDATA[aws lambda]]></category><category><![CDATA[serverless]]></category><category><![CDATA[Cloud]]></category><category><![CDATA[Node.js]]></category><dc:creator><![CDATA[Kunal Bhat]]></dc:creator><pubDate>Fri, 13 Oct 2023 07:03:55 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1697693056560/f7223012-8731-4ccf-b4ca-f05c9b9ec4ec.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction"><strong>Introduction</strong></h3>
<p>In the ever-evolving landscape of cloud computing, serverless technology has emerged as a game-changer. Among the leading platforms that offer serverless capabilities, Amazon Web Services (AWS) has been a pioneer with its Lambda service. In this blog, we'll delve into my hands-on experience with AWS Lambda, explore how serverless works, and unravel the fascinating world of Lambda functions, layers, triggers, destinations, and related concepts.</p>
<h2 id="heading-understanding-serverless-computing">Understanding Serverless Computing</h2>
<p>To kick things off, let's clarify what "serverless" actually means. Contrary to the name, it doesn't mean there are no servers involved. Instead, it implies that developers no longer need to manage the infrastructure. They can focus solely on writing code while AWS takes care of provisioning, scaling, and maintaining the servers.</p>
<p>Serverless computing enables developers to build applications with less operational overhead and more agility. AWS Lambda, being a serverless computing service, plays a pivotal role in this serverless revolution.</p>
<h2 id="heading-aws-lambda-the-heart-of-serverless">AWS Lambda: The Heart of Serverless</h2>
<h3 id="heading-what-is-aws-lambda">What is AWS Lambda?</h3>
<p>AWS Lambda is a computing service that automatically scales your code in response to incoming requests. It allows you to run code without provisioning or managing servers, making it a fundamental component of serverless architecture.</p>
<h3 id="heading-hands-on-with-lambda-functions">Hands-On with Lambda Functions</h3>
<p>My journey began by creating Lambda functions. These are small, single-purpose units of code that can be triggered by various AWS services or HTTP requests. AWS provides support for multiple programming languages, which means you can write your Lambda functions in your language of choice like Nodejs, Python etc.</p>
<p>To create a Lambda function, you write your code and specify the event source that triggers the function's execution. Events can range from HTTP requests through Amazon API Gateway to database changes using Amazon DynamoDB streams.</p>
<p>Once the Lambda function is set up, AWS automatically takes care of scaling it according to the incoming request rate. This means you don't have to worry about server provisioning or maintenance.</p>
<h2 id="heading-lambda-layers-reusable-code-components">Lambda Layers: Reusable Code Components</h2>
<p>While working with Lambda functions, I discovered the utility of Lambda Layers. These are a way to manage and share code and data across multiple functions. Instead of duplicating code across functions, you can package common libraries, custom runtimes, or even custom tools as layers.</p>
<p>Lambda Layers promote code reusability, making it easier to manage and update shared components without having to redeploy every function that uses them.</p>
<h2 id="heading-triggers-and-destinations-orchestrating-workflows">Triggers and Destinations: Orchestrating Workflows</h2>
<p>AWS Lambda functions can be triggered by a variety of event sources. These include AWS services like S3, SQS, and SNS, as well as custom events through the Amazon API Gateway. The choice of trigger depends on the specific use case and the need for seamless integration within your application architecture.</p>
<p>In my hands-on experience, I configured triggers for my Lambda functions that enabled them to respond to specific events. This allowed me to build event-driven, serverless workflows with ease.</p>
<p>Moreover, AWS Lambda offers the ability to configure destinations for asynchronous invocations. You can send the results of a Lambda function's execution to various AWS services, making it simpler to manage the flow of data and processing within your serverless applications.</p>
<h2 id="heading-heres-a-simple-example-of-an-aws-lambda-function-written-in-nodejs">Here's a simple example of an AWS Lambda function written in Node.js</h2>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> handler = <span class="hljs-keyword">async</span> (event, context) =&gt; {
    <span class="hljs-comment">// Convert to JS string</span>
    <span class="hljs-keyword">const</span> body = <span class="hljs-built_in">JSON</span>.stringify(event.body);

    <span class="hljs-comment">// Perform some processing</span>
    <span class="hljs-keyword">const</span> result = processInput(body);

    <span class="hljs-comment">// Prepare the response</span>
    <span class="hljs-keyword">const</span> response = {
        <span class="hljs-attr">statusCode</span>: <span class="hljs-number">200</span>,
        <span class="hljs-attr">body</span>: <span class="hljs-built_in">JSON</span>.stringify(result),
        <span class="hljs-attr">functionName</span>: context.functionName,
        <span class="hljs-attr">requestID</span>: context.awsRequestId, 
    };
   <span class="hljs-comment">// The context object is particularly useful for monitoring and </span>
   <span class="hljs-comment">// logging, as you can use the information it provides to record execution details, </span>
   <span class="hljs-comment">// log to CloudWatch Logs</span>

    <span class="hljs-keyword">return</span> response;
};

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">processInput</span>(<span class="hljs-params">data</span>) </span>{
    <span class="hljs-comment">// In this example, we'll simply return the input data as is.</span>
    <span class="hljs-comment">// You can replace this with more complex processing as needed.</span>
    <span class="hljs-keyword">return</span> data;
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697180041794/177c281d-58df-48dc-908b-6175c3ce442c.png" alt class="image--center mx-auto" /></p>
<p>Output:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697180078446/447df171-8c53-43c7-abd8-86a12328ce7e.png" alt class="image--center mx-auto" /></p>
<p><strong>Note:</strong> To get the output "Hello From lambda" you need to create a test event cause we haven't set any triggers as of now, you can do that in the test and create a new test event and simply add <code>{ "body": "Hello From Lambda 101" }</code> or any other string you would like and make sure to deploy the changes.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>My hands-on experience with AWS Lambda was a revelation. It not only provided a glimpse into the world of serverless computing but also offered a practical understanding of how to build scalable, event-driven applications with ease. Lambda functions, layers, triggers, and destinations are all integral parts of this serverless ecosystem, providing a powerful toolset for developers to create efficient, cost-effective, and scalable applications.</p>
<p>As serverless technology continues to evolve, AWS Lambda remains at the forefront, enabling developers to focus on code while AWS manages the infrastructure. If you're looking to embrace the serverless revolution, AWS Lambda is a fantastic place to start.</p>
]]></content:encoded></item></channel></rss>