Introduction

In recent years, the concept of infrastructure as code has become increasingly popular among DevOps teams. Terraform is an open-source tool that allows you to define and manage your infrastructure using code. With Terraform, you can easily provision, manage, and version your infrastructure across multiple providers and environments.

Modules and Providers

Terraform is based on two main concepts: modules and providers. Modules are reusable packages of Terraform code that define a set of resources and their dependencies. Providers, on the other hand, are responsible for managing the lifecycle of resources in a specific infrastructure environment, such as AWS or Azure.

How to Use a Provider?

To use a provider, you need to first configure it in your Terraform code. For example, if you want to use the AWS provider, you would need to add the following code to your Terraform file:

provider "aws" {
  access_key = "ACCESS_KEY_HERE"
  secret_key = "SECRET_KEY_HERE"
  region     = "us-west-2"
}

This code tells Terraform to use the AWS provider, and provides the necessary authentication credentials and region information.

How to Execute a Plan?

Once you have written your Terraform code and configured your providers, you can generate an execution plan using the terraform plan command. This plan shows you what changes Terraform will make to your infrastructure based on your code.

$ terraform plan

This command generates a plan and displays a summary of the changes that Terraform will make to your infrastructure. If you are satisfied with the changes, you can proceed to apply the plan.

Here is an example Terraform plan:

# aws_instance.example will be created
+ resource "aws_instance" "example" {
    + ami                          = "ami-0c55b159cbfafe1f0"
    + instance_type               = "t2.micro"
    + key_name                    = "example_key"
    + vpc_security_group_ids      = [
        + "sg-0187cbe6af9e9c5f5",
      ]
    + subnet_id                   = "subnet-0187cbe6af9e9c5f5"
    + associate_public_ip_address = true

    + root_block_device {
        + delete_on_termination = true
        + volume_size           = 20
        + volume_type           = "gp2"
      }
  }

# aws_security_group.example will be created
+ resource "aws_security_group" "example" {
    + arn                    = (known after apply)
    + id                     = (known after apply)
    + name                   = "example"
    + revoke_rules_on_delete = false
    + vpc_id                 = "vpc-0187cbe6af9e9c5f5"

    + ingress {
        + cidr_blocks      = [
            + "0.0.0.0/0",
          ]
        + from_port        = 22
        + protocol         = "tcp"
        + security_groups = []
        + self            = false
        + to_port          = 22
      }

    + egress {
        + cidr_blocks      = []
        + prefix_list_ids  = []
        + security_groups = []
        + self            = true
      }

    + tags = {
        + "Name" = "example"
      }
  }

This plan shows that Terraform will create an AWS instance and a security group. It also shows the specific details of those resources, such as the instance type and the security group rules.

How to Apply a Plan and Deploy New Changes?

To apply a plan and deploy new changes to your infrastructure, you can use the terraform apply command:

$ terraform apply

This command executes the plan and applies the changes to your infrastructure. Terraform will prompt you to confirm the changes before proceeding. Once you confirm, Terraform will provision the new resources and update the state file with the changes.

Conclusion

In conclusion, Terraform is a powerful tool for managing your infrastructure as code. With its modular and provider-based architecture, you can easily define and manage your infrastructure across multiple providers and environments. By following the steps outlined in this post, you can get started with Terraform and start automating your infrastructure today.

Terraform is an infrastructure as code tool, developed by HashiCorp, providing a declarative scripting methodology to automate infrastructure related tasks.