Get startedGet started for free

Terraform Commands

1. Terraform Commands

Once Terraform is installed on your machine, you can use commands at your root module to interact with Terraform. Common commands covered in this module include terraform init, Terraform plan, Terraform apply, Terraform destroy And terraform fmt. terraform init is used to initialize the provider with a plugin. terraform plan provides a preview of the resources that will be created after terraform apply. terraform apply creates infrastructure resources. terraform destroy destroys infrastructure resources. And terraform fmt auto formats to match canonical conventions. The terraform init command is run during the initialize phase. It’s the first command to run after creating a configuration or checking out an existing configuration from version control. Terraform init ensures that the Google provider plugin is downloaded and installed in a subdirectory of the current working directory, along with various other bookkeeping files. The provider block includes a source attribute specifying where the provider plugins will be downloaded from. Terraform uses a plugin-based architecture to support the numerous infrastructure and service providers available. Each "provider" is its own encapsulated binary that is distributed separately from Terraform itself. The terraform init command will automatically download and install any provider binary for the providers to use within the configuration. In this case, the provider is Google. After you run terraform init, a hidden directory called . terraform is created inside the current working directory. You will see an "Initializing provider plugins" message, letting you know that Terraform will find the latest plugin and download the associated files. The output of the command tells you the provider version that Terraform has installed, in this case version 4.21. Terraform plan creates an execution plan detailing all the resources that will be created, modified, or destroyed upon executing terraform apply. When terraform plan is run, Terraform: Reads the current state of existing remote objects to ensure that Terraform state is up to date. Compares the current configuration to the prior state and notes any differences. And builds an execution plan that only modifies what is necessary to reach your desired state. Terraform plan does not actually create or change any infrastructure resources, but instead provides you with an opportunity to preview your infrastructure changes before applying them. For example, you might want to run this command before committing a change to version control to be sure that it will behave as expected. You can use the optional -out=FILE option to Optionally, you can save the generated plan to a file on disk, which you can later execute by passing the file to terraform apply as an extra argument. Terraform apply executes the actions proposed in a Terraform plan, creates the resources, and establishes the dependencies. The symbols next to the resources and arguments indicate the action performed on the resource. The plus next to the resource means that Terraform will create this resource. Terraform also shows the attributes that will be set. Minus slash plus means that Terraform will destroy and recreate the resource, rather than updating it in-place. The tilde means that Terraform will update the resource in-place. The minus indicates that the instance and the network will be destroyed. As with terraform apply, Terraform shows its execution plan and waits for approval before making any changes. If the plan is created successfully, Terraform pauses and waits for approval before proceeding. If anything in the plan is incorrect or unsafe, you can abort here without changing your infrastructure. If terraform apply fails, read the error message and troubleshoot the issues. Just like with terraform plan, Terraform determines the order in which things must be destroyed. For example, Google Cloud won't allow a VPC network to be deleted if it still has resources. Terraform waits until the instance is destroyed before destroying the network. Let’s cover some code formatting best practices: Separate the meta arguments from the other arguments by placing them first or last in the code with a blank line. Indent your arguments with two spaces from the block definition When two or more arguments are defined in a given block, align the values at the equal sign When a block includes a nested block, place the block following all the arguments. Finally, when your code includes multiple blocks, separate them with a black line for readability. Running terraform fmt on your modules and code automatically applies all formatting rules and recommended styles to assist with readability and consistency. Terraform fmt automatically maintains consistent formatting, so you don't have to manually change configuration to ensure it meets the standards. The last command to cover in this module is terraform destroy, which destroys resources. Terraform destroy is similar to terraform apply, but it behaves as if all resources have been removed from the configuration. Terraform is sometimes used to manage ephemeral infrastructure for development purposes. In this case, you can use terraform destroy to conveniently clean up temporary objects once you’re finished with your work. You can also destroy specific resources by specifying a target in the command. Destroying your infrastructure is a rare event in production environments. But if you're using Terraform to create multiple environments such as development, testing, and staging, then destroying is often a useful action. Use terraform destroy carefully - it will destroy any resource and the data associated with it. For example, if there’s data in a bucket, be careful when running terraform destroy, as that data cannot be recovered.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.