Terraform is a tool that makes it easier to set up and manage computing resources, like websites and virtual machines. Instead of manually configuring each resource, Terraform allows you to write instructions that define how the resources should be arranged and managed.
For example, let's talk about it as a blueprint for building a house, but for computer resources. You can plan exactly what you want with a blueprint, and the workers will use that plan to build the house. Terraform works similarly by taking your instructions and using them to set up and manage your computing resources automatically.
So, Terraform simplifies building and organising your computing resources using instructions instead of doing everything by hand.
Pre-Requisite
- AWS Cloud Account (We'll work within the boundaries of the Free Tier)
How Does it Work?
Terraform works by using a declarative approach to manage infrastructure. This means that, instead of specifying how to build the infrastructure step-by-step, Terraform describes the desired end state of the infrastructure and the resources needed to achieve it. The Terraform configuration files are written in the HashiCorp Configuration Language (HCL) and define the resources that Terraform should manage, along with the desired state of those resources.
Terraform maintains a state file that tracks the current state of the infrastructure and compares it to the desired state defined in the Terraform configuration files. When Terraform is run, it generates a plan to make necessary changes to align the infrastructure with the desired state. The plan is then applied to make the changes to the infrastructure, which can include creating new resources, updating existing resources, or deleting resources that are no longer needed.
Terraform uses a variety of plugins, known as providers, to interface with various infrastructure services and cloud providers, such as AWS, GCP, and Azure. The providers allow Terraform to manage the infrastructure resources defined in the configuration files and provide a common set of resource types, regardless of the underlying provider.
Overall, Terraform provides a unified and streamlined way to manage infrastructure using a declarative approach and a centralised state file to track and manage resources. It achieves this while using plugins to interface with various infrastructure services and cloud providers.
Installation
Installing Terraform is pretty simple if you have a package manager like Homebrew, which I'll use for this article.
Run the following commands to set up Terraform on your computer.
brew tap hashicorp/tap
brew install terraform
To check for a successful installation, simply write: terraform -v
Creating Infrastructure with Terraform
Components of a Terraform File (.tf)
A Terraform configuration file consists of several components that define the infrastructure to be managed:
- Provider: The provider block defines the cloud or service provider Terraform will use to manage the infrastructure resources.
- Resource: The resource block defines the infrastructure resources that Terraform should manage, such as a virtual machine, a network, or a storage bucket. Each resource block specifies the type of resource to be created, the configuration options, and the desired state.
- Variable: The variable block allows you to define variables that can be used in Terraform configuration files. This makes it easier to reuse code and manage configuration options centrally.
- Data: The data block allows Terraform to retrieve data from an external source, such as an API or a file, and use that data in Terraform configuration files. This can be useful for retrieving information about existing resources or processing external data in Terraform.
- Output: The output block allows you to specify values that should be displayed when Terraform is run, such as the IP address of a virtual machine or the URL of a website.
These are the main components that make up a Terraform configuration file. By combining these components in different ways, you can define the complex infrastructure and automate the management of your resources with Terraform.
Deploying Servers in AWS
Setup aws-cli
To deploy, we will also need the credentials of our AWS Account present in our system. We will use the aws-cli
tool provided by AWS to do so.
Run the following commands.
brew install awscli
aws configure
Now, check if our user account has been registered with aws-cli
.
Run the following command.
aws iam get-user
Setup Terraform Code
Let's create a directory where we will store the project files.
mkdir -p terraform-projects/aws-ec2-demo
cd terraform-projects/aws-ec2-demo
touch main.tf
Paste the following code into the main.tf
file.
Codesplained
The configuration in the above provided Terraform Code (HCL Code) does the following:
- Defines the AWS provider and the region to use.
- Creates a
VPC
with aCIDR block
of10.0.0.0/16
. - Creates a subnet within the
VPC
with aCIDR block
of10.0.1.0/24
. - Creates a security group that allows incoming
SSH
andHTTP
traffic and allows all outgoing traffic. - Provisions an
EC2
instance with the specified AMI, instance type, subnet, and security group. - Creates an
EBS
volume of10 GB
in the same availability zone as theEC2
instance. - Attaches the
EBS
volume to theEC2
instance.
Use the following commands to start infra deployment (or delete the deployed infrastructure if you have done so already).
Conclusion
In this article, we learned to provision infrastructure on AWS using Terraform. We also learned about the different components that make up the Terraform File.
Please comment below if you have any queries or discover any inaccuracies in the article. I revisit my articles to make corrections as required by updates in the technologies that I use.