Day 65 Task: Working with Terraform Resources

Day 65 Task: Working with Terraform Resources

ยท

3 min read

๐Ÿ”ถ Understanding Terraform Resources

A resource in Terraform represents a component of your infrastructure, such as a physical server, a virtual machine, a DNS record, or an S3 bucket. Resources have attributes that define their properties and behaviors, such as the size and location of a virtual machine or the domain name of a DNS record.

When you define a resource in Terraform, you specify the type of resource, a unique name for the resource, and the attributes that define the resource. Terraform uses the resource block to define resources in your Terraform configuration.

๐Ÿ”ถ Task 1: Create a security group

To allow traffic to the EC2 instance, you need to create a security group. Follow these steps:

In your main.tf file, add the following code to create a security group:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.17.0"
    }
  }
}

provider "aws" {
  region = "eu-north-1"
}

resource "aws_security_group" "web_server" {
  name_prefix = "web-server-sg"

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

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

 egress {
        from_port  = 0
        to_port    = 0
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
        }
}
  • Run Terraform init to initialize the Terraform project.

  • Run Terraform apply to create the security group.

๐Ÿ”ถ Task 2: Create an EC2 instance

  • Now you can create an EC2 instance with Terraform. Follow these steps:

  • In your main.tf file, add the following code to create an EC2 instance:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.17.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_security_group" "web_server" {
  name_prefix = "web-server-sg"

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

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

 egress {
        from_port  = 0
        to_port    = 0
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
        }
}

resource "aws_instance" "web_server" {
  ami           = "ami-0fe8bec493a81c7da"
  instance_type = "t3.micro"
  key_name      = "terraform"

security_groups = [
    aws_security_group.web_server.name
  ]

tags = {
        Name= "InstanceByNilesh"
        }
  user_data = "${file("index.sh")}"
}

Now create an index.sh file in the same directory

#!/bin/bash
sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
echo "<html><body><h1>Welcome to my website, My name is Nilesh Sahare!</h1></body></html>" > /var/www/html/index.html

Note: Replace the ami and key_name values with your own. You can find a list of available AMIs in the AWS documentation.

Run terraform apply to create the EC2 instance.

๐Ÿ”ถ Task 3: Access your website

  • Now that your EC2 instance is up and running, you can access the website you just hosted on it.

  • Now Open the web browser by using the new instance Public IP address.

Happy Learning :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #AWS

ย