<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: AnanyaDasgupta</title>
    <description>The latest articles on DEV Community by AnanyaDasgupta (@ananyadasgupta).</description>
    <link>https://dev.to/ananyadasgupta</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1224407%2Ffe830b14-d4c0-46f0-bf4c-ad42503dd9a8.jpeg</url>
      <title>DEV Community: AnanyaDasgupta</title>
      <link>https://dev.to/ananyadasgupta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ananyadasgupta"/>
    <language>en</language>
    <item>
      <title>Terraform 101: Creating Secure Subnets in Your VPC</title>
      <dc:creator>AnanyaDasgupta</dc:creator>
      <pubDate>Thu, 25 Apr 2024 12:11:37 +0000</pubDate>
      <link>https://dev.to/ananyadasgupta/terraform-101-creating-secure-subnets-in-your-vpc-553m</link>
      <guid>https://dev.to/ananyadasgupta/terraform-101-creating-secure-subnets-in-your-vpc-553m</guid>
      <description>&lt;p&gt;Welcome to Part 2 of the series: &lt;a href="https://dev.to/ananyadasgupta/series/27114"&gt;Terraform for Beginners&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Previously, we learned how to create a VPC in AWS using Terraform. In this blog, we'll learn about subnets and how to create them using Terraform.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a subnet?
&lt;/h2&gt;

&lt;p&gt;According to AWS documentation,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A subnet is a range of IP addresses in your VPC. You can create AWS resources, such as EC2 instances, in specific subnets.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's a breakdown of the key aspects of a subnet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Segmentation: Subnets allow you to partition your VPC into isolated units. This enhances security by restricting resources within one subnet from directly accessing resources in another subnet by default. If needed, you can configure specific routing rules to allow controlled communication between subnets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placement: You can create multiple subnets within your VPC, each with its own CIDR block (a range of IP addresses). When launching resources like EC2 instances within your VPC, you can specify their subnet. This placement determines their network access and security posture.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Public vs. Private: A common practice involves creating two main types of subnets:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Public Subnet: Resources placed in a public subnet have direct access to the internet. This allows them to receive inbound traffic from the internet and potentially send outbound traffic. Public subnets are suitable for resources like web servers that need to be accessed publicly.&lt;/li&gt;
&lt;li&gt;Private Subnet: Resources in a private subnet don't have direct internet access by default. This improves security for resources that don't require exposure to the internet. However, if these resources need controlled outbound access (e.g., downloading updates), you can leverage a NAT Gateway (Network Address Translation Gateway) to provide it.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at the architecture diagram.&lt;/p&gt;

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

&lt;p&gt;We have to create a public and private subnet inside the vpc we created in the previous blog. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Complete the tutorial for creating a vpc as we will continue from there.&lt;br&gt;
You can check out the tutorial &lt;a href="https://dev.to/ananyadasgupta/terraform-for-newbs-launching-a-simple-aws-backend-welcome-to-the-series-jh"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Terraform Code to create Subnet in AWS
&lt;/h2&gt;

&lt;p&gt;Step 1: In your vpc.tf file copy and paste the following piece of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_subnet" "public_subnet" {
  for_each                = var.public_subnet
  vpc_id                  = aws_vpc.vpc.id
  cidr_block              = each.value.cidr_block
  availability_zone       = each.value.availability_zone
  map_public_ip_on_launch = true
  tags = {
    Name         = "${var.env}-${local.project}-${each.key}"
    CreatedByTer = true
  }
}

resource "aws_subnet" "private_subnet" {
  for_each          = var.private_subnet
  vpc_id            = aws_vpc.vpc.id
  cidr_block        = each.value.cidr_block
  availability_zone = each.value.availability_zone
  tags = {
    Name          = "${var.env}-${local.project}-${each.key}"
    CreatedByTer  = true
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Explanation&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;for_each&lt;/em&gt; allows you to create multiple subnets with varying configurations based on the data provided in the variables named &lt;em&gt;public_subnet&lt;/em&gt; and &lt;em&gt;private_subnet&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;cidr_block = each.value.cidr_block&lt;/em&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This line defines the CIDR block for the subnets. However, it uses a dynamic approach by referencing each.value.cidr_block. This suggests that &lt;em&gt;var.public_subnet&lt;/em&gt; and &lt;em&gt;var.private_subnet&lt;/em&gt; is a map or list containing key-value pairs where the key is used for iteration (each.key) and the value (each.value) contains a nested object with a cidr_block key. This allows you to define multiple CIDR blocks for different subnets within the loop.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;em&gt;availability_zone = each.value.availability_zone&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similar to the cidr_block, this line defines the availability zone for the subnets using a dynamic reference to the availability_zone key within the nested objects. This allows you to distribute your resources across zones for redundancy.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;em&gt;map_public_ip_on_launch&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This line sets the map_public_ip_on_launch attribute to true or false. When set to true, any EC2 instances launched within that subnet will automatically receive a public IP address, allowing them to be accessed directly from the internet.
Similarly, when set to false, the instances will not be assigned a public IP during creation.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will get an error as we have not yet created the &lt;em&gt;public_subnet&lt;/em&gt; and &lt;em&gt;private_subnet&lt;/em&gt; variables.&lt;/p&gt;

&lt;p&gt;Step 2: In your variables.tf file copy and paste the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;variable "public_subnet" {
  type        = map(any)
  description = "Public Subnets to be created"
}
variable "private_subnet" {
  type        = map(any)
  description = "Private Subnets to be created"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Explanation&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;This code block defines two Terraform variables named "public_subnet" and "private_subnet". It specifies that the variables will hold a collection of data in the form of a map.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;While 'any' allows for flexibility, using more specific data types within the map can improve code readability and catch potential errors during configuration.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 3: In your terraform.tfvars file copy and paste the following code block&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public_subnet = {
  public-subnet-1 = {
    cidr_block        = "10.0.101.0/24"
    availability_zone = "ap-south-1a"
  }
}
private_subnet = {
  private-subnet-1 = {
    cidr_block        = "10.0.1.0/24"
    availability_zone = "ap-south-1a"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Explanation&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The value of the &lt;em&gt;public_subnet&lt;/em&gt; and &lt;em&gt;private_subnet&lt;/em&gt; variables are defined here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This configuration defines the above variables as &lt;em&gt;map of a map&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;public-subnet-1&lt;/em&gt; is the name of the first child map of the variable &lt;em&gt;public_subnet&lt;/em&gt;. It consists of two key-value pairs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;cidr_block specifies&lt;/em&gt; the CIDR range for the subnet&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;availability_zone&lt;/em&gt; specifies the AWS availability zone the subnet will reside in. Here we are creating the subnet in the _Mumbai _region.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Similarly, &lt;em&gt;private-subnet-1&lt;/em&gt; is the name of the first child of the variable &lt;em&gt;private_subnet&lt;/em&gt;. It consists of the same key-value pairs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Deploy infrastructure to AWS
&lt;/h2&gt;

&lt;p&gt;Follow the steps &lt;a href="https://dev.to/ananyadasgupta/terraform-for-newbs-launching-a-simple-aws-backend-welcome-to-the-series-jh"&gt;here&lt;/a&gt; to deploy the changes to AWS.&lt;/p&gt;

&lt;p&gt;Congratulations! You have successfully created subnets in AWS using terraform.&lt;/p&gt;

&lt;p&gt;Up next: Internet Gateway! Stay tuned. 👋&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>aws</category>
      <category>infrastructureascode</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Terraform for Newbs: Launching a Simple AWS Backend (Welcome to the Series!)</title>
      <dc:creator>AnanyaDasgupta</dc:creator>
      <pubDate>Wed, 17 Apr 2024 10:25:26 +0000</pubDate>
      <link>https://dev.to/ananyadasgupta/terraform-for-newbs-launching-a-simple-aws-backend-welcome-to-the-series-jh</link>
      <guid>https://dev.to/ananyadasgupta/terraform-for-newbs-launching-a-simple-aws-backend-welcome-to-the-series-jh</guid>
      <description>&lt;p&gt;In today's world, code can be used to achieve remarkable feats, from building complex applications to managing your infrastructure across cloud providers like AWS, Azure, and GCP. While manually provisioning resources through a cloud console might be feasible for small-scale projects, large production applications require a more robust and scalable approach. &lt;/p&gt;

&lt;p&gt;The following scenarios are just some of the many nuances of manual resource provisioning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Configuration Drift&lt;/strong&gt;: Over time, configurations can diverge between development, testing, and production environments if managed manually. This inconsistency can lead to unexpected behavior and troubleshooting challenges.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Human Error&lt;/strong&gt;: Manual configuration is prone to typos and errors, potentially leading to security vulnerabilities, service outages, or wasted resources due to misconfiguration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Limited Collaboration&lt;/strong&gt;: Sharing and collaborating on infrastructure configurations can be difficult with manual provisioning. Centralized code repositories become less practical, hindering collaboration between developers and infrastructure teams.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where &lt;strong&gt;Infrastructure as Code (IaC)&lt;/strong&gt; comes into play. IaC allows developers to define and provision infrastructure resources using code. One such widely used IaC tool is &lt;strong&gt;Terraform&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This article is the first in a series of articles where I will discuss how to provision a simple backend architecture in AWS using Terraform.&lt;/p&gt;

&lt;p&gt;Note: Terraform uses HCL (HashiCorp Configuration Language), a domain-specific language (DSL) to define and configure infrastructure resources.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;AWS Account with necessary permissions to create resources in it.&lt;/li&gt;
&lt;li&gt;AWS IAM user with access credentials.&lt;/li&gt;
&lt;li&gt;VS Code or any other code editor.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Architecture Diagram
&lt;/h2&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Components&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;VPC&lt;/li&gt;
&lt;li&gt;Subnets (One each for Public and Private)&lt;/li&gt;
&lt;li&gt;Internet Gateway&lt;/li&gt;
&lt;li&gt;Route Table&lt;/li&gt;
&lt;li&gt;NAT Gateway&lt;/li&gt;
&lt;li&gt;EC2 instances &lt;/li&gt;
&lt;li&gt;Application Load Balancer&lt;/li&gt;
&lt;li&gt;Auto Scaling Groups&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a brief intro to the components. I will suggest referring to the AWS documentation to learn about them in detail.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;VPC(Virtual Private Cloud): The foundation of your isolated network. It is a virtual network that is hosted within a public cloud. It provides a level of isolation between different organizations that use the resources. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Public Subnet: A subnet within the VPC that resides in a public availability zone. It has a route table configured to allow access from the internet. This subnet typically houses the application load balancer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Private Subnet: A subnet within the VPC that resides in a private availability zone. It has a route table configured to restrict access from the internet. This subnet houses your EC2 backend servers for security reasons.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Internet Gateway: An internet gateway attached to the VPC, enabling resources in the public subnet (like the load balancer) to access the internet&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;NAT Gateway (Optional): A NAT Gateway in the public subnet can act as an internet gateway for the private subnet, allowing outbound traffic from your backend servers for updates or external service communication (e.g., downloading updates from a public repository).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Application Load Balancer: Distributes incoming traffic from the internet across healthy backend servers in the private subnet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;EC2 Instances: Backend servers running your application code. They reside in the private subnet for security purposes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Auto Scaling Groups: A group that manages backend servers. It can automatically scale the number of EC2 instances up or down based on pre-defined policies (e.g., CPU utilization).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, I will focus on creating the infrastructure for a standalone VPC.&lt;/p&gt;

&lt;h2&gt;
  
  
  Directory Setup
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Step 1: Create a folder in your local environment named backend.&lt;/li&gt;
&lt;li&gt;Step 2: Open the folder in a code editor of your choice. I will use VS Code for this project.&lt;/li&gt;
&lt;li&gt;Step 3: Open the terminal in your code editor and run the command &lt;code&gt;mkdir vpc&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Step 4: In your terminal run the command &lt;code&gt;cd vpc&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before we start with Terraform, we need to configure our AWS credentials so that Terraform can be deployed to AWS from our local environment.&lt;/p&gt;

&lt;p&gt;In your terminal, run &lt;code&gt;aws configure&lt;/code&gt;. Set the &lt;code&gt;AWS Access Key ID&lt;/code&gt;, &lt;code&gt;AWS Secret Access Key&lt;/code&gt;, &lt;code&gt;Default region name&lt;/code&gt;, and &lt;code&gt;Default output format&lt;/code&gt;. Press enter for the default values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terraform Code to create a VPC in AWS
&lt;/h2&gt;

&lt;p&gt;Step 1: Create &lt;strong&gt;provider.tf&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside the vpc folder create a file named 'provider.tf'. Paste the following code block inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform {
  required_version = "&amp;gt;= 1.0"
  required_providers {
    aws = {
        source = "hashicorp/aws"
        version = "&amp;gt;= 4.66.1"
    }
  }
}

provider "aws" {
  region              = "ap-south-1"
  allowed_account_ids = [local.aws_account_id]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A provider in Terraform is a plugin that enables interaction with an API. This file contains the details of all the providers used in the project.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Terraform Provider Configuration Breakdown:&lt;br&gt;
&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;required_version: This specifies the minimum Terraform version required to execute this configuration. Ensure you have Terraform version 1.0 or above installed to work with this code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;required_providers: This block defines the required providers and their versions. Here, we specify the AWS provider with a minimum version of 4.66.1. A specific version ensures compatibility between your Terraform configuration and the provider's functionalities.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;AWS Provider Configuration Breakdown:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;provider "aws" Block: This block configures the AWS provider itself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;region: This specifies the AWS region where your infrastructure will be deployed. In this example, we're using "ap-south-1" which corresponds to the Asia Pacific South (Mumbai) region.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;allowed_account_ids: (Optional) This restricts Terraform to managing resources only within the specified account IDs. Here, we're referencing a variable named aws_account_id defined in a separate variable file. This helps ensure Terraform only manages resources within your authorized accounts.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will get this error: "No declaration found for "local.aws_account_id". To solve this we have to create the locals.tf file&lt;/p&gt;

&lt;p&gt;Step 2: Create &lt;strong&gt;"locals.tf"&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Terraform locals define and assign values within your terraform configuration. These values can be reused throughout your code, making your configuration more concise and easier to maintain.&lt;/p&gt;

&lt;p&gt;Inside the vpc folder create a file named "locals.tf". Paste the following code in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;locals {
  project = "demo"
  aws_account_id = xxxxxxxxxxxx
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace the value of the  aws_account_id variable with your own AWS account ID.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Locals Configuration Breakdown:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;project: This local value assigns the string "demo" to the name project. You can use this value throughout your Terraform configuration wherever you need to reference your project name (e.g., naming resources).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;aws_account_id: This local value assigns the 12-digit AWS account ID to the name aws_account_id.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 3: Create &lt;strong&gt;variables.tf&lt;/strong&gt; and &lt;strong&gt;terraform.tfvars&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Terraform variables are a powerful mechanism for defining configuration values outside your Terraform code. They act as placeholders that can be assigned values later, typically through a separate file named 'terraform.tfvars'.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;variables.tf&lt;/u&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;variable "env" {
  type = string
}

variable "vpc_conf" {
  type = map(any)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Explanation of variables.tf code:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;env: This variable has a type of string, indicating it will hold a textual value.  You can use this variable throughout your Terraform configuration to reference the environment (e.g., "dev", "staging", "production").&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;vpc_conf: This variable has a type of map(any), which essentially defines a key-value dictionary. This allows you to define multiple configuration options for your VPC in a structured manner.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;terraform.tfvars&lt;/u&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;env = "dev"
vpc_conf = {
  cidr_block           = "10.0.0.0/16"
  instance_tenancy     = "default"
  enable_dns_hostnames = true
  enable_dns_support   = true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Explanation of terraform.tfvars code:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;env = "dev": This line assigns the string value "dev" to the env variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;vpc_conf block: This block defines key-value pairs for the vpc_conf variable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cidr_block = "10.0.0.0/16": This defines the CIDR 
block for your VPC.&lt;/li&gt;
&lt;li&gt;instance_tenancy = "default": This defines the 
tenancy for your VPC instances (Default value is "default". The only other option is "dedicated").&lt;/li&gt;
&lt;li&gt;enable_dns_hostnames = (Optional) A boolean flag to enable/disable DNS hostnames in the VPC. Defaults to false.&lt;/li&gt;
&lt;li&gt;enable_dns_support = (Optional) A boolean flag to enable/disable DNS support in the VPC. Defaults to true.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Refer to the official terraform documentation &lt;a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc"&gt;here&lt;/a&gt; to learn more about the attributes used.&lt;/p&gt;

&lt;p&gt;Step 4: Create &lt;strong&gt;vpc.tf&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;The vpc.tf file defines an AWS VPC resource using the aws_vpc resource block provided by the Terraform AWS provider. This block creates a virtual private cloud (VPC) within your AWS account.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_vpc" "vpc" {
  cidr_block           = var.vpc_conf.cidr_block
  instance_tenancy     = var.vpc_conf.instance_tenancy
  enable_dns_hostnames = var.vpc_conf.enable_dns_hostnames
  enable_dns_support   = var.vpc_conf.enable_dns_support
  tags = {
    Name          = "${var.env}-${local.project}-vpc"
    CreatedByTer  = true
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code demonstrates how to leverage variables defined in variables.tf. Notice how it references the vpc_conf map and accesses specific configuration values using the dot notation (e.g., var.vpc_conf.cidr_block). This approach keeps your Terraform code clean and avoids hardcoding configuration details.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Explanation of vpc.tf code:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;cidr_block = var.vpc_conf.cidr_block: This line sets the CIDR block for the VPC using the value assigned to the cidr_block key within the vpc_conf variable map (defined in terraform.tfvars).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;instance_tenancy = var.vpc_conf.instance_tenancy: This line sets the instance tenancy for the VPC using the value assigned to the instance_tenancy key within the vpc_conf variable map.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;enable_dns_hostnames = var.vpc_conf.enable_dns_hostnames: This line enables DNS hostnames for the VPC resources based on the value assigned to the enable_dns_hostnames key within the vpc_conf variable map.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;enable_dns_support = var.vpc_conf.enable_dns_support: This line enables DNS support for the VPC based on the value assigned to the enable_dns_support key within the vpc_conf variable map.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;Adding tags:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Name = "${var.env}-${local.project}-vpc": This line dynamically creates a tag named "Name" using string interpolation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CreatedByTerraform = true: This line assigns a tag named "CreatedByTerraform" with a " true " value, indicating this VPC was created using Terraform.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 5: Create &lt;strong&gt;backend.tf&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;This code snippet defines a Terraform backend using the backend block. By default, Terraform stores the state of your infrastructure (information about created resources) in a local file named terraform.tfstate within your project directory. This local state file is not ideal for production environments due to limitations like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Single Point of Failure: If the local file is lost or corrupted, you might lose track of your infrastructure state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Collaboration Challenges: When working with multiple collaborators, sharing and managing the state file becomes cumbersome.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;Using S3 Backend:&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;To address these limitations, we are going to configure an S3 backend. This instructs Terraform to store the state information in an Amazon S3 bucket. Here's a breakdown of the configuration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;backend "s3": This line declares the backend type as "s3", indicating we'll use an S3 bucket to store the state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;bucket = "terraform-demo-state-files": This line specifies the name of the S3 bucket where Terraform will store the state file. It's recommended to choose a descriptive name for your bucket.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;region = "us-east-1": This line specifies the AWS region where the S3 bucket resides. It is generally recommended to have the S3 bucket where your Terraform state is stored.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;key = "state": This line defines the key (filename) within the S3 bucket where Terraform will store the state information. You can customize this key name if needed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Deploy infrastructure to AWS
&lt;/h2&gt;

&lt;p&gt;Now that we've defined our infrastructure using Terraform code, it's time to deploy it to AWS! &lt;br&gt;
Here's a typical workflow for deploying Terraform code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize Terraform: Open the terminal of code editor, and navigate to the 'vpc' directory. Run &lt;code&gt;terraform init&lt;/code&gt;. This downloads and installs any required plugins (like the AWS provider) based on your configuration.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Review Changes: Before applying, run &lt;code&gt;terraform plan&lt;/code&gt; to see a preview of the changes Terraform will make to your AWS environment. This allows you to verify the planned actions before actual creation.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Apply the Configuration (Optional): Once you are satisfied with the plan, run &lt;code&gt;terraform apply&lt;/code&gt; to create the resources in your AWS account. If you're using a basic Terraform setup, you might see a prompt like "Do you want to apply these changes?"  Carefully review the prompt and answer "yes" only if you intend to proceed.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Congratulations! You have successfully created a vpc in AWS using Terraform. Open the console, and examine the infrastructure that we just created. Notice that a route table has been created. To ensure basic functionality within your VPC, AWS automatically creates a main route table when you create a VPC.&lt;/p&gt;

&lt;p&gt;Up next: &lt;a href="https://dev.to/ananyadasgupta/terraform-101-creating-secure-subnets-in-your-vpc-553m"&gt;Terraform 101: Creating Secure Subnets in Your&lt;/a&gt;! Stay tuned. 👋&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>aws</category>
      <category>infrastructureascode</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Deploy a web application with AWS Lambda</title>
      <dc:creator>AnanyaDasgupta</dc:creator>
      <pubDate>Wed, 17 Apr 2024 05:59:27 +0000</pubDate>
      <link>https://dev.to/ananyadasgupta/deploy-a-web-application-with-aws-lambda-2km3</link>
      <guid>https://dev.to/ananyadasgupta/deploy-a-web-application-with-aws-lambda-2km3</guid>
      <description>&lt;p&gt;Lambda is the serverless computing solution provided by AWS. &lt;br&gt;
According to official AWS documentation,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“AWS Lambda is a compute service that lets you run code without provisioning or managing servers.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lambda follows the principles of serverless computing. In serverless computing, you don’t have to worry about server and operating system maintenance, capacity provisioning, automatic scaling, and logging.&lt;/p&gt;

&lt;p&gt;AWS Lambda takes care of the provisioning of the underlying infrastructure. It ensures the code is run on a high-availability computing infrastructure and administers the solution.&lt;br&gt;
The developer only needs to provide the codebase in one of the languages supported by AWS Lambda.&lt;br&gt;
Refer to this &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html" rel="noopener noreferrer"&gt;link &lt;/a&gt;for the languages supported by Lambda.&lt;/p&gt;

&lt;p&gt;In this blog post, I’ll guide you through the process of creating a simple Node.js API and deploying it using the Serverless Framework on AWS Lambda.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we dive into the development and deployment process, make sure you have the following tools installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Node.js and npm&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AWS CLI&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Serverless Framework&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Setting Up the Project
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Initialize Node.js Project: Here, I am using VS Code to write the code. You can use any code editor of your choice.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  mkdir lambda-api
  cd lambda-api
  npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Install Dependencies:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install express aws-serverless-express serverless -S&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a folder src in the root directory of your project. Navigate to the src directory and create a file named app.js&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const awsServerlessExpress = require('aws-serverless-express');

const app = express();

app.get('/api/hello', (req, res) =&amp;gt; {
  res.json({ message: 'Hello from your Lambda API!' });
});

module.exports.handler = (event, context) =&amp;gt; {
   awsServerlessExpress.proxy(server, event, context);
};

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

&lt;/div&gt;


&lt;p&gt;This is a simple API that prints the message “Hello from your Lambda API!” on the browser.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Configure serverless.yml:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The serverless.yaml file is a configuration file used in serverless computing environments, particularly with the Serverless Framework.&lt;br&gt;
The serverless.yaml file is used to define various aspects of your serverless application, such as functions, events, resources, and provider-specific settings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service: lambda-api

provider:
  name: aws
  runtime: nodejs14.x

functions:
  app:
    handler: src/app.handler
    events:
      - http:
          path: /
          method: ANY
          cors: true
      - http:
          path: /{proxy+}
          method: ANY
          cors: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s a general overview of theserverless.yaml file:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Service Information:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;service: Specifies the name of your service.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;provider: Defines the cloud provider (AWS, Azure, Google Cloud, etc.).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;runtime: Specifies the runtime for your functions (Node.js, Python, Java, etc.).&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service: lambda-api

provider:
  name: aws
  runtime: nodejs14.x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Functions&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Describes the functions in your serverless application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Specifies the handler (entry point) for each function.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;functions:
      app:
        handler: src/app.handler
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Events:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defines events that trigger your functions (HTTP events, S3 events, etc.).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;functions:
      app:
        handler: src/app.handler
        events:
          - http:
              path: /
              method: ANY
              cors: true
          - http:
              path: /{proxy+}
              method: ANY
              cors: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Configure AWS CLI:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Install the AWS CLI on your machine and configure it with your AWS Access Key ID, Secret Access Key, default region, and output format using the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws configure&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying to AWS Lambda
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`npx serverless deploy --stage dev --region ap-south-1`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This command deploys your Lambda function and provides an endpoint URL. The Serverless Framework automatically configures API Gateway as part of the deployment process.&lt;/p&gt;

&lt;p&gt;API Gateway is a fully managed service by AWS that makes it easy to create, publish, maintain, monitor, and secure APIs at any scale. When you deploy a Serverless Framework project, it creates an API Gateway endpoint, and you can find the URL of this endpoint in the output of the deployment process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3152%2F1%2Apr1283ejFB9TFN4VikQX3g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3152%2F1%2Apr1283ejFB9TFN4VikQX3g.png" alt="Image for serverless deploy output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[https://ixnjy43dxk.execute-api.ap-south-1.amazonaws.com/dev/](https://ixnjy43dxk.execute-api.ap-south-1.amazonaws.com/dev/)&lt;/code&gt; is the API Gateway endpoint that was created for this Lambda function.&lt;br&gt;
You can use the endpoint created for your function to access your API and test your deployed Lambda function. It’s the entry point for your serverless application from the internet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You’ve successfully created a simple Node.js API and deployed it using the Serverless Framework on AWS Lambda. Serverless architecture provides a powerful and scalable solution for various applications, and with the right tools, the development and deployment process becomes seamless.&lt;/p&gt;

&lt;p&gt;Feel free to explore additional features and integrations, and share your experience with the Serverless community. Happy coding!&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>lambda</category>
      <category>aws</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
