Understand Terraform basics Flashcards
Handle Terraform and provider installation and versioning Describe the plug-in based architecture Demonstrate using multiple providers Describe how Terraform finds and fetches providers Explain when to use and not use provisioners and when to use local-exec or remote-exec
How do you install terraform on different OS?
// Mac OS brew install terraform
// Windows choco install terraform
https://learn.hashicorp.com/terraform/getting-started/install
How do you manually install terraform?
step 1: Download the zip fille
step 2: mv ~/Downloads/terraform /usr/local/bin/terraform
Where do you put terraform configurations so that you can configure some behaviors of Terraform itself?
The special terraform configuration block type is used to configure some behaviors of Terraform itself, such as requiring a minimum Terraform version to apply your configuration. terraform { # ... }
Only constants are allowed inside the terraform block. Is this correct?
Yes
Within a terraform block, only constant values can be used; arguments may not refer to named objects such as resources, input variables, etc, and may not use any of the Terraform language built-in functions.
What are the Providers?
A provider is a plugin that Terraform uses to translate the API interactions with the service. A provider is responsible for understanding API interactions and exposing resources. Because Terraform can interact with any API, you can represent almost any infrastructure type as a resource in Terraform.
https://www.terraform.io/docs/configuration/providers.html
How do you configure a Provider?
provider “google” {
project = “acme-app”
region = “us-central1”
}
The name given in the block header (“google” in this example) is the name of the provider to configure. Terraform associates each resource type with a provider by taking the first word of the resource type name (separated by underscores), and so the “google” provider is assumed to be the provider for the resource type name google_compute_instance.
The body of the block (between { and }) contains configuration arguments for the provider itself. Most arguments in this section are specified by the provider itself; in this example both project and region are specific to the google provider.
What are the meta-arguments that are defined by Terraform itself and available for all provider blocks?
version: Constraining the allowed provider versions
alias: using the same provider with different configurations for different resources
What is Provider initialization and why do we need?
Each time a new provider is added to configuration – either explicitly via a provider block or by adding a resource from that provider – Terraform must initialize the provider before it can be used.
Initialization downloads and installs the provider’s plugin so that it can later be executed.
How do you initialize any Provider?
Provider initialization is one of the actions of terraform init. Running this command will download and initialize any providers that are not already initialized.
When you run terraform init command, all the providers are installed in the current working directory. Is this true?
Providers downloaded by terraform init are only installed for the current working directory; other working directories can have their own installed provider versions.
Note that terraform init cannot automatically download providers that are not distributed by HashiCorp. See Third-party Plugins below for installation instructions.
How do you constrain the provider version?
To constrain the provider version as suggested, add a required_providers block inside a terraform block: terraform { required_providers { aws = "~> 1.0" } }
How do you upgrade to the latest acceptable version of the provider?
terraform init –upgrade
It upgrade to the latest acceptable version of each provider
This command also upgrades to the latest versions of all Terraform modules.
How many ways you can configure provider versions?
1. With required_providers blocks under terraform block terraform { required_providers { aws = "~> 1.0" } } 2. Provider version constraints can also be specified using a version argument within a provider block provider { version= "1.0" }
How do you configure Multiple Provider Instances?
alias You can optionally define multiple configurations for the same provider, and select which one to use on a per-resource or per-module basis.
Why do we need Multiple Provider instances?
Some of the example scenarios:
a. multiple regions for a cloud platform
b. targeting multiple Docker hosts
c. multiple Consul hosts, etc.
How do we define multiple Provider configurations?
To include multiple configurations for a given provider, include multiple provider blocks with the same provider name, but set the alias meta-argument to an alias name to use for each additional configuration. # The default provider configuration provider "aws" { region = "us-east-1" }
# Additional provider configuration for west coast region provider "aws" { alias = "west" region = "us-west-2" }
How do you select alternate providers?
By default, resources use a default provider configuration inferred from the first word of the resource type name. For example, a resource of type aws_instance uses the default (un-aliased) aws provider configuration unless otherwise stated.
resource “aws_instance” “foo” {
provider = aws.west
# ... }
What is the location of the user plugins directory?
Windows %APPDATA%\terraform.d\plugins
All other systems ~/.terraform.d/plugins
Third-party plugins should be manually installed. Is that true?
True
The command terraform init cannot install third-party plugins? True or false?
True
Install third-party providers by placing their plugin executables in the user plugins directory. The user plugins directory is in one of the following locations, depending on the host operating system
Once a plugin is installed, terraform init can initialize it normally. You must run this command from the directory where the configuration files are located.
What is the naming scheme for provider plugins?
terraform-provider-_vX.Y.Z
What is the CLI configuration File?
The CLI configuration file configures per-user settings for CLI behaviors, which apply across all Terraform working directories.
It is named either .terraformrc or terraform.rc
Where is the location of the CLI configuration File?
On Windows, the file must be named named terraform.rc and placed in the relevant user’s %APPDATA% directory.
On all other systems, the file must be named .terraformrc (note the leading period) and placed directly in the home directory of the relevant user.
The location of the Terraform CLI configuration file can also be specified using the TF_CLI_CONFIG_FILE environment variable
What is Provider Plugin Cache?
By default, terraform init downloads plugins into a subdirectory of the working directory so that each working directory is self-contained. As a consequence, if you have multiple configurations that use the same provider then a separate copy of its plugin will be downloaded for each configuration.
Given that provider plugins can be quite large (on the order of hundreds of megabytes), this default behavior can be inconvenient for those with slow or metered Internet connections.
Therefore Terraform optionally allows the use of a local directory as a shared plugin cache, which then allows each distinct plugin binary to be downloaded only once.