<?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: Imesh Ruchira</title>
    <description>The latest articles on DEV Community by Imesh Ruchira (@ruchira).</description>
    <link>https://dev.to/ruchira</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%2F1219921%2Fd0935732-95db-4729-bfba-5631732e644c.jpg</url>
      <title>DEV Community: Imesh Ruchira</title>
      <link>https://dev.to/ruchira</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ruchira"/>
    <language>en</language>
    <item>
      <title>Creating AWS Security Groups with Dynamic Ingress Rules Using Terraform</title>
      <dc:creator>Imesh Ruchira</dc:creator>
      <pubDate>Wed, 10 Jan 2024 12:20:59 +0000</pubDate>
      <link>https://dev.to/ruchira/creating-aws-security-groups-with-dynamic-ingress-rules-using-terraform-20od</link>
      <guid>https://dev.to/ruchira/creating-aws-security-groups-with-dynamic-ingress-rules-using-terraform-20od</guid>
      <description>&lt;p&gt;&lt;strong&gt;What are Terraform Dynamic Blocks&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;In Terraform, dynamic blocks provide a way to generate repetitive configurations dynamically. They are used in resource, data, and provider blocks to handle situations where you need to define multiple nested blocks with similar configurations.&lt;/p&gt;

&lt;p&gt;The dynamic block allows you to generate multiple instances of a nested block within a resource or module, based on a list or map variable. This can help you reduce code duplication and make your Terraform configurations more concise and maintainable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Use the Dynamic Blocks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Terraform provides the dynamic block to create repeatable nested blocks within a resource. A dynamic block is similar to the for expression. Where for creates repeatable resources, like Security group rules, dynamic creates nested blocks within a resource, like ports within a security group. A dynamic block iterates over a child resource and generates a nested block for each element of that resource.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following code shows the configuration of an AWS security group and four open ports. In this example, the ports blocks are written out explicitly, creating repeated code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;provider "aws" {
  region = "us-east-1"  
}

resource "aws_security_group" "example_sg" {
  name        = "example-sg"
  description = "Example Security Group"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  
  }

  ingress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  
  }

  ingress {
    from_port   = 8080
    to_port     = 8080
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That same configuration using a dynamic block is shown below. Replacing the four port blocks with a dynamic block removes repeated attributes, leading to cleaner code that is easier to maintain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;provider "aws" {
  region = "us-east-1"  
}

resource "aws_security_group" "example_sg" {
  name        = "example-sg"
  description = "Example Security Group"


  dynamic "ingress" {
    for_each = [80, 443, 8080, 8000]  

    content {
      from_port   = ingress.value
      to_port     = ingress.value
      protocol    = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The for_each expression in Terraform is a powerful feature that enables the dynamic generation of multiple resources. in this case, &lt;br&gt;
The for_each expression is used to open multiple ports of a security group or configuration block based on the elements of a given collection. It is commonly used to iterate over lists, sets, or maps and create individual ports for each element.&lt;/p&gt;

&lt;p&gt;Got a project that needs some Terraform love? I've got you covered! Check out my Terraform configuration at this link:https:&lt;a href="https://dev.tourl"&gt;//github.com/98ruchira/Terraform-AWS-Security-group-with-Dynamic-block/tree/main&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to begin with Terraform: AWS EC2</title>
      <dc:creator>Imesh Ruchira</dc:creator>
      <pubDate>Mon, 11 Dec 2023 10:25:16 +0000</pubDate>
      <link>https://dev.to/ruchira/how-to-begin-with-terraform-aws-ec2-20jg</link>
      <guid>https://dev.to/ruchira/how-to-begin-with-terraform-aws-ec2-20jg</guid>
      <description>&lt;p&gt;In the current article, we will start to examine in detail the creation of a secure EC2 machine in AWS using Terraform, which will create a VPC, subnets, two EC2 machines, route tables, security groups, network address translation (NAT), and internet gateways (IG).&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture Diagram for WAS secure EC2
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LWCQYuyt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpgmjcq43cosnwyc23qw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LWCQYuyt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpgmjcq43cosnwyc23qw.jpg" alt="Image description" width="700" height="840"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will work at the us-east-1 region, though you can change it at Terraform to your favorite localization – there is no problem with that.&lt;/p&gt;

&lt;p&gt;Inside the AWS region we will create a VPC (virtual private cloud). Then we will create private subnets at the zone – and assign every subnet to a custom route table – this will provide more control in maintaining the security of each private subnet. We will also create public subnets similar as we have it for private networks. We will use Classless Inter-Domain Routing (CIDR) as it is represented at scheme.&lt;/p&gt;

&lt;p&gt;Also, those resources have a security group It has allowed 22 ports. Opening port 22 typically refers to allowing incoming and outgoing traffic on the SSH (Secure Shell) protocol and you enable remote access to a server.&lt;/p&gt;

&lt;p&gt;And, also those resources have two route tables. first route table associated with the public subnet. In that case I used route table to   &lt;/p&gt;

&lt;p&gt;After that, we will add a NAT (Network address translation) gateway that will allow us to provide internet access to components inside private subnets.  And we will also add an internet gateway to the public subnet to get an internet connection.&lt;/p&gt;

&lt;p&gt;Now we have an Internet connection to our EC2 machine, which is in the private subnet so now our EC2 machine is secure.&lt;/p&gt;

&lt;p&gt;Got a project that needs some Terraform love?  I've got you covered! Check out my Terraform configuration at this link:&lt;a href="https://github.com/98ruchira/Terraform-AWS"&gt;https://github.com/98ruchira/Terraform-AWS&lt;/a&gt;&lt;/p&gt;

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