Providers in Terraform
Install Terraform
The official Terraform installation documentation is available at: Terraform Downloads
Installation varies by OS:
- Linux/Mac: Use a package manager or download the binary.
- Windows: Download the binary and set it up manually.
Since Terraform is commonly used for automating AWS infrastructure, AWS will be used as an example for learning Terraform concepts. However, the principles apply to other platforms and technologies as well.
installation of Terraform on a MacBook using Homebrew. Tap HashiCorp Repository: brew tap hashicorp/tap Adds the HashiCorp repository to Homebrew. Install Terraform: brew install hashicorp/tap/terraform Fetches Terraform from the HashiCorp tap. Downloads and installs Terraform version 1.6.1. Verify Installation: terraform -v Confirms that Terraform v1.6.1 is installed successfully on a darwin_amd64 system. |
Providers
Terraform requires providers to connect and interact with various technologies (AWS, Azure, GCP, Kubernetes, etc.). https://registry.terraform.io/browse/providers
Key Points About Providers
- Exposes resources for specific platforms.
- Understands APIs of different platforms to enable Terraform interaction.
- Acts as a bridge between Terraform and infrastructure technologies.
- Manages infrastructure across various cloud and service providers.
Types of Providers in Terraform
- HashiCorp Providers
- Developed and maintained by HashiCorp.
- Includes popular platforms like AWS, Azure, and Google Cloud.
- Translates Terraform configurations into API requests.
- Partner Providers
- Owned by third-party technology partners.
- Actively maintained and updated.
- Verified by HashiCorp for reliability and security.
- Community Providers
- Created by community members or developer teams.
- Available in the Terraform Registry for public use.
AWS Provider Example
- AWS is widely used with Terraform due to its complexity and modular structure.
- Automating AWS infrastructure using Terraform simplifies management.
- AWS provider documentation provides clear examples and guides for implementation.
Terraform file that Connect to AWS
Steps to Create a Terraform File for AWS VPC and Subnet:
- Create a Project Folder
mkdir terraform cd terraform |
- Open Visual Studio Code
- Launch VS Code and open the terraform folder.
- Create a new file and save it as main.tf.
- Install Terraform Plugin in VS Code
- Open Extensions (Ctrl+Shift+X or Cmd+Shift+X on Mac).
- Search for Terraform.
- Install a plugin for syntax highlighting and autocomplete (e.g., HashiCorp Terraform extension or a well-rated alternative).
Now, you’re ready to write Terraform code to connect to AWS and create a VPC and subnet.
Using the AWS Provider in Terraform
- Setting Up the Working Directory
- Open the Terraform folder in VS Code to manage files.
- The main.tf file will contain the AWS provider configuration.
- Defining the AWS Provider
- Specify the AWS provider in main.tf.
- Configure region, selecting the closest AWS region.
- Define access key and secret key (hardcoded temporarily).
- Handling Credentials Securely
- Hardcoding credentials is not recommended.
- Terraform files are often stored in Git repositories.
- Best practice: Use environment variables or AWS credentials file instead of direct hardcoding.
- Installing the Required Providers
- Terraform does not include providers by default to save space.
- Users must install only the specific providers they need (e.g., AWS, Kubernetes, Jenkins).
- Providers must be defined in the configuration and then installed.
- Specifying Provider Version
- Define providers globally using Terraform configuration (terraform block).
- Specify the required provider versions for compatibility.
- The AWS provider documentation provides the latest version syntax.
- Organizing Provider Configurations
- Multiple providers can be listed under required_providers in main.tf.
Best practice: Use a separate file (providers.tf) for managing providers.

terraform init
Terraform Initialization (terraform init)
- Initializes a working directory – Sets up Terraform in the project folder.
- Installs required providers – Downloads provider plugins (e.g., AWS, Azure, Kubernetes) defined in the Terraform configuration.
Process Overview
- Run terraform init in the Terraform project directory.
- Terraform detects and installs the specified provider and its defined version.
- It generates additional files:
- .terraform/ (hidden directory) – Contains the downloaded provider code.
- .terraform.lock.hcl – Tracks installed provider versions.
Managing Providers
- Best practice: Use providers.tf to keep provider definitions separate for cleaner code.
- Providers from Terraform Registry (e.g., AWS, Azure) do not require an explicit source definition.
- Non-registry providers (e.g., Linode) require defining the source location in the configuration.
Handling Non-Registry Providers
- If a provider is not available in the HashiCorp registry, Terraform will fail to find it.
- The correct source must be specified explicitly (e.g., Linode provider from registry.linode.com).
- Running terraform init again after defining the correct source will successfully download and install the provider.
AWS Provider Capabilities in Terraform
- The AWS provider grants access to the entire AWS API, allowing interaction with all AWS services and resources.
- Through Terraform, any action that can be performed via the AWS API can be automated.
- The provider documentation lists all available AWS services (e.g., EC2, ECR) and their associated resources.
- Users can provision, configure, and manage AWS services directly from Terraform.
- Expanding a specific service (e.g., EC2) in the documentation reveals all possible resources that can be created or modified.