Skip to content
Home » Infrastructure Automation: Terraform to the rescue !

Infrastructure Automation: Terraform to the rescue !

Terraform

What is Infrastructure Automation?

Infrastructure automation is essential for businesses who want to deploy changes safely and on demand. Whether you’re practicing application deployment, system administration, or production operations, infrastructure automation will improve your ability to respond quickly and accurately. Tools like Terraform, Ansible, Chef and Puppet are a powerful ones which helps you automate your infra setup.

I’m going to explain you about Infrastructure Automation, and discuss about Terraform. I am also going to show you how to start with it and move ahead in automating the setup of your infrastructure by utilising the powerful and easy-to-learn tool Terraform.

Benefits of infrastructure automation

Automation gives you reproducibility. If you automate the process of building your application, then anyone can rebuild it on demand. This means that anyone can do a quick check to make sure that it’s still working properly. It also means that people who haven’t looked at the code in six months might be able to quickly fix an urgent bug if they need to.

Taken further, automation makes it possible to share your infrastructure with other groups. For example, if your company uses Chef or Puppet for its servers, then using the same tools for your internal Django site means that any time someone fixes a Django bug, it will get fixed on your servers too without extra work.

Automated tools are also better for newcomers than manual tools. With manual tools, new people typically have to learn how to use them before they can do anything useful. With automated tools like Chef

Advantages

Infrastructure Automation software can significantly improve operational efficiency and performance, reduce total cost of ownership, and streamline service delivery.

Infrastructure automation is an easy way to maintain data integrity across platforms and devices.

What is Terraform?

Terraform is a tool that has been gaining popularity amongst developers and automation enthusiasts alike for the last few years. It was open-sourced by HashiCorp and originated when HashiCorp was still a single product company.

Terraform allows you to write templates that are able to describe your infrastructure. These templates, in turn, are able to create and manage all the necessary infrastructure components that are required to support applications. Whether they’re just an application running on a single server or a highly distributed system consisting of thousands of servers.

Terraform generates an execution plan describing what it will do to reach the desired state. It then executes it to build the described infrastructure.

Terraform is not a “multi-cloud tool” to ease migration among clouds to avoid vendor lock-in. Terraform works with many cloud providers (see the complete list), but it does not abstract their features. Each provider offers a different set of resources that work slightly differently with varying degrees of capability. The goal is instead to provide a common configuration language and workflow that you can use on any cloud provider’s API.

How Terraform works?

The programming language behind Terraform is Go.The code is compiled in a simple binary named terraform. You can deploy this binary on any Operating system and start automating your infrastructure. This binary under the hood makes API calls to other providers like AWS, Azure, Google, Kubernetes and more like them.

Terraform authenticates with the providers using API keys. For eg. With AWS it has access and secret keys. To direct the providers and make it work we write code in small set of files which are referred as Terraform code.

Terraform provides a large number of providers and modules which are ready to use and simplify the infra automation. Thats a relief! You don’t have to write everything from scratch.

Terraform

Terraform Installation

You can download the binary from the Terraform official website. It provides a wide range of Operating systems support. You can download the binary that your operating system supports and and start instantly.

The latest version of Terraform is 1.16 as of writing this article. It provides very good documentation on it main website which will help you to gain knowledge of automating your infrastructure of any of the major cloud providers that you want to use or using already, mentioned here. Hence have a track on the provider list it supporting if you don’t find the one you are looking for.

Terraform commands

If you type in terraform --help in your local terminal wherever you have installed the binary, you will get the full list of commands that you can use to manage your infrastructure.

Whenever you write a new configuration or make changes in an existing one we need to run terraform init. It initializes the configuration directory and installs the provider configurations mentioned in the config files

❯ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v4.2.0...
- Installed hashicorp/aws v4.2.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Next is terraform plan. This will help you see the changes that are going to be deployed as per the configuration written.

❯ terraform plan

Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_instance.app_server will be created
  + resource "aws_instance" "app_server" {
      + ami                                  = "ami-06178cf087598769c"
      + arn                                  = (known after apply)
      + associate_public_ip_address          = (known after apply)
      + availability_zone                    = (known after apply)
      + cpu_core_count                       = (known after apply)
      + cpu_threads_per_core                 = (known after apply)
      + disable_api_termination              = (known after apply)
      + ebs_optimized                        = (known after apply)
      + get_password_data                    = false
      + host_id                              = (known after apply)
      + id                                   = (known after apply)
      + instance_initiated_shutdown_behavior = (known after apply)
      + instance_state                       = (known after apply)
      + instance_type                        = "t3.micro"
      + ipv6_address_count                   = (known after apply)
      + ipv6_addresses                       = (known after apply)
      + key_name                             = (known after apply)
      + monitoring                           = (known after apply)
      + outpost_arn                          = (known after apply)
      + password_data                        = (known after apply)
.
.
.
.

      + root_block_device {
          + delete_on_termination = (known after apply)
          + device_name           = (known after apply)
          + encrypted             = (known after apply)
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + tags                  = (known after apply)
          + throughput            = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = (known after apply)
          + volume_type           = (known after apply)
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

And now, time to action. Next is terraform apply. This will apply the code and create the infrastructure that you have specified in the config files.

❯ terraform apply

Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_instance.app_server will be created
  + resource "aws_instance" "app_server" {
      + ami                                  = "ami-06178cf087598769c"
      + arn                                  = (known after apply)
      + associate_public_ip_address          = (known after apply)
.
.
.
.
.
          + throughput            = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = (known after apply)
          + volume_type           = (known after apply)
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_instance.app_server: Creating...

Wasn’t that easy? It makes the infrastructure creation as easy as cake-walk.

And finally, terraform destroy. You can simply run the destroy command to delete the resources no longer needed.

❯ terraform destroy
terraform destroy
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # aws_instance.app_server will be destroyed
  - resource "aws_instance" "app_server" {
      - ami                          = "ami-06178cf087598769c" -> null
      - arn                          = "arn:aws:ec2:us-west-2:561656980159:instance/ami-06178cf087598769c" -> null
##...

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes
.
.

Conclusion

Of course, Terraform isn’t for everyone, as anyone who has worked with it can attest. But if you are looking to get started with a DIY approach to infrastructure management and cloud computing, Terraform is a great place to start.

If you are looking to automate your infrastructure, Terraform is stable, feature-rich, popular and fits well with other open-source tools. While it can take some time to learn Terraform well, the time spent learning Terraform will be more than worth it.
So, with that, I conclude my look at Terraform. As always, thanks for stopping by, and happy automating!

For more such content, visit DevOps Pod

1 thought on “Infrastructure Automation: Terraform to the rescue !”

Leave a Reply

Your email address will not be published. Required fields are marked *