Variable Defaults
This lesson will teach you how to set defaults in Terraform variables and their use.
We'll cover the following
Project example#
Consider the following project:
We have extended the first example and added a second variable "bucket_suffix"
which has its default set to "-abcd"
. Setting a default on a variable
means that if you do not provide a value for that variable, then the default will be used. We then use the value of the bucket_name
variable concatenated with the value of the bucket_suffix
variable for the value of the bucket name. As we use the values inside a string, we need to surround our variables with ${
and }
. Otherwise,
Terraform will not use the value of the variable
and instead will just print the string var.bucket_name
.
Running the project#
Clicking the terminal will run the terraform init
command.
After running the project:
-
Terraform will ask you to provide a value for
bucket_name
as before. -
Enter a name for the bucket and press enter.
Notice that Terraform will now ask you to confirm the run by typing
yes
. Confirm the run and press enter. -
Terraform will then create the bucket.
You may be wondering why Terraform never asked you for a value for
bucket_suffix
. This is because Terraform does not need a value forbucket_suffix
since you already gave it a default value of-abcd
. -
The end result is that a bucket will be created with whatever name you enter for the bucket name with
-abcd
on the end of it.
📝Note: Once you are done with the project, do not forget to run
terraform destroy
and then confirm with yes to delete all of the resources in this project.