-In this lecture,
we will see how to use multiple providers and resources in Terraform.
Until now we have been making use of a single provider called "local"
to deploy a local file and the system.
Terraform also supports the use of multiple providers
within the same configuration.
To illustrate this,
let's make use of another provider called "random".
This provider allows us to create random resources such as a random ID
or random integer or random password, et cetera.
Let us see how to use this provider and create a resource called random_pet.
This resource type will generate a random pet name when applied.
By making use of the documentation,
we can add a resource block to the existing main .tf file like this.
Here,
we are making use of the resource type called random_pet.
In an earlier lecture,
we saw that the resource type can be broken down to two parts.
The keyword before the underscore is the provider
which in this case is random.
The keyword following it is the resource type which is the pet.
Let's call this resource "my pet".
Within this resource block, we will use three arguments.
One is the prefix that is to be added to the pet name.
The second argument is the separator between the prefix
and the pet name that is generated.
The final argument is the length which is the length of the pet name
to be generated in words.
A main .tf file now has resource definition for two different providers,
one resource of the local file type that we have already created earlier
and another resource of type random_pet.
Before we generate an execution plan and create these resources,
we have to run the Terraform init command again.
Now, this is a mandatory step,
as the plugin for the random provider should be initializing
the configuration directory before we can make use of it.
In the command output of the Terraform init,
we can see that the local provider was previously installed
and it will be reused.
The plug-in for the random provider, on the other hand,
will be installed as it was not used before.
We can now run the Terraform plan to review the execution plan.
As expected, the local file resource called "pet"
will not be updated as it is unchanged from the previous apply.
A new resource by the name of "my pet" will be created based
on the new resource block that we just added.
Now let's apply the configuration using Terraform apply.
As expected, the local file resource is left as it is but now
a new resource has been created which is called "my pet".
Random provider is a logical provider and it displays the results
of the pet name on the screen like this.
Here, an attribute called ID which contains the name of the pet
is written by the apply command.
Before we move on, please know that in our illustration,
the dog icon stands for a pet and we'll be making use of it
throughout this course.
The random_pet can generate any pet name and it does not have to be specifically a dog.
Now, let's head over to the hands-on labs and explore working
with multiple providers in Terraform.