Infrastructure as Code with Terraform
How infra as code tools can make your cloud life easier
Code:
The script used in this intro is here.
Prerequisites
Some familiarity with what cloud is and an AWS account to follow along(free tier will suffice).
Why you absolutely want this
You want to deploy resources on a cloud provider like AWS, easy right, just go through all their individual menus , select all individual configs and done. Now do it again in a different environment.
The above one was a dev environment, now replicate it in UAT.
Or let's say we wanted to change some configuration and test the outcome. The changed configuration didn't work and now you want to rollback to the previous version. Oh wait, you don't have a version control for this. . And we start to see how tedious it starts to get to manage things.
This is where infrastructure as code tools come into the view. Using a infrastructure as code tool like Cloudformation or Terraform makes life easier for everyone. This article will talk about Terraform.
Terraform allows you to provision and manage resources across multiple cloud providers using .tf files. It has an open source version and is very easy to setup and install.
The installation instructions can be found here. You can verify the installation by the terraform -v command. It should print out a valid terraform version.
Providers
Terraform has the concept of providers, these are analogous to cloud providers in the real world, think AWS, Azure, GCP. The entire list can be found here. We need to use the aws provider. Create a main.tf file and add the following code snippet:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.39.0"
}
}
}
Fire up a terminal and run the terraform init command. You should see that the aws provider gets installed and a few new files are created. Terraform uses these files to maintain a state of the resources.
Provider configuration.
We need to give access to terraform to your aws account.
Add the following snippet to your .tf file. The access key and secret key can be found in the security credentials settings of AWS and should be replaced here.
provider "aws" {
# Configuration options
region = "ap-south-1"
access_key = "accesskey"
secret_key = "secretkey"
}
Resources
Resources are exactly what they sound like, they are the infrastructure you keep in the cloud. Resources in terraform follow the resource "resource_name" "identifier" convention. The ami id used in the below command needs to be obtained from the aws console. We use a t2.micro instance because it's free tier eligible.
resource "aws_instance" "my-first-terraform-ec2" {
ami = "ami-074dc0a6f6c764218" # ap-south-1
instance_type = "t2.micro"
tags = {
Name = "ubuntu"
}
}
Enter the terraform plan command to see what changes will be made by terraform to the environment. Now, run terraform apply once you are happy with the changes. Your EC2 instance now shows up in the aws console.
Make sure to run terraform destroy at the end, else you will have a huge AWS bill soon.
The command will clean up the resources managed by terraform. There is a lot more you can do with terraform, if you want me to write about it, do let me know.
Hope you enjoyed the read. Questions welcome.