Terraform Basics and Initial Configuration

Terraform Basics and Initial Configuration

This post will help you to understand the terraform basics and how to do the terraform initial configurations. In the previous post, we have covered the terraform introduction, you can refer it if you want to start from terraform introduction?

Topics Covered:

  1. Terraform Basic Files
  2. Terraform Providers
  3. How Terraform Works?
  4. Terraform Workflow with Commands

If you are interested in learning, Request you to go through the below recommended tutorial.

You can also watch this entire tutorial on our Youtube channel with demo.

Terraform Basic Files

In terraform, we do everything through files. So we should have knowledge on what are the types of terraform files we have and its purpose. Lets understand the terraform files first.

TF files (.tf)

Terraform files are created with .tf extension. Filename can be anything but .tf extension is must. Terraform will read only .tf files and other terraform files, It won't read any other file types.

Examples:
main.tf
ec2.tf
vpc.tf
busapp_vpc.tf
iam_developer.tf

State files (.tfstate)

Terraform always maintains a state file called terraform.tfstate whenever you apply your changes. This file will track the current state of your target infrastructure resources and store it in terraform.tfstate fille.

Example:
terraform.tfstate
terraform.tfstate.backup (This file is backup of previous terraform.tfstate file.)

Variable files (.tfvars)

Terraform provides a dedicated files to define any variables that is needed for our infrastructure. These variables can be called in any of our terraform files wherever needed. We can create our variables files with extension .tfvars.

Examples:
variables.tfvars
dev_env.tfvars
production.tfvars

Terraform Providers

Terraform providers are the most important component that tells the terraform to interact with cloud providers, API's, services. So providers are responsible to manage the resources on the target infrastructure.

Obviously we will write terraform files to manage some target infrastructure. That infrastructure is providers. We need to specify the providers (eg. AWS, Google Cloud, Azure, VMware) in the terraform files.

Terraform supports lot of providers.  That is why terraform is so popular.

Here is the link to find all supported terraform providers - Browse Providers | Terraform Registry

Lets say, you have decided to manage your AWS infrastructure. In that case, your provider is AWS.

If we define the below provider section in terraform files, terraform will make sure to download all the necessary plugins and codes to talk to aws cloud api's so that we would be able to create or manage any resources within the AWS cloud environment. Also we have to create CLI based token on AWS like aws_access_key and aws_secret_key to manage resource through terraform tools. For any providers you choose, it should support such authentication, then only you can manage through cli.
provider "aws" {
  region     = "us-west-2"
  access_key = "my-access-key"
  secret_key = "my-secret-key"
}
Once terraform provider is defined, we can start writing further terraform files to create or manage any resources of the infrastructure. We will talk about how to create terraform resources in the next post.

How Terraform Works?

Terraform reads all your terraform files, starting with provider block, followed by all resources blocks.

Then it builds a plan to create, update or delete resources based on the terraform configurations.

We need to provide instructions to terraform for the workflow. Lets understand the terraform workflow with commands.

Terraform Workflow

1. First, Write terraform files.
2. Initialize terraform directory - "terraform init"
3. Validate terraform configuration - "terraform validate"
4. Generate execution plan and identify the changes - "terraform plan"
5. Apply terraform changes - "terraform apply"

Terraform Commands

terraform init - This command will initialize the current terraform directory and will download all necessary plugins, source codes that are needed to manage the infrastructure. It is based on the provider block you have defined in terraform files.

Once if you initialize your terraform directory, terraform will recognize all the terraform files.

terraform validate - This terraform validate command helps you to validate for syntax errors in your terraform files.

terraform plan - This command will be like a dry run. It will generate the execution plan and will show you the list of resources that are going to be affected if we apply the changes. So before applying any changes, its good to run the terraform plan command to understand the impact.

terraform apply - This terraform apply command will be the main command that will apply the changes to your infrastructure based on the terraform configuration files you have created.

terraform destroy - This command will delete all your resources on your infrastructure that is created through your terraform files.

That's it for this post. Keep practicing and have fun. Leave your comments if any.


إرسال تعليق

0 تعليقات