Terraforms Step-by-step installation steps:
Step 1:
https://developer.hashicorp.com/terraform/install check full installation steps @above link. Choose your operating system and start installation. In my case, I'm having Ubuntu so will go with Ubuntu installation.
Step 2:
We can check whether terraform install are not with "terraform --version " command
Step 3:
AWS CLI credentials obtained through "aws configure"
are used by Terraform to authenticate and interact with your AWS account. Without valid credentials, Terraform won't be able to create, modify, or delete resources on your behalf.
Click on your account id and security credentials to install "aws configure"
Now, once we type "aws configure" to authenticate my aws server with terraform we have to put the access keys and secret access keys. We can get it through aws console.
Now can use "aws cli" to create resources through command line interface instead AWS UI. If you want to cross check go to aws console and create bucket then execute this command " aws s3 ls".
Then we can write our first terraform script:
Lets understand the concept here once we write the code in Terraform It's doesn't understand why we are writing the scripts & which cloud provider to need to talk (AWS, Azure or GCP) so first thing we have to keep in mind that in provider we have to mention
Always refer the terraform documentation.
I have taken "ami-id" from the console.
So, we can create main.tf file first. to create our first instance through terraform.
then we have to execute the command "terraform init" this will initialization the configuration
& then we have to executed the command "terraform plan" this is act as a savior. The best practice is always execute this command before creating infrastructure . Its like a dry run before configure your infrastructure.
& Boom!! our ec2 instance has been created. We can check on the AWs console the instance has been created.
To avoid charges we can use "terraform destroy" command is used to destroy the resources that were created by a Terraform configuration. This command is useful when you want to tear down the infrastructure that you've provisioned using Terraform.
The below is the whole lifecycle of the terraform:
To get started with Terraform, it's important to understand some key terminology and concepts. Here are some fundamental terms and explanations.
Provider: A provider is a plugin for Terraform that defines and manages resources for a specific cloud or infrastructure platform. Examples of providers include AWS, Azure, Google Cloud, and many others. You configure providers in your Terraform code to interact with the desired infrastructure platform.
Resource: A resource is a specific infrastructure component that you want to create and manage using Terraform. Resources can include virtual machines, databases, storage buckets, network components, and more. Each resource has a type and configuration parameters that you define in your Terraform code.
Module: A module is a reusable and encapsulated unit of Terraform code. Modules allow you to package infrastructure configurations, making it easier to maintain, share, and reuse them across different parts of your infrastructure. Modules can be your own creations or come from the Terraform Registry, which hosts community-contributed modules.
Configuration File: Terraform uses configuration files (often with a
.tf
extension) to define the desired infrastructure state. These files specify providers, resources, variables, and other settings. The primary configuration file is usually namedmain.tf
, but you can use multiple configuration files as well.Variable: Variables in Terraform are placeholders for values that can be passed into your configurations. They make your code more flexible and reusable by allowing you to define values outside of your code and pass them in when you apply the Terraform configuration.
Output: Outputs are values generated by Terraform after the infrastructure has been created or updated. Outputs are typically used to display information or provide values to other parts of your infrastructure stack.
State File: Terraform maintains a state file (often named
terraform.tfstate
) that keeps track of the current state of your infrastructure. This file is crucial for Terraform to understand what resources have been created and what changes need to be made during updates.Plan: A Terraform plan is a preview of changes that Terraform will make to your infrastructure. When you run
terraform plan
, Terraform analyzes your configuration and current state, then generates a plan detailing what actions it will take during theapply
step.Apply: The
terraform apply
command is used to execute the changes specified in the plan. It creates, updates, or destroys resources based on the Terraform configuration.Workspace: Workspaces in Terraform are a way to manage multiple environments (e.g., development, staging, production) with separate configurations and state files. Workspaces help keep infrastructure configurations isolated and organized.
Remote Backend: A remote backend is a storage location for your Terraform state files that is not stored locally. Popular choices for remote backends include Amazon S3, Azure Blob Storage, or HashiCorp Terraform Cloud. Remote backends enhance collaboration and provide better security and reliability for your state files.
Happy Learning!!