Terraform to the Rescue
Learn how Terraform can help solve the issues that arise from configuring infrastructure manually.
We'll cover the following
Solve problems using Terraform#
Terraform solves all of the problems mentioned in the last lesson by defining infrastructure in code. The code represents the state of your infrastructure. When you run Terraform against your code, it will update your environment to exactly reflect what you have specified in the code. This means Terraform’s results are reproducible every time.
Environment change#
Terraform can make all of the changes to your environment very quickly. No longer do you have to wait for days whilst someone follows the run book by hand. A change is made to the code, merged, and Terraform instantly updates every environment simultaneously to include the new change.
As your infrastructure is now defined in code, you can check it into source control. This means that you can change your code, roll it into an environment using Terraform, and try it out. If the change is not good, you can go back to the previous version of the source control and rerun Terraform. Then Terraform handles restoring the environment back to how it was. If the change is good, you can check that into source control and roll it into your other environments.
Multiple instances of the same configuration#
Having your infrastructure in code has another major benefit, in that it allows you to easily create multiple instances of the same configuration in multiple environments. All of the instances can be created quickly and all will be identical. Being able to create multiple identical environments is a big competitive advantage as it means that each team can have its own environment. You could even have one per person if you wanted! You know that the environment you are testing your software on is exactly the same as production, so there are no sudden surprises due to environment drift.
Two parts of Terraform#
Terraform is actually split into two parts:
-
One part is the Terraform engine that knows how to get from the state your infrastructure is currently into the state you want your infrastructure to be in.
-
The other part is the provider, which talks to the infrastructure to find out the current state and make changes using the infrastructure’s API.
Due to the clever way Terraform is split, there are providers available for practically anything you can think of. This allows you to use Terraform to configure infrastructure in AWS, Azure, GCP, Oracle Cloud Platform, and just about any other cloud you can think of. It can also be used to configure a huge variety of other components that make up your environment, such as Kong, Postgres, Runscope, Auth0, Couchbase, and TeamCity. If for some reason there is not a provider for it, you can write your own and use that in your project.
Terraform language#
Terraform uses a language called Hashicorp Markup Language, or HCL as it is known. HCL is a very simple, easy-to-read syntax that is understandable even to people looking at it for the first time. This makes it straightforward to read through the code that defines the environment and work out what it is going to do.
Configure multiple components using Terraform#
This means that you can configure multiple components and infrastructures in a single project even though they sit in multiple clouds using the same language (HCL). This allows you to define every aspect of your environment in the same project and Terraform can work out the order to run and configure each component for you.
Terraform community#
Terraform has a massive online community which means that help is never far away. If you have a problem, the chances are good that you will be able to find a solution from the community. The community also contributes to the catalogue of providers, which is a big reason why there is such a breadth. Since the providers are often open-source, you can raise issues you find on the provider repository and get answers on an issue from the provider authors themselves. Often bugs and issues are quickly fixed.
You can even fix the provider yourself and run a local fixed build if you need the provider fixed straight away. Due to the way providers are built and run, they are normally very quick to encompass new changes to an infrastructure API.
Terraform security#
Terraform allows you to see a preview of what it is going to do (plan) and awaits your confirmation before it actually makes any changes. This is a great safety net in case you made an accidental change. This gives you an insight into how Terraform will update your environment to match your desired state (we will cover Terraform plans in much more detail later on).
Destroy an environment using Terraform#
When using Terraform, you can destroy an environment and be guaranteed that there will be nothing left. This means no more unwanted bills for that piece of infrastructure you forgot to delete. Terraform can calculate the dependency order that an infrastructure needs to be deleted automatically and very quickly.
Add Terraform to an existing infrastructure#
Terraform has a solution if you already have the infrastructure and want to start using Terraform to manage it. You can do this simply by importing your infrastructure into Terraform. This is great as it allows you to move your infrastructure from being manually set up to be defined in code.