2a-Terraform Basics
2a-Terraform Basics
All interactions with Terraform occur via the CLI. Terraform is a local tool (runs on the current machine).
The terraform ecosystem also includes providers for many cloud services, and a module repository.
Hashicorp also has products to help teams manage Terraform: Terraform Cloud and Terraform Enter-
prise.
• terraform init
• terraform validate
• terraform plan
• terraform apply
• terraform destroy
These commands make up the terraform workflow that we will cover in objective 6 of this course. It
will be beneficial for us to explore some basic commands now so that work alongside and deploy our
configurations.
You can get the version of Terraform running on your machine with the following command:
terraform -version
If you need to recall a specific subcommand, you can get a list of available commands and arguments
with the help argument.
terraform -help
1
Task 2: Terraform Init
Initializing your workspace is used to initialize a working directory containing Terraform configuration
files.
Copy the code snippet below into the file called main.tf This snippet leverages the random provider,
maintained by HashiCorp, to generate a random string.
main.tf
Once saved, you can return to your shell and run the init command shown below. This tells Terraform
to scan your code and download anything it needs locally.
terraform init
Once your Terraform workspace has been initialized you are ready to begin planning and provisioning
your resources.
Note: You can validate that your workspace is initialized by looking for the presence of a
.terraform directory. This is a hidden directory, which Terraform uses to manage cached
provider plugins and modules, record which workspace is currently active, and record the last
known backend configuration in case it needs to migrate state. This directory is automatically
managed by Terraform, and is created during initialization.
The terraform validate command validates the configuration files in your working directory.
To validate there are no syntax problems with our terraform configuration file run a
terraform validate
Terraform has a dry run mode where you can preview what Terraform will change without making any
actual changes to your infrastructure. This dry run is performed by running a terraform plan.
2
In your terminal, you can run a plan as shown below to see the changes required for Terraform to reach
your desired state you defined in your code. This is equivalent to running Terraform in a “dry” mode.
terraform plan
If you review the output, you will see 1 change will be made which is to generate a single random
string.
Note: Terraform also has the concept of planning out changes to a file. This is useful to ensure
you only apply what has been planned previously. Try running a plan again but this time passing
an -out flag as shown below.
This will create a plan file that Terraform can use during an apply.
Run the command below to build the resources within your plan file.
Once completed, you will see Terraform has successfully built your random string resource based on
what was in your plan file.
3
Terraform can also run an apply without a plan file. To try it out, modify your main.tf file to create a
random string with a length of 10 instead of 16 as shown below:
terraform apply
Notice you will now see a similar output to when you ran a terraform plan but you will now be
asked if you would like to proceed with those changes. To proceed enter yes.
Terraform used the selected providers to generate the following execution plan. Resource
following symbols:
-/+ destroy and then create replacement
Enter a value:
Once complete the random string resource will be created with the attributes specified in the main.tf
configuration file.
The terraform destroy command is a convenient way to destroy all remote objects managed by
a particular Terraform configuration. It does not delete your configuration file(s), main.tf, etc. It
destroys the resources built from your Terraform code.
4
Run the command as shown below to run a planned destroy:
You will notice that it is planning to destroy your previously created resource. To actually destroy the
random string you created, you can run a destroy command as shown below.
terraform destroy
5
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value:
Note: As similar to when you ran an apply, you will be prompted to proceed with the destroy by
entering “yes”.