Complete Terraform Tutorial Part – 1 – Introduction

Terraform is an Opensource tool that will provide Infrastructure as code. Infrastructure as Code is a system that stores the machine-readable file to manage, provision, and configure the Software Infrastructure from Cloud computing. So, Terraform will define your Cloud Infrastructure with some popular Cloud provides including AWS, Azure, Google Cloud Platform, DigitalOcean, CloudFlare, and more. In this article, we will see How Terraform works and Complete Terraform Tutorial with Example.

Why Terraform?

Before getting into the Tutorial, we should understand Why Terraform is needed in the first place. Creating and managing application instances in Datacenters is a really crucial job and it cannot even afford a small mistake or misconfiguration. Wait! even application developers do mistakes with their application codes but how they are managing it to avoid big downtime or issue? Yes, They have everything coded and everyone has their own versions of code which can be reviewed and approved!. Likewise, creating code for Infrastructure will help misconfiguration, and most importantly, it will automate the way long infrastructure processes. Now, Terraform provide such a platform that will store the configuration of infrastructure with configuration, provision, and manage feature as code. We call that as Infrastructure as Code (IaC).

So, Terraform gives following Advantage over the manual Infrastructure Management.

  • Manual Needs many Human Resources which can be Managed by a single Terraform Platform.
  • Since we need fewer human resources, the Cost of Infrastructure management can be reduced drastically.
  • Misconfiguration and Manual errors can be taken away by Terraform
  • Scaling or High Availablity of Application stack within Infrastructure can be easily achieved with Terraform.
  • As Infrastructure setup is available as Configuration Files, It can be Documented and Reviewed every version of the changes.
  • No Blame Games in Infrastructure Configurations. It can be traced back and Identified easily.

Now, Let’s start the tutorial with Installing Terraform.

Installing Terraform

Terraform can be easily installed using the binary for the respective operating system. Let’s see one by one.

Binaries can be downloaded from here: https://www.terraform.io/downloads.html.

Unzip and keep the downloaded files.

Beginning with Terraform

Now we have installed the Terraform into your machine. The Terraform comes with the Command-Line tool so it will work well with CLI. Just like git init, we have terraform init which will read the .tf file and install the necessary plugin and library to start. In our case, we will create main.tf.

$ nano main.tf

which will have the following content for creating an AWS instance and install Nginx server in it.

provider "aws" {
  region     = "us-west-1"
  access_key = "accd32c5dcs5c225624fs53fs5c352"
  secret_key = "fsfs42fs7f36s2cw5ecs2xs7225"
}
resource "aws_instance" "digitalvarys" {
	ami = "ami-5a1f6d6c6w752"
	instance_type = "t2.nano"
	provisioner "remote-exec" {
		inline = [
		  "sudo apt-get -y update",
		  "sudo apt-get -y install nginx",
		  "sudo service nginx start",
		]
	}
}

So, Copy the above content and make a file called main.tf in your working directory. Then, run following command:

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.62.0...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 2.62"

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.

So, the above command (terraform init) will check for the available plugins and downloads. In this scenario, It is downloading the aws plugin as we are going to create the infrastructure with AWS. However, this is only preparation.

We have to see our plan of action before applying it to the actual AWS account. terraform plan will help us in terms of review and verification of what we are going to do. To do so, just simply follow the command below.

$ terraform plan
...
+ aws_instance.digitalvarys
    ami:                         "ami-5a1f6d6c6w752"
    ...

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

Also, terraform plan will have a standard format which can be fed into another script or something to automatically verify the action.

Then, Simply Run the following code to make the changes apply on the AWS account.

$ terraform apply
...
aws_instance.digitalvarys: Creating...
  ami:                         "" => "ami-5a1f6d6c6w752"
  ...
aws_instance.digitalvarys: Creation complete (ID: i-c2d4ba5c1df5ac6b81d)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
...

terraform apply will apply all the instructed changes into the AWS account. In our case, It will create an EC2 instance and install Nginx into it. In the middle of the execution, it might prompt you with yes or no to proceeding further.

Just like the creating resources, we have operations to delete or destroy the same by simply passing terraform destroy.

$ terraform destroy
...
- aws_instance.digitalvarys
...
Destroy complete! Resources: 1 destroyed.

Just like terraform apply, terraform destroy will prompt you to proceed and you need to enter yes to continue.

So, Terraform basically reads the .tf file from the current directory and execute the instructions one by one according to the command you are passing. That’s all for basics.

Conclusion

Just to recall, we have discussed why the Terraform is needed, how to install the Terraform on various operating systems, and finally some basic command which is enough to understand the working concept of the Terraform. In our Upcoming series articles, we will discuss more How to write Terraform Configuration Files with the basic components and syntax of the same. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps, and App Development.

5 thoughts on “Complete Terraform Tutorial Part – 1 – Introduction”

  1. I get this error when I run “terraform plan”:
    “Error: configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: operation error STS: GetCallerIdentity, https response error StatusCode: 403, RequestID: 5616ef9c-d60c-4c4c-80dc-c0e25109a15a, api error InvalidClientTokenId: The security token included in the request is invalid.”

    What can I do?

Leave a Reply