front matter


When Mitchell Hashimoto and I founded HashiCorp, we sought to build a portfolio of tools to cater to practitioners in the new cloud ecosystem. Provisioning was a critical piece, and we knew we wanted to build something special. When we designed Terraform, we had three goals in mind. First, we wanted a consistent and simple workflow, regardless of the platform. Second, we wanted to ensure high confidence and no surprises for users. Finally, we wanted the tool to be extensible so it could support just about anything.

I first met Scott as a conference speaker for HashiConf, our annual user conference. Scott was speaking about how Ellie Mae was using Terraform Enterprise and the patterns and best practices it had implemented to enable a large organization to adopt an infrastructure as code practice. Scott continues to be an active contributor to the Terraform ecosystem and has contributed a novel provider for Minecraft, a shell provider, and dozens of modules in the public registry.

I was incredibly excited when Scott reached out about writing a book on Terraform because he brings a depth of experience using and contributing from small projects to a large enterprise setting. This book does a great job of providing a gentle introduction for entirely new users of Terraform, but it quickly gets to more complex and realistic patterns. The chapter on deploying a multi-tier application on AWS delivers a strong prescription around modules to provide encapsulation and abstraction along with best practices for file and folder layouts.

Subsequent chapters go further in showcasing Terraform for layers above IaaS, such as serverless platforms and CI/CD pipelines. This gives the reader a sense of the wide applicability of Terraform and how to apply infrastructure as code to higher-level resources. Advanced patterns like zero-downtime deployments with Blue/Green and canary patterns are covered and are invaluable for production infrastructure where changes must be made with live traffic. Beyond just using Terraform, Scott discusses how to collaborate in a team environment using modules, remote state, and Terraform Cloud.

For readers who are so inclined, the book also provides pointers for how to contribute to Terraform by building custom providers. While most users are unlikely to author providers, the book shows how simple the process is and can be a useful reference if you find the need to support a custom internal system or novel resource.

Scott is an expert on Terraform, and this book distills hundreds, if not thousands, of hours of practice into practical advice that is easy to follow. The book is a great guide for new users and readers hoping to learn best practices, and it can serve as a reference guide on more complex patterns. You will find the investment in Terraform in Action fruitful wherever you are on the journey to mastery.

I hope you enjoy the book, and best of luck Terraforming!

— Armon Dadgar, Co-Founder and CTO, HashiCorp


When I started writing this book, Terraform 0.12 was months away from being released. I was one of the lucky people who gained early access to an alpha prerelease candidate, but it wasn’t as useful as you might expect. None of the existing providers were compatible with Terraform 0.12, so I couldn’t use any resources or data sources. What little experimenting I could do was restricted to input variables, output values, and expressions. Some good did come out of this, because eventually I was able to develop a simple templating engine that would later become the foundation for chapter 3.

As much as possible, I tried to be on the cutting edge when writing this book. The problem with always being on the cutting edge is that you never know what’s going to rain on your parade next. On multiple occasions, I had to rewrite entire chapters because a new feature was released that broke something or new design patterns became available that made my code obsolete. It’s exciting to work with emerging technologies, but it can also be frustrating.

Even today, there is some volatility in Terraform, but I feel that it’s finally settling down. Terraform has matured enormously and today is used by hundreds of thousands of engineers across the globe to manage billions of dollars’ worth of infrastructure. Don’t get me wrong—change is still happening. But it’s not as radical or fast as it once was. Even with Terraform 1.0 on the horizon, I don’t think there will be any drastic changes, based on what we already have in Terraform 0.15. But maybe I’ll have to eat my words someday.

I’m glad to have written this book, and I feel it’s more important now than ever before because the popularity of Terraform is exploding and people need a practical guide on using it to solve real-world problems. You can find many introductory guides on how to get started with Terraform, but what if you want to take your skills to the next level? That’s what this book is for. I can say with confidence that this is the most advanced book on Terraform available at the moment. My hope is that this book will inspire you to do great things with Terraform.


Many people have contributed their time and effort to the development and production of this book. Without them, the book would not have been possible. First, I would like to thank my development editor, Katie Sposato Johnson, whose feedback and contributions were instrumental in shaping the book into what you see here. Second, I would like to thank Niek Palm, the technical proofer, who diligently tested every line of code and provided excellent technical feedback. My sincere thanks go out to the numerous other staff at Manning Publications and volunteer reviewers who contributed in their own ways.

For introducing me to Terraform and helping write the book’s outline, I would like to thank my mentor and coworker, Anthony Johnson. At HashiCorp, special thanks go to Armon Dadgar for his official endorsement and for writing the book’s foreword. I would also like to thank Jay Fry, VP of corporate marketing, for promoting the book; and Paul Hinze, VP of engineering, for giving advice on what topics to cover.

I am grateful for the staff at Manning: Brian Sawyer for reaching out to me about writing this book; my project editor, Deirdre Hiam; copyeditor, Tiffany Taylor; proofreader, Jason Everett; and review editor, Ivan Martinović. Thank you to all those at Manning who helped make this happen.

To all the reviewers: Adam Kaczmarek, Alessandro Campeis, Amado Gramajo, Andrea Granata, Brian Norquist, Bruce Bergman, Dan Kacenjar, Emanuele Piccinelli, Enrico Mazzarella, Ernesto Cardenas Cangahuala(ne potpisuje poslednje ime), Geoff Clark, James Frohnhofer, Jürgen Hötzel, Kamesh Ganesan, Lakshmi Narasimhan, Leonardo Taccari, Luke Kupka, Matt Welke, Neil Croll, Paul Balogh, Riccardo Marotti, Sébastien Portebois, Stephen Goodman, Tim Bikalp, and Vamsi Krishna—your suggestions helped make this a better book.

Finally, I would like to thank my lovely fiancée and soon-to-be wife, Beatrice. She supported me while I was writing the book and always made sure I had a hot cup of coffee on hand. I would also like to thank my mom and dad, who put me through school and encouraged me as a writer, and my Grandpa Jerry, who never stopped believing in me.

about this book

Who should read this book

This book is for anyone who wants to learn Terraform. Maybe you are new to infrastructure as code or looking to switch roles. Maybe you already have many years of experience and just want to improve your game. Whatever the case, I am confident that there will be something for you here. It doesn’t matter whether you call yourself a sysadmin, operations, SRE, or DevOps engineer—as long as you want to learn Terraform, you’re in the right place.

I don’t presume that you have prior experience with Terraform. But I do expect that you have some experience in related technologies, especially the cloud. You don’t have to be a solutions architect, but you should know what the cloud is and how to use it. Terraform is an infrastructure as code provisioning tool, and it’s mainly used for provisioning cloud-based infrastructure, so I feel this is a reasonable expectation.

Finally, Terraform is a highly expressive declarative programming language. To extend Terraform, you need to have some programming ability, preferably with go-lang. Again, you don’t have to be a rockstar coder, but the more you know, the better your learning experience will be.

How this book is organized: A roadmap

This book is split into three parts. Part 1 is a fast-paced bootcamp that takes you from zero to intermediate with Terraform. If your goal is to learn and become productive with Terraform as quickly as possible, these chapters are for you:

  • Chapter 1—Introduces Terraform and a “Hello World!” style deployment

  • Chapter 2—Builds a mental model for how Terraform works

  • Chapter 3—Covers the fundamentals of how to write effective Terraform

  • Chapter 4—Demonstrates how to structure larger Terraform projects

Part 2 explores various real-world scenarios and things you need to know as an individual contributor:

  • Chapter 5—Presents alternate ways to structure and organize Terraform code

  • Chapter 6—Discusses how to reuse and share code across teams

  • Chapter 7—Examines how Terraform fits into the larger continuous integration / continuous delivery (CI/CD) ecosystem, as well as Terraform’s limitations

  • Chapter 8—Presents an ambitious multi-cloud scenario that ties together all the previous learnings

Part 3 covers advanced topics on Terraform, such as testing, automation, and security:

  • Chapter 9—Covers how to perform Blue/Green deployments with Terraform and how to combine Terraform with Ansible

  • Chapter 10—Shows how to test and refactor Terraform configuration

  • Chapter 11—Extends Terraform by writing a custom provider plugin

  • Chapter 12—Demonstrates how to run Terraform at scale and how to automate running Terraform

  • Chapter 13—Discusses security threats and how to manage secrets

Chapters 1 through 7 should be read sequentially. After that, you can read the chapters in any order. If you read nothing else, I do recommend reading chapters 10 and 13 because those topics are useful for everyone.

About the code

All of the code for each chapter is available on GitHub: form-in-action/manning-code. Everything was written for Terraform 0.15, which you need to have installed (newer versions could work, too). Some chapters use additional CLI utilities or programming that must be installed separately; these are called out as applicable. Finally, most chapters deploy real cloud infrastructure, so you need credentials for Amazon Web Services (AWS), Google Cloud Platform (GCP), or Azure. Appendices A, B, and C cover this process.

The book contains many examples of source code, both in numbered listings and inline with normal text. In both cases, source code is formatted in a fixed-width font like this to separate it from ordinary text. Sometimes code is also in bold to highlight code that has changed from previous steps in the chapter, such as when a new feature adds to an existing line of code.

In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even this was not enough, and listings include line-continuation markers (). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts.

liveBook discussion forum

Purchase of Terraform in Action includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum, go to You can also learn more about Manning’s forums and the rules of conduct at https://!/discussion.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the author some challenging questions lest his interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

about the author


Scott Winkler is a DevOps engineer and distinguished Terraform expert. He has presented his work at HashiConf and HashiTalks and has been honored as a HashiCorp ambassador and core contributor. Scott is active in the community and has developed multiple modules and providers. In his free time, Scott likes to ballroom dance and ride horses. Scott is available for independent consulting on Terraform.

about the cover illustration

The figure on the cover of Terraform in Action is captioned “Habit d'un Morlakue de Sluin en Croatie,” or dress of a Slunj Morlakue in Croatia. The illustration is taken from a collection of dress costumes from various countries by Jacques Grasset de Saint-Sauveur (1757-1810), titled Costumes de Différents Pays, published in France in 1797. Each illustration is finely drawn and colored by hand. The rich variety of Grasset de Saint-Sauveur’s collection reminds us vividly of how culturally apart the world’s towns and regions were just 200 years ago. Isolated from each other, people spoke different dialects and languages. In the streets or in the countryside, it was easy to identify where they lived and what their trade or station in life was just by their dress.

The way we dress has changed since then and the diversity by region, so rich at the time, has faded away. It is now hard to tell apart the inhabitants of different continents, let alone different towns, regions, or countries. Perhaps we have traded cultural diversity for a more varied personal life—certainly for a more varied and fast-paced technological life.

At a time when it is hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by Grasset de Saint-Sauveur’s pictures.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.