Terraform is an open-source Hashicorp’s tool that supports cloud orchestration. With Terraform you can provision the resources on the cloud and launch as many instances or resources as you need. All that Terraform needs is a
.tf configuration file. This file is written in Terraform DSL (Domain-specific Language) and when executed it sends API calls to the cloud console and makes the resource available on the cloud. So as an exercise we will be doing a practice of provisioning tools over Terraform and will also explain the 5 commands of Terraform. Create these files in any directory. These files contain Infrastructure code which will help provision tools on the cloud.
So in the first file, as you could see we have specified the cloud provider and region in which you want to work. After this block, you could define any resource in the
resource block by mentioning the type of resource and resource name like here I have defined an
aws_instance with the name of
example. Then in the resource block, I have defined some basic variables as the
ami which is the amazon image which you want to use and
instance_type. And so in aws_instance, I have used one more tag block in which I have initialized the name instance. A point noted here is that it is a simple configuration file to just explain the syntax of the language which terraform uses. You can also see the documentation in which all types of resources are listed with their usage. Select your cloud provider and scroll down to the resources.
- terraform init:- The following command initializes a terraform working directory. It creates a
.terraformdirectory in which you could see a subdirectory named
providerswhich contains configuration files according to the provider you choose.
- terraform validate:- Validate the configuration files in a directory, referring only to the configuration and not accessing any remote services such as remote state, provider APIs, etc. Validate runs checks that verify whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. It is thus primarily useful for general verification of reusable modules, including the correctness of attribute names and value types. It is safe to run this command automatically, for example as a post-save check in a text editor or as a test step for a re-usable module in a CI system. Validation requires an initialized working directory with any referenced plugins and modules installed. To initialize a working directory for validation without accessing any configured remote backend, use: terraform init -backend=false. If dir is not specified, then the current directory will be used. To verify the configuration in the context of a particular run (a particular
target workspace, input variable values, etc), use the ‘terraform plan’
command instead, which includes an implied validation check.
- terraform plan:- Generates an execution plan for Terraform. This execution plan can be reviewed prior to running apply to get a
sense of what Terraform will do. Optionally, the plan can be saved to a Terraform plan file, and applications can take this plan file to execute this plan exactly.
- terraform apply:- Builds or changes infrastructure according to Terraform configuration files in DIR. By default, apply scans the current directory for the configuration and applies the changes appropriately. However, a path to another configuration or an execution plan can be provided. Execution plans can be used to only execute a pre-determined set of actions.
- terraform destroy:- Destroy Terraform-managed infrastructure.
So this was all about provisioning tools on the cloud with the help of Terraform.
Hope you liked it. Don’t forget to give a clap!