Introduction to Terraform and Terraform Basics
#TerraWeek Challenge by TWS Community Builders
1. What is Terraform ?
Terraform is an open-source infrastructure-as-code(IaC) software tool created by HashiCorp. It allows you to automate and manage the infrastructure, your platform, and services that run on the platform. It is a tool for Infrasture provisioning.
We write terraform scripts using Hasicorp configuration language or HCL and it is a declarative language, meaning you just have to define what end result you want. It's not much about remembering syntax.
2. How can Terraform help you manage infrastructure as code?
Suppose you just started a project where you create some application and you want to set up an infrastructure from scratch where that application will run.
What will your infrastructure look like?
Let's say you want to set up several servers where you will deploy some microservices and you decided to use the AWS cloud platform.
So now your first step will be to go to AWS and prepare the set-up so that the application can be deployed there. This means you need to create your private network space, EC2 server instances, install the required tools, set up security on the servers etc.
So now to prepare the whole infrastructure in an automated manner Terraform comes into the picture.
Now let's assume that our infrastructure is ready by using Terraform. If there is a situation where we need to add more servers to our existing infrastructure and we need to remove some stuff which we added in the beginning. Using Terraform we can make such adjustments pretty easily and you don't have to manually configure or manage your infrastructure.
Another very useful case of Terraform is replicating the infrastructure. Let's say you have tested your set-up in a Dev environment and everything is working fine and now you decided you want to release your application in a production environment. So, we have to replicate the exact setup you made in dev. Here you can use the same terraform code to build the identical infrastructure in Production.
Terraform provides a comprehensive set of providers that allow you to define and manage resources from various cloud and infrastructure providers, such as AWS, Azure, Google Cloud, and more. These providers expose resources as Terraform modules, which you can use to create, update, and delete infrastructure resources consistently across different environments.
Terraform i) Supports multiple platforms and has hundreds of providers.
ii) Simple configuration language and faster learning curve.
iii) Easy integration with configuration management tools like Ansible.
iv) Easily extensible with the help of plugins.
v)And it's Free !!!
3.How to Install Terraform on Windows?
Terraform installation is very simple. You have a single binary file, download and use it. Installing Terraform on Windows requires you to download the correct Terraform package, unpack, and execute it via the CLI. Follow the instructions below to ensure you do not miss any steps.
Download Terraform File for Windows
To find the latest version of Terraform for Windows:
1. Browse to the Download Terraform page.
2. Select the Windows tab under the Operating System heading. The latest version is preselected.
3. Choose the binary to download. Select 386 for 32-bit systems or AMD64 for 64-bit systems. Choose the download location for the zip file if the download does not start automatically.
4. Unzip the downloaded file. For example, use the C:\terraform path. Remember this location so you can add the path to the environment variables.
Add Terraform Path to System Environment Variables
To add the Terraform executable to the system's global path:
1. Open the start menu, start typing environment and click Edit system environment variables. The System Properties window opens.
2. Click the Environment Variables... button.
3. Select the Path variable in the System variables section to add terraform for all accounts. Alternatively, select Path in the User variables section to add terraform for the currently logged-in user only. Click Edit once you select a Path**.**
Note: You need to edit both, else it won't work properly.
4. Click New in the edit window and enter the location of the Terraform folder.
5. Click OK on all windows to apply the changes.
Verify Windows Terraform Installation
To check the Terraform global path configuration:
1. Open a new command-prompt window.
2. Enter the command to check the Terraform version: terraform -version
terraform -version
The output shows the Terraform version you downloaded and installed on your Windows machine.
After this, you need to install an editor for Terraform. Here we will install "Visual Studio Code" as the primary editor.
You can download the exe file from below Link :
https://code.visualstudio.com/download
Once you installed the binary file. Please enable the extensions as mentioned in the below screenshot. i.e : Hasicorp Terraform and Terraform.
Extensions are add-ons that allow you to customize and enhance your experience in Visual Studio by adding new features or integrating existing tools They offer a wide range of functionality related to colours, auto-complete, report spelling errors etc.
Setting up the Environment for AWS:
i)Log in to the AWS Management Console and search for IAM service. ii)Create a new IAM user. iii)Under the "Security credentials" tab, click on "Create access key". iv)Save the access key ID and secret access key.
Now set AWS Access Key and Secret Access Key as Environment Variables:
On your local machine, open a terminal or command prompt and enter "aws configure", then it will ask for an access key and a secret access key.
4.Important Terminologies in Terraform.
Providers:
A provider is a plugin that lets Terraform manage an external API. When we run terraform init, plugins required for the provider are automatically downloaded and saved locally to a .terraform directory. Terraform supports multiple providers. Depending on what type of infrastructure we want to launch, we have to use appropriate providers accordingly.
Resource :
Resource block describes one or more infrastructure objects.
Example: resource aws_instance, resource aws_alb, resource iam_user, resource digitalocean_droplet
A resource block declares a resource of a given type ("aws_instance") with a given local name ("aws_ec2_test"). Resource type and Name together serve as an identifier for a given resource and so must be unique.
Variables :
Variables in Terraform can be assigned values in multiple ways. Some of these include: i)Environment variables ii)Command Line Flags iii)From a File iv)Variable Defaults
State:
Terraform's primary function is to create, modify, and destroy infrastructure resources to match the desired state described in a Terraform configuration. It maintains a state file that tracks the current state of your infrastructure. This allows Terraform to determine what changes need to be made to bring your infrastructure in line with your desired state.
The "terraform refresh" command will check the latest state of your infrastructure and update the state file accordingly.
Modules :
We can centralize the terraform resources and can call out from TF files whenever required.
Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the .tf files in the main working directory. A module can call other modules, which lets you include the child module's resources in the configuration concisely. A module that includes a module block like this is the calling module of the child module.
GitHub Link : https://github.com/SudipaDas/TerraWeek/tree/main/day01
If this post was helpful, please follow and click the ๐ button below to show your support.
_ Thank you for reading!
_Sudipa