Terraform Providers: Terraform is one of the most popular tools used by DevOps teams to automate infrastructure tasks. It is used to provision and manages any cloud, infrastructure, or service.
Terraform officially supports around 130 providers. Its community-supported providers’ page lists another 160. Some of those providers expose just a few resources, but others, such as AWS, OCI, or Azure, have hundreds of them.
In this blog post, we cover a basic introduction of terraform providers and some major terraform cloud providers such as AWS, Azure, Google, and OCI.
Index:
- Terraform Providers
- Terraform AWS Provider
- Azure Terraform Resource Provider
- Oracle Cloud Infrastructure Terraform Provider
- Google Cloud Terraform Provider
- Provider Block
- Provider Workflow
- How To Use Providers
Terraform Providers
A large percentage of Terraform users provision their infrastructure on the major cloud providers such as AWS, Azure, OCI, and others. To know more about various other terraform providers check here.
Let’s understand the basic terminologies often used in Terraform:
- Terraform provisions, updates, and destroys infrastructure resources such as physical machines, VMs, network switches, containers, and more.
- Configurations are code written for Terraform, using the human-readable HashiCorp Configuration Language (HCL) to describe the desired state of infrastructure resources.
- Providers are the plugins that Terraform uses to manage those resources. Every supported service or infrastructure platform has a provider that defines which resources are available and performs API calls to manage those resources.
- Modules are reusable Terraform configurations that can be called and configured by other configurations. Most modules manage a few closely related resources from a single provider.
- The Terraform Registry makes it easy to use any provider or module. To use a provider or module from this registry, just add it to your configuration; when you run `terraform init`, Terraform will automatically download everything it needs.
A provider is responsible for understanding API interactions and exposing resources. It interacts with the various APIs required to create, update, and delete various resources. Terraform configurations must declare which providers they require so that Terraform can install and use them.
Also check: Types of Terraform Variables
Terraform AWS Provider
Terraform can provision infrastructure across public cloud providers such as Amazon Web Services (AWS), Azure, Google Cloud, and DigitalOcean, as well as private cloud and virtualization platforms such as OpenStack and VMWare.
AWS is a good choice for learning Terraform because of the following:
- AWS is the most popular cloud infrastructure provider, by far. It has a 45% share in the cloud infrastructure market, which is more than the next three biggest competitors (Microsoft, Google, and IBM) combined.
- AWS provides a huge range of reliable and scalable cloud hosting services, including Amazon Elastic Compute Cloud (Amazon EC2), which you can use to deploy virtual servers; Auto Scaling Groups (ASGs), which make it easier to manage a cluster of virtual servers; and Elastic Load Balancers (ELBs), which you can use to distribute traffic across the cluster of virtual servers.
- AWS offers a generous Free Tier for the first year that should allow you to run all of these examples for free. If you already used up your free tier credits, the examples in this book should still cost you no more than a few dollars.
Learn more about how to AWS Free Tier Account to avail the free tier services.
The two most popular options for deploying infrastructure to AWS are CloudFormation, a service native to AWS, and Terraform, an open-source offering from HashiCorp. Most of the AWS resources can be provisioned with Terraform as well and is often faster than CloudFormation when it comes to supporting new AWS features. On top of that, Terraform supports other cloud providers as well as 3rd party services.
Here’s an example of how to configure the AWS provider in Terraform using code:
# main.tf # Configure the AWS provider provider "aws" { access_key = "YOUR_AWS_ACCESS_KEY" secret_key = "YOUR_AWS_SECRET_KEY" region = "us-west-2" }
In the example above, we configure the AWS provider by specifying the AWS access key, secret key, and the desired AWS region. The access key and secret key are used to authenticate Terraform with your AWS account.
Also read: Step by step guide on Terraform Certification
Azure Terraform Resource Provider
Azure Resource Providers for HashiCorp Terraform enables Azure customers using Azure Resource Manager (ARM) to provision and manage their resources with Terraform Providers as if they were native Azure Resource Providers.
Below are some of the core infrastructure services supported by Azure Resource Provider in Terraform:
- Virtual machines
- Storage accounts
- Networking interfaces
- Azure Container Instances
- Azure Container Service
- Managed Disks
- Virtual Machine Scale Sets
The ARM Resource Provider leverages HashiCorp Terraform to provide third-party services to ARM users directly via ARM. Some of these third-party services supported are listed below:
Terraform is built into Azure Cloud Shell and cloud shell automatically authenticates your default Azure CLI subscription to deploy resources through the Terraform Azure modules.
To know more about Azure provider for Terraform, click here.
Also Check: Our blog post on Terraform Commands Cheat Sheet. Click here
Oracle Cloud Infrastructure Terraform Provider
Oracle Cloud Infrastructure is an official provider of Hashicorp Terraform supporting infrastructure-as-code for oracle cloud customers.
The provider is compatible with Terraform 0.10.1 and later. Following are some of the main resources supported by the Terraform provider:
- Block Volumes
- Compute
- Container Engine
- Database
- File Storage
- Identity and Access Management (IAM)
- Load Balancing
- Networking
- Object Storage
A detailed list of supported resources and more information about how to get started is available on the HashiCorp website.
Oracle also provides Resource Manager, a fully managed service to operate Terraform. Resource Manager integrates with Oracle Cloud Infrastructure Identity and Access Management (IAM), so you can define granular permissions for Terraform operations. It also provides state locking, giving users the ability to share state, and lets teams collaborate effectively on their Terraform deployments. Most of all, it makes operating Terraform easier and more reliable.
To know more about Resource Manager, check here.
Oracle had announced two features to help you bring your existing infrastructure to Terraform and Resource Manager:
- Terraform Resource Discovery helps you move from a console- or SDK-managed infrastructure to an infrastructure managed by Terraform.
- State File Import helps customers who have a Terraform-managed infrastructure move to a Resource Manager–managed infrastructure.
To know more about Terraform Resource Discovery, check here.
Now that we got an overview of what a provider is and services provided by some major providers, let’s see how we can use one in our terraform configuration files.
Also Read: Our blog post on Hashicorp Terraform. Click here
Google Cloud (GCP) Terraform Provider
The Google Cloud Terraform Provider is used to configure your Google Cloud Platform infrastructure. It is collaboratively maintained by the Google Terraform Team at Google and the Terraform team at HashiCorp.
It looks over the lifecycle management of GCP resources, including Compute Engine, Cloud Storage, Cloud SDK, Cloud SQL, GKE, BigQuery, Cloud Functions, and more.
Below is a general provider configuration snippet:
provider "google" { project = "my-project-id" region = "us-central1" }
In order to get started with Google Terraform Provider, you need to have a Google Cloud Free Trial Account with billing enabled and terraform installed in your project. Follow the steps from this guide to create your Google Cloud Free Trial Account and create a project.
Provider Block
Terraform configurations must declare which providers they require so that Terraform can install and use them. Providers are executable plugins that contain the code necessary to interact with the API of the service it was written for.
A provider is defined by a provider block, the actual arguments in a provider block vary depending on the provider, but all providers support the meta-arguments of version and alias.
The below image shows the provider block format across different providers.
Check Out: How to Install Terraform on Mac, Windows & Ubuntu. Click here
Provider Workflow
Terraform finds and installs providers when initializing a working directory. It can automatically download providers from a Terraform registry, or load them from a local mirror or cache.
Hashicorp distributed providers are available for download automatically during Terraform initialization, while third-party providers must be placed in a local plug-ins directory located at either %APPDATA%\terraform.d\plugins for Windows or ~/.terraform.d/plugins for other operating systems.
The flow of steps performed is explained below:
- Initialize the Terraform configuration, i.e. run terraform init command.
- It looks for provider being used, and download the provider plug-ins, if not found.
- Retrieves the provider plug-ins
- Stores them in a .terraform subdirectory
- Check the default version or specified version
How To Use Providers
You can follow some simple steps to use your choice of provider be it AWS, Azure, Google Cloud, or Oracle in any IDE of your choice (here we are using VSCode- Visual Studio Code).
On your laptop, you can create a folder names Terraform and inside this folder create sub-folders like AWS, Azure, Google, etc.
Then open this complete folder in VSCode and also install the HashiCorp Terraform extension.
Now suppose you have to use the AWS provider, then in VSCode inside the AWS folder create a new file as the provider.tf and copy the code from the official AWS provider site in that file. And once done you need to initialize the terraform by running terraform init command in the terminal.
Check this video on Providers in Terraform to get in detail the next steps to follow after initializing Terraform.
FAQs
Can I use multiple providers in a single Terraform configuration?
Yes, Terraform supports using multiple providers within a single configuration file. This allows you to manage resources from different services or cloud providers in a unified manner.
Where can I find Terraform providers?
Terraform providers can be found on the Terraform Registry (registry.terraform.io). The registry hosts a wide range of providers contributed by both the Terraform community and service providers themselves.
How do I authenticate with a Terraform provider?
Authentication methods vary depending on the provider. Most providers support authentication using API keys, access tokens, or credentials stored in environment variables. Refer to the provider's documentation for specific instructions on authentication.
Can I create my own Terraform provider?
Yes, you can create your own Terraform provider if the service or resource you want to manage is not already supported by an existing provider. Terraform provides a software development kit (SDK) and documentation to help you build custom providers.
How frequently are Terraform providers updated?
The frequency of provider updates varies depending on the provider and the service it integrates with. Providers are typically updated to support new features, bug fixes, and changes to the underlying service's API. It's recommended to regularly check for updates and review the changelog of providers you use.
Can I use Terraform providers with infrastructure-as-code frameworks other than Terraform?
No, Terraform providers are specifically designed to work with Terraform. However, some providers may have similar functionality implemented in other infrastructure-as-code frameworks. You should consult the documentation of the framework you're using for information on integrating with different providers.
How do I install the Terraform AWS provider?
The Terraform AWS provider is automatically installed when you run terraform init in a Terraform configuration directory that includes AWS resources. Terraform will download the provider binary from the Terraform Registry and place it in the appropriate directory.
Can I specify a specific version of the Terraform AWS provider?
Yes, you can specify a specific version of the Terraform AWS provider in your Terraform configuration file. Use the version attribute in the provider block to pin it to a specific version. This ensures reproducibility and avoids unexpected changes when updating Terraform configurations.
Can I use Terraform modules with the Terraform AWS provider?
Yes, you can use Terraform modules with the Terraform AWS provider. Modules allow you to encapsulate and reuse Terraform configurations for AWS resources. You can create reusable modules or utilize community-contributed modules from the Terraform Registry to simplify the provisioning of AWS infrastructure.
Related/References
- Terraform Installation Overview
- HashiCorp Infrastructure Automation Certification: Terraform Associate
- Terraform Workflow & Use Cases
- Terraform Interview Questions & Answers
- Variables in Terraform
- Terraform Provisioners
Join FREE Class
🚀 Master Terraform & DevOps to get High-Paying Jobs!
🔥 Join our EXCLUSIVE Free class! 🚀
Get your hands dirty with lots of projects and labs based on Terraform and DevOps in our Program.
Click on the below image to Register for Our FREE Class Now!
Leave a Reply