Terraform Basic Architecture
Terraform Basic Architecture
info
Terraform
Terraform is an open-source, infrastructure as code (IaC), software tool
created by HashiCorp.
All one needs to do, is describe the type of resource needed and its count.
Terraform will analyze the state of all the resources it manages, and
execute the required actions and API calls to set the cloud resources to the
desired state.
1. Providers
Terraform interacts with over 1000 cloud platforms and services using
their APIs. These interactions are made possible by Terraform plugins,
which are called providers.
Once specified, each provider shows all the different resources and
data types available for use.
2. Resources
Each resource block can define one or more infrastructure objects, for
example compute instances or private networks. Resources can be
composed of objects from different providers as well.
3. Terraform State
Terraform records and logs information about all the resources it creates
and manages in a file, called the state file.
This state file acts as a source of truth for Terraform, and maps the objects
in its configuration to the real world (remote instances and infrastructure).
It uses this state file and determines all the changes required to align the
infrastructure with the declared configuration.
Install Terraform
Terraform is written in the Go programming language and doesn’t have
any special dependencies.
Getting started
info
Note
Terraform has been pre-installed in this course.
To install Terraform on your own machine, you can follow the instructions
on the Terraform website and download the relevant version -
https://developer.hashicorp.com/terraform/downloads
wget
https://releases.hashicorp.com/terraform/1.3.4/terraform_1.3.4_l
inux_amd64.zip
unzip terraform_1.3.4_linux_amd64.zip
chmod +x terraform
You can check out the tfenv Github repo for more information on its usage.
info
Note
We used tfenv to install Terraform in this course!
You can check the installed version of Terraform by pasting the following
command in the terminal on the left:
terraform --version
Let’s create and initialize our first
configuration!
We’ll begin by defining an AWS EC2 instance.
info
Note
We will use this AWS EC2 instance to run our demo app for this course.
Click here for more information on EC2.
You should see a file called main.tf in the top-left panel. This file will
contain the main set of configuration.
Config Details
info
Initializing
First, we need to initialize the state and check all dependencies. This is
done by running terraform init in the terminal.
Note
The .terraform module should usually be excluded from version
control.
Locks
You should also be able to see the .terraform.lock.hcl file in the file tree. It
locks the dependencies to the exact versions (hashes) for components in the
configuration.
Terraform will always reselect versions of components it uses from the lock
file unless -upgrade is specified for the init command.