Terraform Providers Block

From Terraform.io

Terraform is used to create, manage, and update infrastructure resources such as physical machines, VMs, network switches, containers, and more. Almost any infrastructure type can be represented as a resource in Terraform.

A provider is responsible for understanding API interactions and exposing resources. Most providers configure a specific infrastructure platform (either cloud or self-hosted). Providers can also offer local utilities for tasks like generating random numbers for unique resource names.

In my view point this is where it all starts, the selection of where your infrastructure will reside. In many cases, this is already decided, and is probably a major cloud provider. I will provide some examples from the providers I deal with. In other cases, a browse through the Terraform Registry providers section will yield some enlightenment.

For AWS, one assumes that we have an AWS account, have installed the AWS cli, (use v.2, please), and have configured your profile such that results of a test command (see below) yields a list of aws regions.

$ # TEST COMMAND FOR AWS CLI INSTALL AND CONFIGURATION
$ aws --region <REGION> --profile <PROFILE> ec2 describe-regions --query Regions[*].RegionName

If you do not have the aws cli installed or configured, I’ll refer you to the AWS provided page for installation of the v2 cli. And will also recommend the informative page on configuring the various options of the v2 cli. Along with those the page on configuring named profiles is highly recommended.

Yes, I could write many posts on how to install and configure, but they would be out of date .351 milimicroseconds after I hit publish.

Another basic requirement is terraform naturally. The basic binaries can be downloaded from the terraform site. There is an excellent set of tutorials located in the terraform tutorials pages. The actual installation is quite easy and is well documented in the install section of the tutorials page.

And yes, many times have I railed against blog writers that link key and basic information or processes to other sites. Do see my comments prior, re the lifespan of memorializing vendor documentation. In the interest of providing the most up to date information possible, I’ll direct you to the various vendor pages as they are appropriate.

My basic terraform providers block is similar to below:

# 20201201 - SAFH
# Some description of project / client / environment
# SAFH@techhell.org
# A private work, for reference and training, not for publication
# Basic AWS provider code for Terraform on Win 10
# Defines the basic provider information
#
provider "aws" {
# SET YOUR REGIONS FROM the vars.tf for the project
    region = var.region
# Just for completeion one can set region manually
#   region = "us-east-1"
# You do have the aws cli installed and configured?
    shared_credentials_file = "~/.aws/credentials"
# One can also set access keys and secret access keys in the provider file
# NOT RECCOMMENDED, I shan't give the example
# In case you have multiple profiles in the credentials file
    profile = <NAMED PROFILE FOR PROJECT>
}

Lines 1 – 7, are code commentary, ( you do comment your code, right?), the actual provider stanza begins on line 8, where we declare our provider to be aws

In line 10, where we declare a variable ( var.region), this will be defined in our var.tf shown below. In this specific case we have defined our region as us-east-1.

In line 14, we define the location of our aws credentials, do note this is out of the directory path for our code, thusly a git commit will not expose our aws credentials, globally ..

At line 18, an aws credentials profile is named, as we may have multiple projects or multiple environments for our project. This can also be defined as a variable in our var.tf file to make the code more mobile. For more on named profiles I’ll refer you to the excellent AWS documentation.

At line 19, we close the stanza.

# 20201201 - SAFH
# Some description of project / client / environment
# SAFH@techhell.org
# A private work, for reference and training, not for publication
#
# var.tf
# vars file  Define Project varaiables
#
# Review and set ALL VARS!!
# --------------------
# Project Vars
variable "name" {
  default     = "dev"
  type        = string
  description = "Name of the Virtual Private Cloud ( VPC )"
}
variable "project" {
  default = "website"
  type        = string
  description = "Name of project this VPC is meant to house"
}
variable "environment" {
  default = "dev"
  type        = string
  description = "Name of environment this VPC is targeting"
}
variable "region" {
  default     = "us-east-1"
  type        = string
  description = "Region of the VPC"
}
variable "tags" {
  default     	= {
      Owner 	= "SAFH"
      Expire    = "End of project"
  }
  type        = map(string)
  description = "Extra tags to attach to the VPC resources"
}

A note about the var.tf file or what you are calling the variables declaration file. It is a good idea to provide a description for each variable, and to declare a type for said variable. It WILL HELP whence one returns to a code fragment after a long period of time.