What is providers?
In Terraform, a provider is a plugin that interacts with the APIs of various cloud platforms and services. Providers are responsible for understanding API interactions and exposing the resources available for management. They are the bridge between Terraform and the service you’re using, such as AWS, Azure, Google Cloud, or any other service.
Key Points about Providers
- Resource Management: Providers define the types of resources and data sources that Terraform can manage. Each provider is responsible for its own set of resources.
- Configuration: Providers require configuration, which typically includes specifying authentication credentials and setting default parameters like region or endpoint.
- Plugins: Providers are distributed as plugins and must be installed for Terraform to interact with the services they manage. Terraform automatically installs the required providers during the initialization phase.
Common Providers
- AWS Provider: Manages resources in Amazon Web Services.
- Azure Provider: Manages resources in Microsoft Azure.
- Google Provider: Manages resources in Google Cloud Platform.
- Kubernetes Provider: Manages resources in a Kubernetes cluster.
- GitHub Provider: Manages resources within a GitHub organization or repository.
How to Install Providers Using Code?
Using CODE: To manage and install providers in Terraform, you write your configuration in code files with the .tf
extension.
How to Store Code: Store your code in files with a .tf
extension, such as filename.tf
.
One Directory: Place all your .tf
files in one directory. This allows Terraform to read and process them together as a single configuration.
Example: You can have multiple .tf
files in one directory:
filename1.tf
filename2.tf
filename.tf
This setup helps manage your infrastructure code more efficiently.
Example Code to Install a Provider
Go to the terraform providers line – https://registry.terraform.io/browse/providers
Search for provider you are looking for if its AWS — search for it and copy the code and paste in providers.tf file.
Let’s say you want to use the AWS provider. Here’s how you might write the code in providers.tf
:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.50.0"
}
}
}
provider "aws" {
# Configuration options
}
For Azure Provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.104.2"
}
}
}
provider "azurerm" {
# Configuration options
}
To install provider , Run terraform init
This terraform init command will download terraform providers
Directory Structure
Your directory might look like this:
my-terraform-project/
├── main.tf
├── variables.tf
├── providers.tf
├── outputs.tf
Each file contains different parts of your configuration:
- main.tf: Main resources and infrastructure code.
- variables.tf: Variable definitions.
- providers.tf: Provider configurations.
- outputs.tf: Outputs definitions.
Running Terraform
- Initialize Providers:
terraform init
This command downloads and installs the necessary providers based on your configuration.
2. Plan and Apply:
terraform plan
terraform apply
How to perform above steps
Open CMD – from same path from where your providers.tf file saved
Run Terraform init command
while running I got below error, lets debug the error
if you read the error messages it say required providers – not required 2 times in files so change it to below format
First part blocking the version and second part are provider installation
#blocking the version for both aws and azurerm
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.50.0"
}
azurerm = {
source = "hashicorp/azurerm"
version = "3.104.2"
}
}
}
# below are the providers
provider "aws" {
# Configuration options
}
provider "azurerm" {
# Configuration options
}
Now re run it again,
we can see additional folder create, if we go inside we can see the providers installed , which we installed via code