{{announcement.body}}
{{announcement.title}}

Intro to Terraform for Infrastructure as Code

DZone 's Guide to

Intro to Terraform for Infrastructure as Code

Learn how to codify your infrastructure configuration with Terraform for more standardization and the ability to provision new environments quickly.

· DevOps Zone ·
Free Resource

Infrastructure as code (commonly known as IaC) is a methodology wherein instead of building and configuring infrastructure manually, we codify it and run that code to perform the tasks.

With this approach, we build what is known as "immutable infrastructure." The advantage of this approach is that we can put up and tear down the entire infrastructure as if we are doing it on our local machines. This brings standardization and also ensures that the knowledge of what has been configured in the infrastructure is not in someone's memory, but in the source code. This approach also allows us to provision new environments quickly.

There are many tools and frameworks available for provisioning infrastructure on the cloud. One of the most popular cloud agnostic tools is Terraform. We will learn about some basic nuances of Terraform (with target cloud as AWS) in this article.

Terraform deals with files with an extension of '.tf'. A simple terraform file is shown below. Note the syntax, the variables should be referred with a prefix 'var' and variables can have default values.

variable "name" {
  type = "string",
  default ="Raghu"
}

output "developer"{
  value="${var.name}"
}

Terraform init

Before we can start running this file, we need to initialize Terraform. Terraform init is used to initialize a new or existing Terraform configuration. The command to initialize Terraform is  terraform init.

Terraform Plan

Now, to run this file, we need to create a plan first. A plan generates and shows an execution plan by showing what resources it is going to create. It is always advisable to create a plan as it gives an opportunity to review what resources are being created.
We can save the plan to a file and then apply changes using the plan.

terraform plan -out first.terraform
terraform apply first.terraform

When we run the "apply" command, Terraform will apply the changes in the target environment and will provide an output of how many resources and what resources have been created.

Terraform Providers

Providers are responsible for managing the lifecycle of a resource, i.e creating a resource or updating it, etc. There are a lot of cloud providers which are implemented by Terraform. It supports AWS and Google Cloud, to name a few.

Let's look at a sample provider configuration for AWS. Here is an illustration of provider configuration for AWS. You can export the AWS secret and key as environment variables.

provider "aws" {
   region = "us-east-1"
}
resource "aws_instance" "my-vm-automated" {
   ami = "ami-a4c7edb2"
   instance_type = "t2.micro"

   tags {
     Name = "my-vm-automated"
   }
}

Note that we are trying to create an EC2 instance of type t2.micro and tagged as 'my-vm-automated'.

Terraform State

When you apply changes, you would notice that a 'terraform.tfstate' file is created. This file is used by Terraform to save the current state of the infrastructure. This is used when we run subsequent files and hence terraform will know what changes to apply. It is advisable to commit this state file to source code repository, as for subsequent runs, you would need the current state of the infrastructure.

Destroying Resources

We can destroy all the resources which have been created by using the terraform destroy command.

Conclusion

Terraform has many more powerful features and is popular due to the wide spectrum of cloud providers it supports. Next time you want to create an infrastructure on AWS, resist the temptation of using AWS CLI or GUI....happy Terraforming!

Topics:
devops ,automation ,tutorial ,infrastructure as code ,terraform

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}