Spinning up Terraform Configuration – 2

terraform
Reading Time: 4 minutes

Hi Readers, In previous blog we discussed the basics of terraform and understood the cloud infrastructure. Now here we will understand how to spin up our first terraform configuration. In addition to that we will also see how to setup terraform in our local system.

Terraform installation

  • Terraform is available for all Operating systems i.e for Mac OS, Windows and Linux.
  • HashiCorp distributes Terraform as a binary package.
  • We can also install Terraform using popular package managers.
  • For this blog you can simply run the below script(this is for Linux OS) or install from here.
#!/bin/bash
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
echo "=======INSTALLED TERRAFORM==========="
sudo apt-get update && sudo apt-get install terraform

Writing the first configuration

Let’s start,

In this blog series we will be using AWS for demonstration. Before writing your first configuration make sure to have AWS account already set up.

instance.tf
resource "aws_instance" "example" {
  ami           = var.AMIS[var.AWS_REGION]
  instance_type = "t2.micro"
  # name = "sample_instance"
}
provider.tf
provider "aws" {
  access_key = var.AWS_ACCESS_KEY
  secret_key = var.AWS_SECRET_KEY
  region     = var.AWS_REGION
}
terraform.tfvars
AWS_ACCESS_KEY = "XXXXXXXXXXXXXXXXXXXX"
AWS_SECRET_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
AWS_REGION="ap-south-1"

You can find your AWS_ACCESS_KEY and AWS_SECRET_KEY from My Security Credentials (see below). Make sure you do not expose these keys to public vcs(Github, BitBucket etc.)

vars.tf
variable "AWS_ACCESS_KEY" {
}

variable "AWS_SECRET_KEY" {
}

variable "AWS_REGION" {
  default = "ap-south-1"
}

variable "AMIS" {
  type = map(string)
  default = {
    ap-south-1 = "ami-0860c9429baba6ad2"
  }
}

Refer Amazon EC2 AMI Locator for locating AMI-ID and other details for your zone.

versions.tf
terraform {
  required_version = ">= 0.14"
}

Terraform commands to apply configurations

terraform init

This will create following files and folders,

.terraform
  • The .terraform directory is a local cache where Terraform retains some files it will need for subsequent operations against this configuration.
  • In addition the contents of .terraform directory are not intended to be included in version control.
.terraform.lock.hcl
  • Terraform configuration refers to two different kind of external dependency Providers and Modules.
  • Both of these dependency types can be published and updated independently from Terraform itself and from the configurations that depend on them.
  • However Terraform must determine which versions of those dependencies are potentially compatible. And with the current configuration and which versions are currently selected for use.
  • Above all Version constraints within the configuration itself determine which versions of dependencies are potentially compatible.
  • But after selecting a specific version of each dependency Terraform remembers the decisions it made in a dependency lock file so that it can (by default) make the same decisions again in future.
terraform plan -out file.terraform

Above command will create following files,

file.terraform
  • This file.terraform contains the execution plan which is expected to reflect in actual after running terraform apply.
  • We can run terraform plan without -out flag also and this will not create file.terraform. But this is a good practice to store the plan to other file and then go for apply.
terraform apply file.terraform

This will create following files,

terraform.tfstate
  • Terraform stores the state about your managed infrastructure and configuration.
  • In addition Terraform uses this state to map real world resources with our configuration and keeps track of metadata and improves performance for large infrastructure.
  • The state is stored by default in a local file named terraform.tfstate, but it can also be stored remotely, which works better in a team environment.

Verifying the Configuration on AWS Console

Destroying the applied configuration

terraform destroy

This will destroy the resources you just created using terraform apply. Therefore If you are applying the configuration for practice purpose, it is always advised to destroy the configuration after every apply. This will protect you from getting monetarily charged from AWS.

Congratulations!!! You just successfully applied your first terraform configuration .However If you have any doubt, feel free to contact me at nitin.mishra@knoldus.com. In next blog we will try to configure more complex infrastructure using terraform.

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and if you feel, give me suggestions on scope of improvements.

Written by 

Nitin Mishra is a Software Consultant at Knoldus Software LLP. He has done MCA from GGSIPU and completed Bachelor of Science in Computer Science from Delhi University. He is a tech enthusiast with good knowledge of Java. He is majorly focused in DevOps practice. On personal front he loves to travel mountains and writes poetry.

2 thoughts on “Spinning up Terraform Configuration – 24 min read

Comments are closed.