Day 65 - Terraform Resources

Day 65 -  Terraform Resources

In previous blog, we have seen about resources, refer link: Terraform Resources.

Let's dive deep into the Terraform Resources.👇

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.

A resource block typically includes the following elements:

  • Resource Type: Specifies the type of resource being defined, such as “aws_instance” for an Amazon EC2 instance.

  • Resource name: Provides a unique name for the resource within your configuration.

  • Resource configuration: Specifies the desired settings and attributes for the resource, such as the instance type, disk size, or access control rules.

Note: Resource names must start with a letter or underscore, and may contain only letters, digits, underscores, and dashes.

Characteristics of Terraform Resources

Key characteristics of Terraform resources include: -

  1. Declarative Configuration: Resources are defined in Terraform configuration files, specifying the desired state of infrastructure rather than imperative steps to achieve that state.

  2. Provider Compatibility: Terraform supports various providers, including major cloud providers like AWS, Azure, and Google Cloud Platform, as well as other services like Kubernetes, Docker, and GitHub.

  3. Dependency Management: Resources can depend on each other, allowing Terraform to automatically determine the order in which resources should be created or updated.

  4. State Management: Terraform maintains a state file that records the current state of managed infrastructure. This state file enables Terraform to detect drifts between the desired state defined in configuration files and the actual state of infrastructure.

  5. Idempotency: Terraform applies changes to infrastructure in an idempotent manner, ensuring that applying the same configuration multiple times yields the same result as applying it once.

  6. Versioning and Collaboration: Terraform configuration files can be versioned using version control systems like Git, enabling collaboration among team members and tracking changes to infrastructure over time.

TASKS

TASK 1: Create a security group

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

Step 1: Create a terraform.tf and pass the aws provider.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

Step 2: Create a provider.tf and put the selected AWS Region that you want to create a security group.

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

Step 3: Now, Create main.tf file and add the following code to create a security group:

resource "aws_security_group" "web_server" {
  name_prefix = "web-server-sg"
  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"] 
   } 

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

Step 4: Now the first step is to initialize the working directory with the necessary plugins and modules by executing terraform init .

Step 5: Once you initialize all the plugins required for AWS, now execute the terraform plan which will create an execution plan by analyzing the changes required to achieve the desired state of your infrastructure.

Step 6: Finally, use the command terraform apply it will apply the changes to create or update resources as needed.

Step 7: Check whether the security group is created or not.

TASK 2: Create an EC2 instance

Now, We can create an EC2 instance with Terraform. Follow these steps: -

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

resource "aws_instance" "web_server" {
  ami = "ami-0c7217cdde317cfec"
  instance_type = "t2.micro"
  key_name = "terraform"
  tags = {
      Name = "TerraformTestServer1"
  }
  security_groups = [
    aws_security_group.web_server.name
  ]

 user_data = <<-EOF
  #!/bin/bash
  sudo apt-get update -y
  sudo apt-get install apache2 -y
  sudo systemctl start apache2
  sudo systemctl enable apache2
  sudo systemctl restart apache2
  sudo chmod 766 /var/www/html/index.html
  sudo echo "<html><body><h1>Welcome to my website!</h1></body></html>" >/var/www/html/index.html    
 EOF
}

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

Step 2: Now the first step is to initialize the working directory with the necessary plugins and modules by executing terraform init .

Step 3: Once you initialize all the plugins required for AWS, now execute the terraform plan which will create an execution plan by analyzing the changes required to achieve the desired state of your infrastructure.

Step 4: Finally, use the command terraform apply it will apply the changes to create or update resources as needed.

You can see the instance has been created.

TASK 3: Access your website

Now that your EC2 instance is up and running, you can access the website you just hosted on it. Follow these steps: -

Step 1: Go to the newly created EC2 instance and copy Public IPv4 address that is created by terraform.

Step 2: Browse http://<Public_IPv4_Addr> of your instance. You can see the webpage.

Step 3: Once you are done with the newly created instance we can use terraform destroy command which will delete the complete infrastructure.

Step 4: Now from EC2 Instance, we can verify that the newly created EC2 instance is in the terminated state.

Conclusion

In Conclusion, Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records.

These resources are defined in Terraform configuration files using a declarative language called HashiCorp Configuration Language (HCL) or JSON.

Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨

*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊

~Smriti Sharma✌