Aspiring to become a Terraform expert? Check out this exclusive Terraform cheat sheet that will help you become a Terraform pro!
Terraform is a productive tool that came out in 2014. It is quite essential for describing a complete infrastructure of an application or software through integrated measures. There are certain commands and codes that are commonly infused and called within the tool for specific functions.
It is evident that the beginners might experience a tough time mastering the commands and sub-commands of terraform. This IaC tool is highly intuitive and to get the best out of it, you will need to keep in mind the basic commands that are to be called within it. And for that, cheat sheets are considerably the optimal remedy to work on Terraform while you are progressing upon memorizing the commands.
Hence, this Terraform cheat sheet is destined to help serve that purpose and make you aware of those commonly used Terraform codes or commands.
Terraform CLI Commands – Terraform Cheat Sheet
The Command-Line Interface consists of commonly used, advanced, and less common commands. All of these commands and subcommands are used to call different functions within the tool. Let’s cover all possible commands within Terraform under this Terraform cheat sheet.
This image consists of some of the most commonly used Terraform commands that you must adapt and utilize in the right manner for accessing select functions. Apart from these, there are many other advanced commands and subcommands that you will explore further in this Terraform cheat sheet.
1. Terraform CLI Help
In case you need help with any of the commands within the tool, Terraform has a built-in help function that can be called by putting up specific help commands for the same.
The command is terraform <commandname> – help
For instance, if you are willing to get help on subcommand ‘state’, then you will have to run terraform state –help. You can do it with any of the subcommands embedded within Terraform. The commands and subcommands that are involved within the functionality of this tool are listed further in this Terraform cheat sheet.
2. Show Version Command
The show version command is the terraform version. The purpose of this subcommand is to show the installed version of Terraform.
Have you tried our Free Test for HashiCorp Certified Terraform Associate certification? Try today!
3. Initialize Infrastructure Commands
- terraform Init: It commands let the tool initiate the working directory.
- terraform init –input=true: It is the command to ask for a valid input if the code demands it.
- terraform init –lock=false: It is the command that is run for disabling the locking of state files during the dedicated operations.
4. Get Commands
- terraform get: It is the command to allow the tool for downloading and updating the existing modules within the root.
- Terraform get –update=true: It is the command under which the downloaded modules will be checked for available updates. If found, then those modules will be updated.
5. Provision Infrastructure Commands
- terraform plan: It is the command that works on creating an execution plan and implementing a DRY RUN.
- terraform plan –out=path: This command saves the created plan in the form of a file.
- Terraform plan-destroy: This command derives a destroy plan for the select resources.
- Terraform apply: This command is meant to implement the required changes to the existing infrastructure.
- Terraform apply- auto-approve: With this command, the changes made to the infrastructure will be implemented without any prompts.
- Terraform apply –refresh=true: This command is meant to update the resource states before the execution of the plan and applying the changes.
- Terraform apply –input=false: It is meant to request input of variables if they are not directly set.
- Terraform apply –var ‘foo=bar’: This command allows to set a multi-use variable within the configuration.
- Terraform apply –var-file=foo: This command is called for specifying files consisting of key pairs for the values that are variable.
- Terraform apply –target: This command is meant to apply or deploy the planned changes to the selected resources.
- Terraform destroy –auto-approve: With this command, the resource will be deleted or destroyed without any prompts.
- Terraform destroy –target: With this command, you can target specific resources or dependencies that are meant to be destroyed.
6. Terraform Workspace Commands
- Terraform workspace new: This command will allow you to create a new workspace and select it for further operations.
- Terraform workspace select: This command will allow you to select the existing workspace.
- Terraform workspace list: This command will display the list of all the existing workspace.
- Terraform workspace show: With this command, the name of the current workspace will be shown on the screen.
- Terraform workspace delete: It is meant to delete any empty workspace that you will target.
7. Format and Validate Terraform Command or Code
- Terraform fmt: This is the formatting code based upon the HCL canonical standard.
- Terraform validate: With this command, you get the potential to validate the configuration files for their use in the form of syntax.
8. Inspect Infrastructure Commands
- Terraform graph: This command creates a graph listing all resources within the configuration.
- Terraform output: This command is to list the root module outputs.
- Terraform output instance_public_ip: With this command, you can call for a listing of any specific output.
- Terraform output –json: It is meant to list the outputs, particularly in JSON formatting.
- Terraform show: This command can access the plan or state file for providing a human-readable or understandable output.
9. Terraform Import Commands
Terraform import aws_instance.foo i-xyz123: This command is meant to import an AWS instance of a specific ID. In this sample command, the ID is i-xyz123. It will be imported onto the aws_instance resource that is named as ‘foo’.
Try HashiCorp Certified Terraform Associate certification Practice Tests here before attempting the real exam!
10. Terraform State Manipulation Commands
- Terraform state list: It is meant to display all the resources in the form of a list within the state file.
- Terraform state list aws_instance.my_ec2: The resource name that you infuse within this command will be the only one to be listed.
- Terraform state mv: This command can be used for moving any specific item to the state file.
- Terraform state rm: This command is meant to remove the items from the select state file.
- Terraform state pull: This command will pull the present state file and output to stdout.
- Terraform state push: This command is meant to update any remote state from any of the local state files.
- Terraform state show aws_instance.my_ec2: It is meant to show the detailed attributes of any of the single resources.
- Terraform state refresh: It is meant to refresh the state file.
Read more on Terraform vs CloudFormation vs Ansible today!
11. Terraform Miscellaneous Commands
- Echo “1+5”| terraform console: This command is used to display a predicted result of any of the expressions as an output.
- Terraform taint aws_instance.my_ec2: It marks a particular resource as tainted. As a result of which it is forced to destroy and be recreated in the next APPLY run.
- Terraform untaint aws_instance.my_ec2: The tainted mark upon the resource is removed with this command.
- Terraform force-unlock LOCK_ID: The lock upon the state file of the ongoing configuration is removed.
- Terraform login: This command will get and save API tokens from the cloud.
- Terraform logout: This command will remove all the stored credentials during the login process.
Conclusion
This Terraform cheat sheet has covered almost all of the major commands that are used within Terraform for conducting different operations and accessing diverse functionalities. There is a lot to explore and master within Terraform and all you need is a bit of a reference to look at while starting with it. This cheat sheet article intends to be that working reference for you to initiate your work operations with Terraform. If you want to learn more about Terraform, enrol in our Terraform training course and enhance your knowledge to become a Terraform expert.
- Top 20 Questions To Prepare For Certified Kubernetes Administrator Exam - August 16, 2024
- 10 AWS Services to Master for the AWS Developer Associate Exam - August 14, 2024
- Exam Tips for AWS Machine Learning Specialty Certification - August 7, 2024
- Best 15+ AWS Developer Associate hands-on labs in 2024 - July 24, 2024
- Containers vs Virtual Machines: Differences You Should Know - June 24, 2024
- Databricks Launched World’s Most Capable Large Language Model (LLM) - April 26, 2024
- What are the storage options available in Microsoft Azure? - March 14, 2024
- User’s Guide to Getting Started with Google Kubernetes Engine - March 1, 2024