Understand Complete Terraform in 5 Mins!

Divesh
5 min readMay 4, 2023

--

HashiCorp Terraform!

Terraform is an open-source infrastructure as code (IaC) tool used to manage and provision cloud resources. It was developed by HashiCorp and was first released in 2014. It onself of a declarative language to describe the desired state of infrastructure, and it automatically manages the creation, modification, and deletion of resources to achieve that desired state. The intresting is that TF supports a wide range of cloud providers, including AWS, Azure, Google Cloud, and many others. It can also manage resources on-premises and across hybrid cloud environments. TF supports reusable modules, which allow teams to define and share infrastructure configurations as reusable components. This can help reduce duplication of effort and improve collaboration across teams.

One of the key benefits of using Terraform is its ability to create reproducible infrastructure. By defining the desired state of infrastructure as code, teams can version control and collaborate on infrastructure changes in the same way they do with application code.

Terraform uses a plugin-based architecture to support a wide range of cloud resources. Each provider has its own set of plugins that map Terraform resources to the corresponding cloud resources.Terraform resources represent cloud resources, such as virtual machines, databases, or storage buckets. Each resource has its own set of properties that can be configured, such as the size or type of the resource.Terraform uses a dependency graph to determine the order in which resources should be created, modified, or deleted. This ensures that resources are created and managed in the correct order to avoid any dependency issues.

Terraform can also be used to manage infrastructure state. When Terraform creates or modifies resources, it stores the current state of the infrastructure in a state file. This file is used to track changes to the infrastructure over time. Main fact is that it can also work with existing infrastructure which is not small thing. It can import existing resources into the Terraform state file, allowing teams to manage resources that were created outside of Terraform.

Terraform can also be used to manage environment-specific configurations, such as staging, production, or development environments. By using separate Terraform configurations for each environment, teams can ensure that each environment is consistent and reproducible.

Terraform provides several ways to validate and test infrastructure configurations. It includes a built-in syntax checker to ensure that configurations are syntactically correct. It also supports third-party tools for more advanced validation and testing. Integration with other tools, such as version control systems, continuous integration and delivery (CI/CD) pipelines, and monitoring tools. This makes it easy to incorporate Terraform into existing workflows.

Terraform therefore can be run locally or in the cloud. When running Terraform locally, it interacts with the cloud API to create and manage resources. When running Terraform in the cloud, it can be run in a serverless or containerized environment. Terraform is highly extensible. It has a plugin-based architecture that allows users to create custom providers or extend existing ones. This can be useful for integrating with proprietary or non-standard cloud resources. Terraform provides detailed logs and error messages, making it easy to troubleshoot issues. It also supports rollbacks, which allows teams to revert infrastructure changes if there are any issues.

Terraform is constantly evolving. The HashiCorp team releases frequent updates and bug fixes, and the Terraform community contributes to the development of new features and plugins.

In simple word, Terraform is an infrastructure as code tool that provides a simple and flexible way to manage cloud resources. It supports a wide range of cloud providers, can manage existing infrastructure, and provides several features for testing, validation, and collaboration. Its declarative language and dependency graph ensure that infrastructure is created and managed in the correct.

Some of the key features of Terraform include:

  1. Multi-cloud support:

Terraform supports multiple cloud providers, such as Amazon Web Services, Microsoft Azure, Google Cloud Platform, and many others.

2. Infrastructure as code:

Terraform allows users to define infrastructure resources in a declarative configuration language, which can be version controlled, shared, and reused.

3. Dependency management:

Terraform automatically manages the dependencies between infrastructure resources, ensuring that they are created and destroyed in the correct order.

4. Plan and apply:

Terraform provides a “plan” command that allows users to preview the changes that will be made to the infrastructure before they are applied, helping to prevent accidental changes or errors.

5. State management:

Terraform maintains a state file that records the current state of the infrastructure resources, allowing it to track changes and apply only the necessary modifications.

There are also some hidden features that are less well-known

  1. Data Sources — Data sources allow you to retrieve information about an existing resource, which can be useful when you need to reference that information in your configuration. For example, you could use a data source to retrieve the ID of an existing AWS VPC, and then use that ID in another resource block.
  2. Local Exec Provisioner — This provisioner allows you to execute a command on the machine running Terraform, typically after the resource has been created. This can be useful for tasks such as configuring software or running tests.
  3. Count Parameter — The count parameter allows you to create multiple instances of a resource, each with a unique name. This can be useful when you need to create a large number of resources with similar configuration.
  4. Dynamic Blocks — Dynamic blocks allow you to generate multiple blocks of configuration based on a list or map variable. This can be useful for creating multiple subnets or security groups based on a list of IP addresses.
  5. Terraform Graph — Terraform graph generates a visual representation of the dependency graph of your infrastructure. This can be useful for understanding the relationships between resources and identifying potential issues.
  6. Terraform State Management — Terraform has a built-in state management system that tracks the state of your infrastructure. While this feature is well-known, many people don’t realize the full extent of its capabilities, such as remote state storage and locking.

Overall, Terraform is a very powerful tool with many features, and the above list is just a small sample of the many hidden features available to its users.

bellow easy example to create ec2 server with apache installation:

# Configure the AWS provider
provider "aws" {
region = "us-west-2"
}

# Create an EC2 instance
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
key_name = "example-key"
subnet_id = "subnet-0123456789abcdef"

# Create a security group for the instance
vpc_security_group_ids = [aws_security_group.example.id]

# Install Apache using a shell script
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
EOF
}

# Create a security group for the instance
resource "aws_security_group" "example" {
name_prefix = "example-sg"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
  1. Run terraform init

2. Run terraform plan

3. Run terraform apply

Please comment with output if you face any error I will be happy to help!

Thank you for reading.

--

--

Divesh
Divesh

Written by Divesh

An Architect, A DevOps Engineer, An Automation master, A Kubernetes Security Specialist and always willing to help because helping others is my favourite task.

No responses yet