Using multiple providers in Terraform configuration file
- The first thing that we have to do after creating the configuration file with terraform is to run the terraform init command
- The terraform init command will check the configuration file and initialize the providers needed to let terraform interact with the infrastructure
1. Working with the same providers
- As long as we still work with the same providers, there is no need to execute this command again
- for example, we can add in the configuration file another resource block for creating another file
* for the resource name: i will call it "count"
* for the filename: i will call it 'count.txt'
* and for the content: i will store in this file this value "50"
-now, since the local provider was already initialized, there is no need to execute the terraform init command again
- so let's run the terraform plan command to see the execution plan
- we are happy with that, let's run now the terraform apply command
- so now, a new file called count.txt was created
2. Using a new provider
- we will use now another provider, called random, which is a logical provider, and that means
that it works entirely within terraform's logic and it does not interact with any other service
- using this provider, we can create a set of resources such as a random_id, a random_interger,
a random string and a fiew other resources
- in our situation, we want to use the random provider to generate a random integer
- so, let's add here a new resource block. the resource type is random_integer
- let's give a name to our resource, for example random_int
- here the required arguments are the max and min arguments
so for example min = 10 and max = 20
- if run now the terraform plan command, we will get an error telling us the we are using a provider
that is not already initialized which is the random provider
-so when we make use of a new provider that is not already initialized we have to run the terraform
init command to initialize this provider.
- so let's execute the terraform init command to initialize the random provider
- we see here in the output of this command that terraform install the random provider and make use
of the previously installed local provider
- we can verify that by checking the content of the .terraform folder. so now two providers are
initialized the local provider and the random provider
- let's see now the execution plan with the terraform plan command.
- so you see here that a new resource of type random_interger will be created
- let's now apply the execution plan using the terraform apply command
- now a random integer was created and you can see this integer using the terraform show command
-so the lesson here is that whenever we make use of a new provider, we have to run the terraform
init command to initialize this provider
Comments
Post a Comment