Terraform Tutorials: Frequently Asked Questions
By: Raphael Socher
Tags: Devops Training, DevOps, Terraform, Devops Practice

1. What is Terraform and why do we need it?

Terraform is an open-source tool that was designed to enable the creation, modification, and even deletion of infrastructure on the cloud. It mainly works on the principle of “Infrastructure as Code”. In an era where DevOps processes are booming, Terraform has been revolutionizing the process with its simple, yet efficient ways of managing infrastructure through code. It isn’t restricted to any cloud provider and can be used to write configurations for most of the cloud providers.

2. In which language was Terraform coded and when was it initially released?

It was coded in the “GO” language and can work efficiently on almost all the popular Operating Systems. It was first released in July 2014.

3. Can we consider Terraform as a DevOps tool?

Yes, of course. DevOps is a complete cycle or process that involves both the development and the operations team. A very critical part of this process is setting up the right infrastructure, and it’s one the best to do so.

4. How do I use Terraform?

You may download the CLI from the official website of HashiCorp. Once you have the CLI installed, you may use the declarative HashiCorp configuration language (HCL) to write your configuration files or Terraform templates, that can be in “.tf” or “.tf.json” format. With the Terraform templates ready, you can initialize and apply your configurations.

5. Which version should I use?

You may use any version, however, the version from 0.12 is available with the newer configuration options and more flexibility. It is available as an open-source, Cloud, and Premium or Enterprise version.

6. What is Terraform Enterprise and Cloud?

Terraform Cloud is a cloud service provided in order to enable a team to have a shared state and also an approval flow towards the changes made on the infrastructure. Enterprise provides your organization with your own private instance of cloud and there are no restrictions on the resources and a few other additional features.

7. Which cloud providers are supported?

All major cloud providers such as AWS, Azure, GCP are supported. You may access the full list of cloud providers by clicking here.

8. What are some useful commands?

There are many commands for different purposes, but the most commonly used commands are “init”, “plan”, “apply” and “destroy”.

9. How does Terraform work internally for tfresources?

Every time you create a template or a configuration file and initialize and apply the configuration, a remote object for a particular resource is created on your system. The link between the remote object and your resource is saved in the Terraform state file. Whenever you update a resource on your configuration, a new remote object is created. This object gets mapped against the resource in the Terraform state file, and the previous remote object is deleted. Therefore, every time you update a resource, you’re also creating a new remote object.

10. Is it possible to add sensitive information to my configuration file?

Yes, you can include information such as credentials or token information in your configuration. But, we recommend that you don’t store sensitive information in the “.tf files” on your system. We encourage storing the files on remote storage, such as your cloud provider or on Terraform Cloud so that every bit of information remains encrypted.

11. Can we clone an existing infrastructure in Terraform?

There is a provision to clone using the command “terraform import”. For now, this is restricted to cloning only the Terraform state file and you’ll have to write the rest of the configuration (such as resources, modules, and blocks). It’s also important to note that if your configuration is from a different tool, you can’t import your infrastructure. Instead, you’ll have to map your complete configuration to a Terraform template.

12. What is the Terraform state file?

A Terraform state file stores the configuration/resources that you create or modify on a cloud provider, in JSON format. It is responsible for how the current resources are recognized. Also, to ensure that your complete team maintains the same Terraform state file, we suggest storing it in cloud storage rather than in your local system.

13. How can I deploy my infrastructure on the cloud?

The very first step for you would be to create a template with tfvariables, providers, resources, and other configurations. Once you have them in the right modular structure, you will have to initialize your working directory which will start with your “main.tf” file. You can then test whether the resource allocation meets your needs by using the “plan” command. If everything seems to be in place, you can apply the changes and your infrastructure will be deployed.

14. What are Terraform best practices I can follow when using tfvariables?

Stick to using the correct data type: It ensures the right results and less bugs in your configuration.

Use your variables: Declaration is not usage. Don’t just declare them in your “tfvars” file and forget them. Use them wisely in your configuration.

Name your variables right: Once you have your infrastructure deployed, you are not going to just forget it. You or any other member of your team will eventually make changes to the infrastructure. Therefore, name your variables correctly and always have a description argument to accompany it, which will help you make changes in the future.

Sensitive flag: If your file contains sensitive data, make sure to have the sensitive flag in the output values file and store your Terraform state file on cloud.

15. What is a Terraform template and what does it consist of?

A template is a collection of your configuration files. These files, together, define your infrastructure. The template has a resource file, provider file, variables file and modules as the most basic elements.

16. Since it is open-source, can I contribute towards the development of Terraform?

HashiCorp does allow you to place pull requests. However, there is no guarantee that your pull request will actually be merged post review or when it will be reviewed. It will be merged only if your request adds value to the current changes or updates planned. If you are willing to contribute and are sure your pull requests are of value, go ahead — after all, it’s open source!

17. What are modules? Are modules and functions the same?

Terraform modules act as containers and contain all similar resources and variables. One module may contain or nest another module. A parent module can also call a child module. When compared with an overall view of functions in other programming languages, we can say that modules are similar to functions but they are not the same. Modules are user-defined and have to be defined by us. Functions here are only in-built and cannot be defined by us.

18. Can I add version control to my providers?

There is a provision to mention the version as an argument inside the “required_providers” block

19. What are some common issues I might face?

No matter how efficient a tool and the team working on it is, it is always good to keep in mind these issues:

Handling the Terraform state file: The state file is one of the most important files, but it’s also a file that you need to be extremely careful with when handling. The possibility for disaster increases when you begin to make manual changes to the Terraform state file — if you make a wrong move, the configuration will not sync. Tip: Always have the Terraform state file stored in a remote location, and always have “state locking” enabled to protect yourself.

Renaming of resources and variables: When you constantly rename resources and variables, it becomes impossible for the Terraform state file to keep track of this, and might result in hours or days of manual work to get the files back on track.

Limited application of “import” command: The “import” command currently lets you only clone the Terraform state file and not the complete configuration. Also, when you have many resources, the usage of the command becomes problematic as it might need to be used multiple times.

Avoid complicated module structure: Nesting multiple modules and complicating the structure also complicates any changes that you or any other engineer working on the configuration plan to make in the future. There might be stray or missing tfvariables and resources in modules when you try updating the infrastructure in the future. So, try to keep your modules as simple as possible.

20. Is additional documentation necessary with my configuration files?

The major advantage of being a declarative language is that it is very easily readable and usually does not need any additional documentation. Your code becomes your documentation! But it is a good practice to always add comments in your configuration file and also have a README file. This makes your documentation stronger and allows better understanding and easier changes to anyone who handles the configuration in the future.

This is only the beginning of our Terraform Tutorials: FAQ Series. Stay tuned as we go even deeper into topics that matter.

Have an idea for a topic for us to cover? Looking to get more help with your Terraform project? Join our Slack community to learn from Terraform experts.

👋 Join FAUN today and receive similar stories each week in your inbox! ️ Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬