Working with Terraform Resources ๐Ÿš€

Working with Terraform Resources ๐Ÿš€

#day59 of #90daysofdevops

ยท

5 min read

Introduction:)

Welcome to #Day59 of #90DaysOfDevOps journey!
Today, we'll delve into key AWS concepts using Terraform, focusing on creating a fundamental AWS elementโ€”the security group. Learn how to define security groups with Terraform, ensuring robust network security.

Our journey continues with EC2 instances, where we'll showcase Terraform's prowess in creating and configuring dynamic instances. From key parameters to user data scripts, discover how Terraform simplifies infrastructure provisioning on AWS.

Understanding Terraform Resources

In Terraform, a resource is like a building block for your infrastructure โ€“ it could be a server, a virtual machine, or even a DNS record.

Key Points:

  • Resources are the things Terraform can create, change, or delete.

  • They come with attributes defining their unique traits. For a VM, it could be size and location.

  • In your Terraform file, you use a "resource block" to tell Terraform what to create.

  • This block specifies the resource type, a name, and its essential attributes.

Example:

resource "aws_instance" "example" {
  ami           = "ami-12345"
  instance_type = "t2.micro"
}

In Short:

  • Resources = Infrastructure components.

  • Resource block = How you tell Terraform to manage those components.

It's Terraform's way of making complex infrastructure feel like building with LEGO bricks. ๐Ÿ—๏ธ๐Ÿ’ป #Terraform #InfrastructureAsCode


Task 1: Create a security group

Certainly! Let's break down the steps for creating a security_group.tf file and adding the content:

  1. Create security_group.tf File:

    Create a new file named security_group.tf in your Terraform project directory.

  2. Declare Security Group Resource:

    Add the following code to declare a security group in the security_group.tf file. This security group allows incoming SSH traffic (port 22) from any source.

     resource "aws_default_vpc" "default_vpc" {
    
     }
    
     resource "aws_security_group" "allow_ssh" {
       name        = "allow_ssh"
       description = "Allow SSH inbound traffic"
    
       # Using default VPC
       vpc_id = aws_default_vpc.default_vpc.id
    
       ingress {
         description = "SSH from any source"
         from_port   = 22
         to_port     = 22
         protocol    = "tcp"
         cidr_blocks = ["0.0.0.0/0"]
       }
    
       tags = {
         Name = "allow_ssh"
       }
     }
    

    Explanation:

    ingress block in the provided Terraform script defines an inbound rule for an AWS security group

    cidr_block: AWS security groups, the CIDR block is used to define the range of IP addresses allowed to access or be accessed by the resources associated with the security group.

  3. Run Terraform Commands:

    Open your terminal, navigate to the directory containing your Terraform files (including security_group.tf), and run the following commands:

     terraform init
     terraform apply
    

  4. Review and Confirm:

    Terraform will show you the changes it intends to make. Type "yes" to confirm and apply the changes.


Task 2: Create an EC2 instance

Creating the key pair:

Firstly, we have to create the keypair in your machine:

  1. Navigate to the path

     cd .ssh
    
  2. Generate the Key Pair:

     ssh-keygen
    

    Give the name of your key. I gave the terrakey name to my key

Now make the new file with anyname.tf . I make the file with name awskeypair.tf

Add the content to this file:

resource "aws_key_pair" "key" {
  key_name   = "terrakey" 
  public_key = file("~/.ssh/terrakey.pub") # Path to your public key file
}

Note: In the public_key we simply paste the public key of give the path of that key here

Now to create the EC2 instance

  1. Add the below resource block in the main.tf

     # Define the EC2 instance resource
     resource "aws_instance" "my_instance" {
         ami           = "ami-0c7217cdde317cfec"
         instance_type = "t2.micro"
         key_name = aws_key_pair.key.key_name
         security_groups = [
             aws_security_group.allow_ssh.name
             ]
         tags = {
             Name = "MyEC2Instance"
         }
    
     user_data = <<-EOF
       #!/bin/bash
          sudo apt update -y &&
          sudo apt install -y nginx
          echo "Welcome to my website!" > /var/www/html/index.html
        EOF
    
     }
    

    Explanation:

    1. Heredoc (<<-EOF):

      • The <<-EOF syntax is a way to create a multi-line string or block of text in Bash. It allows you to include multiple lines of text without the need for escaping characters.
    2. Bash Script:

      • #!/bin/bash: This line is known as a shebang, specifying that the script should be interpreted using the Bash shell.

      • echo "<html><body><h1>Welcome to my website!</h1></body></html>" > index.html: This line creates an HTML file named index.html with a simple welcome message.

      • nohup python -m SimpleHTTPServer 80 &: This line starts a Python SimpleHTTPServer on port 80 in the background (&). This allows the instance to serve the content of the index.html file over HTTP.

    3. EOF (End of File):

      • EOF marks the end of the Heredoc block. It should match the delimiter used at the beginning (<<-EOF).
  2. Apply the changes using the terraform apply command.

     terraform apply
    

  3. Verify the changes


Task 3: Access your website

  • Open a web browser and enter the public IP or DNS name of your EC2 instance.

  • You should see your website's content displayed in your browser.


Conclusion:)


Connect with me:)

Thank you for diving into this blog with me! I trust you found the information both helpful and enlightening. To stay updated on the latest in DevOps ๐Ÿš€, make sure to follow me. Remember, staying informed means staying ahead in the dynamic world of DevOps!

Feel free to connect with me on:

LinkedIn

Twitter

GitHub

For more updates and engaging discussions on DevOps, let's connect! ๐Ÿš€ #DevOpsCommunity

ย