Preface
A long time ago, in a datacenter far, far away, an ancient group of powerful beings known as sysadmins used to deployinfrastructure manually. Every server, every database, every load balancer, and every bit of network configurationwas created and managed by hand. It was a dark and fearful age: fear of downtime, fear of accidental misconfiguration,fear of slow and fragile deployments, and fear of what would happen if the sysadmins fell to the dark side (i.e., took avacation). The good news is that thanks to the DevOps movement, there is now a better way to do things: Terraform.
Terraform is an open source tool created by HashiCorp that allows you to define yourinfrastructure as code using a simple, declarative language and to deploy and manage that infrastructureacross a variety of public cloud providers (e.g., Amazon Web Services, Microsoft Azure, Google Cloud Platform, DigitalOcean) and privatecloud and virtualization platforms (e.g., OpenStack, VMWare) using a few commands. For example, instead of manuallyclicking around a web page or running dozens of commands, here is all the code it takes to configure a server on AWS:
provider
"aws"
{
region
=
"us-east-2"
}
resource
"aws_instance" "example"
{
ami
=
"ami-0c55b159cbfafe1f0"
instance_type
=
"t2.micro"
}
And to deploy it, you just run the following:
$ terraform init$ terraform apply
Thanks to its simplicity and power, Terraform has emerged as a key player in the DevOps world. It allows youto replace the tedious, fragile, and manual parts of infrastructure management with a solid, automated foundation uponwhich you can build all your other DevOps practices (e.g., automated testing, Continuous Integration, ContinuousDelivery) and tooling (e.g., Docker, Chef, Puppet).
This book is the fastest way to get up and running with Terraform.
Youll go from deploying the most basic Hello, World Terraform example (in fact, you just saw it!) all the wayup to running a full tech stack (server cluster, load balancer, database) capable of supporting a large amount oftraffic and a large team of developersall in the span of just a few chapters. This is a hands-on tutorial thatnot only teaches you DevOps and infrastructure as code (IaC) principles, but also walks you through dozens of code examplesthat you can try at home, so make sure you have your computer handy.
By the time youre done, youll be ready to use Terraform in the real world.
Who Should Read This Book
This book is for anyone responsible for the code after it has been written. That includes sysadmins, operations engineers, release engineers, site reliability engineers, DevOps engineers, infrastructure developers, full-stack developers, engineering managers, and CTOs. No matter what your title is, if youre the one managing infrastructure, deploying code,configuring servers, scaling clusters, backing up data, monitoring apps, and responding to alerts at 3 a.m., thisbook is for you.
Collectively, all of these tasks are usually referred to as operations. In the past, it was common to finddevelopers who knew how to write code, but did not understand operations; likewise, it was common to find sysadmins whounderstood operations, but did not know how to write code. You could get away with that divide in the past, but in themodern world, as cloud computing and the DevOps movement become ubiquitous, just about every developer will need tolearn operational skills and every sysadmin will need to learn coding skills.
This book does not assume that youre already an expert coder or expert sysadmina basic familiarity with programming,the command line, and server-based software (e.g., websites) should suffice. Everything else you need youll be able topick up as you go, so that by the end of the book, you will have a solid grasp of one of the most critical aspects ofmodern development and operations: managing infrastructure as code.
In fact, youll learn not only how to manage infrastructure as code using Terraform, but also how this fits into theoverall DevOps world. Here are some of the questions youll be able to answer by the end of the book:
Why use IaC at all?
What are the differences between configuration management, orchestration, provisioning, and server templating?
When should you use Terraform, Chef, Ansible, Puppet, Salt, CloudFormation, Docker, Packer, or Kubernetes?
How does Terraform work and how do you use it to manage your infrastructure?
How do you create reusable Terraform modules?
How do you write Terraform code thats reliable enough for production usage?
How do you test your Terraform code?
How do you make Terraform a part of your automated deployment process?
What are the best practices for using Terraform as a team?
The only tools you need are a computer (Terraform runs on most operating systems), an internet connection, and thedesire to learn.
Why I Wrote This Book
Terraform is a powerful tool. It works with all popular cloud providers. It uses a clean, simple language and has strongsupport for reuse, testing, and versioning. Its open source and has a friendly, active community. But there is onearea where its lacking: maturity.
Terraform is a relatively new technology. As of May 2019, it has not yet hit a 1.0.0 release yet, and despite Terraformsgrowing popularity, its still difficult to find books, blog posts, or experts to help you master the tool. The officialTerraform documentation does a good job of introducing the basic syntax and features, but it includes littleinformation on idiomatic patterns, best practices, testing, reusability, or team workflows. Its like trying to becomefluent in French by studying only the vocabulary but not any of the grammar or idioms .