Skip to content

Terraform Lifecycle

Now that we have defined our Terraform, run the following commands within the Terraform lifecycle and analyze the output from each one.

Where is Terraform running? Although you will be running these Terraform commands locally, it will be executed on the TFE server in the cloud. The command you run locally will give you a link to view the run natively on the actual TFE server. Recall standards to run the Terraform lifecycle only in the pipeline, which we will setup in the next lab.

First, let's navigate to our Terraform directory before running our commands.

cd terraform

terraform fmt

The terraform fmt command is used to rewrite Terraform configuration files to its proper HCL format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability. (docs)


terraform init

The terraform init command is used to initialize a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times. (docs)

Terraform Init


terraform validate

The terraform validate command validates the configuration files in a directory, referring only to the configuration and not accessing any remote services such as remote state, provider APIs, etc. (docs)

Terraform Validate


terraform plan

The terraform plan command is used to create an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files. (docs)

It is possible that your local version of Terraform and the version running on Terraform Enterprise do not match and could throw an error when running terraform plan. In this case, continue to run apply.

Terraform Plan


terraform apply

The terraform apply command is used to apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a terraform plan execution plan. (docs)

Terraform Apply 1

After running terraform apply, you will get to the output and will be prompted for yes/no to apply the changes to your infrastructure. This manual validation is built-in to require human input. Type yes to create the infrastructure.

Manual or auto-approve Terraform changes? Terraform will always be run in the pipeline and requires a manual approve within Terraform Enterprise. We will see this in the next lab.

After applying your Terraform, you will see your infrastructure be created in the logs of the terraform apply and will see that two resources were created.

Terraform Apply 2

Awesome! Let's now see it in TFE!

Terraform Enterprise

The Terraform execution will provide you with a link to view the execution in TFE. This will show you where your Terraform code is actually being executed from, which is inside the TFE Workspace. Follow the link to view the Terraform execution in TFE.

Terraform Apply 3

From here, you can navigate to the various stages of the Terraform lifecycle within TFE, along with viewing the versioned state files and other Terraform configuration.

Awesome! Let's now see what we created in Azure!

Azure Portal

After validating the run on Terraform Enterprise, check the Azure Portal and verify that resources were created as expected. You will need to login and view the subscription.

You should see the App Service and App Service Plan inside our RG-Dojo-Sandbox resource group. By clicking into the App Service, you can find the public URL. Click on that URL to see your new infrastructure deployed to Azure.

Awesome! We launched a new App Service using infrastructure as code!

Note that there is an error message when visiting the domain. This is because we have not deployed an application on top of the App Service yet.

Tear down resources

Once completed, tear down all the resources.

terraform destroy

The terraform destroy command is used to destroy the Terraform-managed infrastructure. (docs)