Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Terraform for Google Cloud Essential Guide

You're reading from   Terraform for Google Cloud Essential Guide Learn how to provision infrastructure in Google Cloud securely and efficiently

Arrow left icon
Product type Paperback
Published in Jan 2023
Publisher Packt
ISBN-13 9781804619629
Length 180 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Bernd Nordhausen Bernd Nordhausen
Author Profile Icon Bernd Nordhausen
Bernd Nordhausen
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Part 1: Getting Started: Learning the Fundamentals
2. Chapter 1: Getting Started with Terraform on Google Cloud FREE CHAPTER 3. Chapter 2: Exploring Terraform 4. Chapter 3: Writing Efficient Terraform Code 5. Chapter 4: Writing Reusable Code Using Modules 6. Chapter 5: Managing Environments 7. Part 2: Completing the Picture: Provisioning Infrastructure on Google Cloud
8. Chapter 6: Deploying a Traditional Three-Tier Architecture 9. Chapter 7: Deploying a Cloud-Native Architecture Using Cloud Run 10. Chapter 8: Deploying GKE Using Public Modules 11. Part 3: Wrapping It Up: Integrating Terraform with Google Cloud
12. Chapter 9: Developing Terraform Code Efficiently 13. Chapter 10: Google Cloud Integration 14. Index 15. Other Books You May Enjoy

Terraform workflow

A basic Terraform workflow consists of the following two steps:

  1. Init
  2. Apply

In this first step, terraform init initializes Terraform and downloads all the necessary configuration files. You see that after init is run, there is a hidden directory named .terraform. This directory is where Terraform stores various configuration files. You must run the initialization step every time you write a new configuration file. Don’t worry; you can run this command multiple times, and you learn over time when you need to rerun it. If in doubt, run it again as it only takes a few seconds.

The second step, terraform apply, consists of two phases. First, Terraform creates an execution plan, then it executes this plan. The execution plan is an internal plan of the actions Terraform will perform and in which order. Terraform outputs this plan, including a summary of how many resources will be added, changed, and destroyed. You should always review this output carefully. Once you have confirmed that the plan does what you intended, you can confirm, and Terraform actually provisions the cloud resources.

You can also run the two phases explicitly by running terraform plan, saving the plan in a file, and then running terraform apply against that file, like so:

$ terraform plan --out=plan
$ terraform apply plan

This is often done in CI/CD pipelines, and we will return to it later.

One of the advantages of IaC is that you can quickly remove resources. This is not only useful during the development phase, as you can test things quickly, but it also can help you save costs. At the end of the workday, you can remove all the resources and reprovision them the next day. To destroy resources in Terraform, simply execute the following command:

$ terraform destroy

Now, after you have run terraform destroy and run terraform apply twice, you’ll notice that the second time you run terraform apply, Terraform reports no changes, and no action is performed. The next chapter discusses how Terraform decides what actions to take.

There are many more Terraform commands, and we introduce them throughout the book. For now, remember the following four commands:

  • terraform init to initialize
  • terraform plan to view and create a Terraform plan
  • terraform apply to actually provision the resources
  • terraform destroy to remove all resources

Now that we have described the basic workflow of Terraform and shown how you run Terraform in Google Cloud Shell, let’s look at the different ways of running Terraform on your local computer.

You have been reading a chapter from
Terraform for Google Cloud Essential Guide
Published in: Jan 2023
Publisher: Packt
ISBN-13: 9781804619629
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image