Terraweek#Day06-Terraform Providers

Terraweek#Day06-Terraform Providers

Terraform Providers:

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.

  • 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.

AWS provider

Configuration and Authentication:

Configuration for the AWS Provider can be derived from several sources, which are applied in the following order:

  1. Parameters in the provider configuration

  2. Environment variables

  3. Shared credentials files

  4. Shared configuration files

  5. Container credentials

  6. Instance profile credentials and region

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

# Create a VPC
resource "aws_vpc" "example" {
  cidr_block = "10.0.0.0/16"
}

However, it's recommended to use environment variables or shared credentials files to avoid exposing sensitive information in your Terraform configuration files.

e.g.

Credentials can be provided by adding an access_key, secret_key, and optionally token, to the aws provider block.

Usage:

provider "aws" {
  region     = "us-west-2"
  access_key = "my-access-key"
  secret_key = "my-secret-key"
}

[**

Environment Variables**](registry.terraform.io/providers/hashicorp/a..)

Credentials can be provided by using the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and optionally AWS_SESSION_TOKEN environment variables. The region can be set using the AWS_REGION or AWS_DEFAULT_REGION environment variables.

For example:

provider "aws" {}
$ export AWS_ACCESS_KEY_ID="anaccesskey"
$ export AWS_SECRET_ACCESS_KEY="asecretkey"
$ export AWS_REGION="us-west-2"
$ terraform plan

Other environment variables related to authorization are:

How to use the Terraform Azure Provider?

Terraform depends on plugins to interact with cloud providers such as Azure, Google Cloud Platform (GCP), and Oracle. One of the most widely used providers is the Terraform Azure provider. The provider interacts with many resources supported by Azure, such as Azure SQL, Azure Data Factory, Azure Active Directory, and many more.

Terraform uses the Azure provider with proper credentials to authenticate and connect with Azure to manage or deploy/update dozens of Azure services.

Terraform supports multiple methods for authenticating to Azure, such as authenticating to Microsoft Azure using the Azure CLI and Managed Service Identity, etc. But this tutorial will authenticate using Azure CLI. So, let’s dive in.

Authenticating to Microsoft Azure using Azure CLI

Terraform supports a number of different methods for authenticating to Azure:

The easiest way to test Terraform resource management is by authenticating with Azure CLI. If you want to try creating resources on your local machine or in a dev environment, consider using the Azure CLI as your authentication method. Let’s learn how to use Azure CLI and configure it for Terraform.

To install Azure CLI on an Ubuntu machine, click here

  1. Log in to the Ubuntu machine using your favorite SSH client.

  2. First, need to log in to the Azure CLI using the below command.

az login

e.g.

  1. The Azure Provider is declared within the Terraform configuration file and it includes various parameters such as version, endpoint URLs or cloud regions, etc., as shown below.
terraform {

 required_providers {

 azurerm = {

 source = "hashicorp/azurerm"

 version = "=2.46.0"

}

}

}

# Configure the Microsoft Azure Provider (azurerm provider) in provider blocks.

provider "azurerm" {

 features {}

}

Google Cloud Platform Provider

The Google provider is used to configure your Google Cloud Platform infrastructure.

e.g.

A google provider configuration will look something like:

provider "google" {
  project     = "my-project-id"
  region      = "us-central1"
}

linkdin:https://www.linkedin.com/in/gajanan-barure-7351a4140

Happy Learning :)

Thank you for reading!! Hope you find this helpful.

Terraweekday06#challenge90daysofdevops

Shubham Londhe