Lab : Multiple Provider Q 10

hi Team / @vijin.palazhi

Thanks a lot for this wonderful course.

In lab : Multiple provider Q 10 all my checks are passing . I have question on concepts .

Here we make addition to the existing configuration file code.tf where we add additional resources which needs prvoider “random” which is not yet installed . As we have not done terraform init again . So when we run taerrafom it does suggest what is missing
Question is at this stage when the 2nd provider is not yet installed when I run command # terraform provider I see below message .


 iac-server $ terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/local]
└── provider[registry.terraform.io/hashicorp/random]

Providers required by state:

    provider[registry.terraform.io/hashicorp/local]

iac-server $ 
iac-server $ pwd
/root/terraform-projects/provider-a
iac-server $ 
iac-server $ ls -al
total 20
drwxr-xr-x 3 root root 4096 Feb 25 09:42 .
drwxr-xr-x 6 root root 4096 Feb 25 09:27 ..
drwxr-xr-x 3 root root 4096 Feb 25 09:40 .terraform
-rw-r--r-- 1 root root  190 Feb 25 09:41 code.tf
-rw-r--r-- 1 root root  822 Feb 25 09:41 terraform.tfstate
iac-server $ 
iac-server $ date
Thu Feb 25 09:57:41 UTC 2021
iac-server $ 

iac-server $ date
Thu Feb 25 09:57:41 UTC 2021
iac-server $ 
iac-server $ 
iac-server $ terraform init

Initializing the backend...

Initializing provider plugins...
- Using previously-installed hashicorp/local v2.1.0
- Finding latest version of hashicorp/random...
- Installing hashicorp/random v3.1.0...
- Installed hashicorp/random v3.1.0 (signed by HashiCorp)

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* hashicorp/local: version = "~> 2.1.0"
* hashicorp/random: version = "~> 3.1.0"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
iac-server $ 
iac-server $ terrafrom plan 
bash: terrafrom: command not found
iac-server $ 
iac-server $ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

local_file.iac_code: Refreshing state... [id=11bd49955f5aed77659cbc4e125e2f88a78ee1e5]

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # random_string.iac_random will be created
  + resource "random_string" "iac_random" {
      + id          = (known after apply)
      + length      = 10
      + lower       = true
      + min_lower   = 0
      + min_numeric = 0
      + min_special = 0
      + min_upper   = 5
      + number      = true
      + result      = (known after apply)
      + special     = true
      + upper       = true
    }

Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

iac-server $ 
iac-server $ terraform apply
local_file.iac_code: Refreshing state... [id=11bd49955f5aed77659cbc4e125e2f88a78ee1e5]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # random_string.iac_random will be created
  + resource "random_string" "iac_random" {
      + id          = (known after apply)
      + length      = 10
      + lower       = true
      + min_lower   = 0
      + min_numeric = 0
      + min_special = 0
      + min_upper   = 5
      + number      = true
      + result      = (known after apply)
      + special     = true
      + upper       = true
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

random_string.iac_random: Creating...
random_string.iac_random: Creation complete after 0s [id=KtTDDuU]N6]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
iac-server $ 
iac-server $ 
iac-server $ 

Why does it says providers required by state is only local and not random .
As I understand we need random as local is already installed .
So what is a state and why random which is not installed is not mentioned here as required ?

Hi @ppnehe, it means that at that specific moment, since you didn’t have executed terraform init yet, that’s what the current state requires.
If you run terraform init and then terraform providers, in this scenario, then the random provider would be listed as required by the state.

Regards,
Vitor Jr.
KodeKloud Support