A Quick Intro to Terraform
What is Terraform and Why you want IaC?
Terraform is an infrastructure as code (IaC) tool (written in Go) that can provision resources in the cloud from simple declarative code. When building an app in the cloud, you may find yourself using the graphical user interface (GUI) of your preferred Cloud Service Provider, clicking buttons and paying for products just like you would on an e-commerce website. But the only drawback of this approach is that it’s complete and total chaos when you might click thousands of different buttons to get you Virtual machine (VM) configured properly. Now imagine if you want to repeat this procress in the future. Yikes! Good luck clicking those buttons again.
Features of Terraform
Terraform provides a way to build, change, and version infrastructure safely and efficiently by representing your infrastruture and all the related settings using Hashicorp configuration language or optionally JSON. Think of it like a human readable blueprint that can execute and automate everything you do in cloud. It’s was open source however now Hashicorp has made it Business Source License (BSL).
Get Started
The first step to get started is to install the CLI on your local system. You can check out how to do that over here. If you are a Windows user, I highly recommend installing WSL (Windows Subsystem for Linux) which you can find out how to do over here.
Once installed, then in your project folder create a file ending in .tf
like main.tf
. You can refer the code block provided below to understand what is going on.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.43.0"
}
}
}
provider "aws" {
# Configuration options
region = "us-west-2"
access_key = "my-access-key"
secret_key = "my-secret-key"
}
resource "aws_ami_from_instance" "example" {
name = "terraform-example"
source_instance_id = "i-xxxxxxxx"
}
At the top you have Terraform block which controls global settings for terraform itself. Most importantly you can write your providers
in this block which are like plugins that connect to your preferred CSP (AWS, GCP, Azure, etc).
Below that you have a provider
block which gives terraform the neccessary credentials to connect to our AWS account. From there you can create actual cloud infrastructure using a resource
block. It takes a type as its first argument which corresponds to a product on AWS (in our case AMIs) then a custom name to identify it as the second argument. Inside the block we can customize the settings of the resource like its source instance id and anything else.
Launching the .TF file
Now that our infrastruture is defined as code, we can run terraform init
to initialize the project. Then we can use terraform apply
to make the actual changes to AWS. It’ll require a confirmation before making the changes to AWS. Then after it’s complete the terraform show
command can be used to inspect the current state of the infrastructure. What’s awesome is that as we add more resources to the configuration terraform keeps track of the changes or diff similar to a version control system like Git
terraform init
terraform apply
terraform show
Now that you have successfully provisioned the services using IaC, you can tear it all down using terraform delete
command as the inverse of apply, to delete everything in your configuration file.
This is just the start
In conclusion, Terraform presents an innovative solution to infrastructure as code, allowing for the seamless provisioning, management, and deployment of resources across various cloud platforms. With its declarative syntax and robust ecosystem, Terraform simplifies the orchestration of complex infrastructure setups, enhancing scalability, repeatability, and efficiency. Embracing Terraform empowers teams to automate infrastructure management, accelerate deployment cycles, and foster collaboration between development and operations. As organizations increasingly adopt cloud-native approaches, Terraform emerges as a pivotal tool in modernizing IT operations and driving digital transformation.
To delve deeper into Terraform and unlock its full potential, here are some essential resources:
-
Terraform Documentation: The official Terraform documentation provides comprehensive guides, tutorials, and references for beginners and advanced users alike. It covers everything from installation and basic usage to advanced topics such as modules, state management, and provider development. Terraform Documentation
-
Terraform Learn: Terraform Learn offers interactive tutorials and hands-on labs to help users get started with Terraform quickly and effectively. It provides guided exercises and real-world scenarios to reinforce concepts and best practices. Terraform Learn
-
Terraform Registry: The Terraform Registry hosts a vast collection of modules and provider plugins contributed by the community and ecosystem partners. Users can browse, search, and leverage reusable infrastructure components to accelerate their projects and streamline development workflows. Terraform Registry
-
Terraform Up & Running: Written by Yevgeniy Brikman, “Terraform: Up & Running” is a comprehensive guidebook that explores Terraform’s core concepts, best practices, and real-world use cases. It offers practical insights and strategies for effectively managing infrastructure as code in production environments. Terraform: Up & Running
By leveraging these resources, individuals and teams can enhance their proficiency with Terraform, unlock new capabilities, and drive greater agility and innovation in their infrastructure management practices.