Initialize Terraform
Having installed Terraform and added its location to your system's path, you can now create a new Terraform configuration and initialize it. If you plan to work through the examples in the coming pages, do the following on the computer where you will run Terraform:
- Create a new directory to contain your example configuration.
- Download the file
main.tf
and place it in your configuration directory. Note that throughout this topic, whenever you are directed to download a file like this, left-clicking on the link will automatically download the correctly named file. You can then move the file from your default download directory to your Terraform configuration directory. - Copy the openrc.sh file for your desired Jetstream2 project into the configuration directory.
- Open a terminal/shell and change your current directory to be the configuration directory.
Each Terraform configuration directory may contain multiple Terraform files, which have the extension .tf
.
In the coming pages we will be adding several of these files to your configuration directory,
but for now there is just one (main.tf
).
That file is different from those to come in that it only contains
information needed to initialize the configuration:
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "1.54.1"
}
}
}
provider "openstack" { }
provider "null" { }
The two "blocks" of code in this file specify the required version of Terraform and the
provider requirements for this configuration.
You can see that a provider named "openstack" appears in that list (line 3),
even though its definition comes later, in the second block (line 10).
This works because Terraform reads all .tf
files in the directory before resolving references between blocks.
The internal name "openstack" will be referenced by code in the coming example files, so you should not change it here.
The "null" provider will not be used until much later in this topic, but it is specified here for consistency.
In many Terraform examples that you may see, the provider blocks will contain several assignments
that state the URL for the cloud platform, the credentials to use when accessing it, etc.
Such assignments can be made for the
OpenStack provider,
but we are not using them in this example.
Instead, those values will be gathered by the provider from environment variables that are set in your openrc.sh
file.
Once you have downloaded the file to your configuration directory, run the command terraform init
to initialize the configuration.
The command's output should indicate that the OpenStack and "null" providers are being downloaded into your configuration.
That provider comes in the form of an executable file that is stored in your configuration's .terraform
sub-directory.
A file named .terraform.lock.hcl
is also created to remember Terraform's initialized state.
In general, you should not edit or remove files or directories that are created by Terraform in your configuration directories.
At this writing, Terraform prints a warning for the line that includes the OpenStack provider (line 10), but you can ignore this.
If you receive error messages when running init
,
check the names and assignments in your main.tf
file and try again.
Once the command succeeds, your Terraform configuration directory is ready to use.
Note that you only need to perform the initialization of any given configuration once,
unless you change values in the "terraform" or "provider" blocks.