How Do You Master Terraform

Griffith by MarinaPRIV Griffith berserk, Interesting drawingSo you've heard about this Terraform thing and want to get in on the action? Learning a new technology such as Terraform can be a daunting task at first. Today we're going to go through the best way to learn Terraform so that you can break through the fog of uncertainty and start learning today. I strongly believe that all infrastructure should be written as code (if you don't know what that is, don't worry we'll cover that soon!). I also know that cloud computing is the future of software engineering. So it's fair to say that you're making a great choice starting down the path to learning Terraform.For today's article I won't give you all the answers to what Terraform is and how to use it (that would be a HUGE article) but instead I'm going to give you a map - a guide - to help you navigate your learning journey and a good starting point. Which should make the process of learning Terraform less daunting. Let's take a quick look at the structure of what we're going to cover today:Get An Overview Of Terraform - We'll need to learn what Terraform is capable of (at a high-level) in this section we highlight the main functionalities of Terraform and start to understand what it is.Learn The Foundations Terraform - Now we'll want to understand the principles that underly Terraform to understand why it's built the way it is and understand what differentiates it from the competitors.Learn The Cloud (Or a vendor) First - Terraform comes with the added difficulty of needing to understand another tool to work with. We discuss how you should tackle the problem by learning your vendor first.Learn Terraform Basics - In this section we'll move onto and highlight the main Terraform topics that you need to know. This is your map of the main important Terraform skills to learn.Learn Advanced Terraform - Lastly we'll discuss the more advanced features of Terraform. We cover these features so you know about them, but you can safely ignore them when you're starting out.

39;ve learned Terraform we can use our skills and apply them to different cloud providers.

Prohlédnout recept Wait - hold up. Before we get into it let's quickly define what Terraform is. Terraform is a CLI based infrastructure as code tool that allows you to script the creation, updating and deleting of (mainly) cloud resources. With your resources managed in code you can enjoy benefits such as: applying code review for changes, using the code as documentation for new joiners and having repeatability in the updates to your infrastructure. What makes Terraform especially great is that it's a universal language (figuratively) ) that can be applied to all cloud vendors, it's vendor agnostic. What that means is once we've learned Terraform we can use our skills and apply them to different cloud providers. Been using Terraform with AWS? It's now simple to start writing Azure resources too. And with the definition out of the way, let's get into the details of the best way to learn Terraform, starting with an overview of Terraform.1.

When it comes to learning something it makes sense to first understand the map of what's to come; to understand what lies ahead. If you're reading a textbook you would flick back and forth through the pages, look at the titles and spoil the ending, right? And whilst we don't have a book, we do have an article! So let's take a look at the high level features and functionality that you'll need to know about Terraform. I'll quickly answer / write about each of the topics so you have an idea what it's about. But don't worry if it's a little overwhelming, the point here is to just get you acclimatised to the different words and ideas that you'll meet when you learn Terraform. Terraform is a CLI - That means you can download it and run it from your machine. Terraform is vendor agnostic - You can use Terraform with a range of third party vendors, not just one.Terraform is declarative - It declares the resources to create and infers the dependencies (more on this later).Terraform is different than it's competitors - Many other infrastructure as code tools are imperative, so they're different to Terraform. Terraform uses providers for third party integrations - A provider is what allows Terraform to integrate with different vendors. Terraform uses state - Terraform generates a state file on run to manage data about your infrastructure.

Whilst we're not going to cover all these topics in deep detail today if you still find yourself itching for a physical book to flick through to get an overview of Terraform then I can wholeheartedly recommend reading Terraform Up & Running By Yevgeniy Brikman.I personally learned Terraform by reading the book and experimenting with Terraform itself. It's very comprehensive and gives you a great walkthrough, genuinely one of the best technical books I've read. What Is Declarative Infrastructure as Code? So let's answer these questions now. What Is Infrastructure as Code? Infrastructure As Code is the process of writing Infrastructure In Code (well, duh). And in this context, infrastructure refers can refer to servers, databases, etc. What Is Declarative Infrastructure as Code? Code (infrastructure code too!) can either be declarative or imperative. Declarative code is code that describes an end state, but not how to get there. The imperative describes the steps to get somewhere, but not necessarily the end state.

39;re here because you want to use Terraform with AWS.

By now you might be thinking: Why should I bother to learn the differences? What does it have to do with Terraform? So let me explain… Terraform is a declarative infrastructure as code tool. That means in Terraform we define our target end state and Terraform works out how to get us there. So let's do that. Still a bit confused? Check out the following article for more details Declarative vs. When using Terraform we're looking to provision using a vendor such as AWS. And so are you, right? I imagine that you're here because you want to use Terraform with AWS. Or maybe with Azure. Or maybe something similar. But the simple fact that Terraform works with many vendors poses a problem to us when we're learning Terraform. Why? Let's take a look… When you're learning Terraform you're going to come into contact with a lot of new topics and ideas. But if you're also learning a vendor or cloud provider at the same time as Terraform it could get really painful. The solution is to ensure you know your Cloud provider well enough before starting to put things together in Terraform. To understand why I emphasize the point of learning your vendor first let's take an example. Let's pretend that you're using Terraform to configure an S3 bucket on AWS to be used as a static website. When you're creating your bucket you'll get many options… What bucket policy do you want?

What CORS rules?- What tags?

Do you want logging enabled? What region do you want? What CORS rules?- What tags? As you can see there are lots of options for configuring an S3 bucket. And this is only a humble S3 bucket, one of the simplest resources you can use - it can in fact get a lot more complicated. If you're setting up networking for instance, there's a lot of prior knowledge you'll need. And that's why one of the best head starts you can get for learning Terraform is to understand your vendor well first. A simple way to learn your vendor is: Setup a sandbox account and manually provision resources. By configuring things manually you'll soon start to understand how the resources work, and what their options actually do.Remember: When you're finished with your experiments nuke the account to contain any mess and avoid large bills. Seriously, don't be tempted to use these manually created resources, it could make your life hell in future.For a simple first project, creating an S3 website is a good start. By now you should have a good overview of Terraform.

What Is a Terraform Provider?

But what are those main things? To give you a sneak preview, they are:- What is a Terraform Provider? What is a Terraform Plan? What is a Terraform Apply? How do we structure a Terraform project? What is Terraform State and why do we need it? As before, we won't cover these in great detail, but we'll cover them with enough detail to give you a good picture of what the concept is before you dive in. What Is a Terraform Provider? A provider is Terraform's way to know what vendor you want to integrate with and create / manage resources on. For instance: If you want to use Terraform with AWS, you'll need the AWS provider. Or if you want to use GCP you'll need the GCP provider.A provider is usually an open source plugin. When you add the provider code (like in the above image) terraform is smart enough to know that you want to pull in that third party plugin.

39;ve written produces our expected output.

So when you run a terraform init command it'll go and fetch you the right assets.And of course with each provider you'll likely have to provide some access credentials, for this you should consult the documentation for your vendor. Providers usually accept credentials that are stored as environment variables. What is a Terraform Plan? Terraform works through the following process:1. Setup Terraform with access to your vendor2. Write your resource code (tell Terraform what resources you want)3. Execute a plan (so Terraform knows what you want to do)4. Apply the plan (to tell Terraform to make the changes)5. A plan in Terraform is therefore a way of performing a dry-run on your code changes. It's kinda like running tests against code. We're validating whether what we've written produces our expected output. If you've already got existing infrastructure and you refactor your Terraform code re-running a plan should show that no changes are to be applied. A Terraform Plan is how Terraform lets you know what changes are going to be applied. Before making any changes you'll often run a Terraform Plan to see what difference your changes will make to your infrastructure. And since we've discussed Terraform Plans, an apply is quite simple. One of the main things you'll need to know when you get going is how to lay out your file structure.

Terraform can either create state locally in a file or it can be pushed remotely.

But it's pretty easy. When your run Terraform commands, Terraform will search the current directory for any files with the.tf extension.There are no required files or naming in Terraform but you will likely need a provider block we discussed before in order to do anything productive. If you want to group all your resources together in a single file you can, or you can start to break them down into different files to make life easier for you. What is Terraform State? A key thing you'll need to understand about Terraform is how it manages state. Terraform state is how Terraform compares between your requested resources and your actual resources so that you can effectively plan new resources and delete old ones. Terraform state in reality is just a big JSON file. Terraform can either create state locally in a file or it can be pushed remotely. When you know these few parts you've got all you need to start creating resources using Terraform. Now with the basics out of the way let's take a look at some of the more advanced features of Terraform. You won't need these features right away but it's useful to know they exist for when you do need them in future.5. Once you've got the Terraform basics down, you can start to look at the more advanced stuff.

Another way to tackle this list is to treat it as: things you can safely ignore when starting Terraform. In short: Everything presented below is not mandatory for most small projects. You could get by for a long time without ever needing to know these topics. Terraform Modules are a way that you can encapsulate shared code. A Terraform Module acts very similarly to a programming function. The module takes inputs, does something and then produces outputs. By refactoring parts of your infrastructure into Modules you can easily enforce standards and keep resources in sync. But they can be fiddly. Another important point to note is that everything in Terraform is a module. The first files you write in your Terraform project are part of what's called the root module. But when it comes to learning Terraform it's not really important to understand modules at first. As we said earlier, Terraform State is a file that Terraform generates when it is ran to collect information about your resources.


Related posts