Terraform: Up & Running: Writing Infrastructure as Code

Terraform: Up & Running: Writing Infrastructure as Code

by Yevgeniy Brikman
Terraform: Up & Running: Writing Infrastructure as Code

Terraform: Up & Running: Writing Infrastructure as Code

by Yevgeniy Brikman

Paperback(2nd ed.)

    Temporarily Out of Stock Online
    Check Availability at Nearby Stores

Related collections and offers


Terraform has become a key player in the DevOps world for defining, launching, and managing infrastructure as code (IaC) across a variety of cloud and virtualization platforms, including AWS, Google Cloud, Azure, and more. This hands-on third edition, expanded and thoroughly updated for version 1.0 and beyond, shows you the fastest way to get up and running with Terraform.

Gruntwork cofounder Yevgeniy (Jim) Brikman walks you through code examples that demonstrate Terraform's simple, declarative programming language for deploying and managing infrastructure with a few commands. Veteran sysadmins, DevOps engineers, and novice developers will quickly go from Terraform basics to running a full stack that can support a massive amount of traffic and a large team of developers.

  • Compare Terraform with Chef, Puppet, Ansible, CloudFormation, and Pulumi
  • Deploy servers, load balancers, and databases
  • Create reusable infrastructure with Terraform modules
  • Test your Terraform modules with static analysis, unit tests, and integration tests
  • Configure CI/CD pipelines for both your apps and infrastructure code
  • Use advanced Terraform syntax for loops, conditionals, and zero-downtime deployment
  • Get up to speed on Terraform 0.13 to 1.0 and beyond
  • Work with multiple clouds and providers (including Kubernetes!)

Product Details

ISBN-13: 9781492046905
Publisher: O'Reilly Media, Incorporated
Publication date: 10/08/2019
Edition description: 2nd ed.
Pages: 360
Product dimensions: 7.00(w) x 9.10(h) x 0.80(d)

About the Author

Yevgeniy (Jim) Brikman loves programming, writing, speaking, traveling, and lifting heavy things. He is the co-founder of Gruntwork, a company that helps startups get up and running on AWS with DevOps best practices and world-class infrastructure. He's also the author of "Hello, Startup: A Programmer's Guide to Building Products, Technologies, and Teams," a book published by O'Reilly Media that has a 4.9/5.0 rating on Amazon and 4.5/5.0 rating on GoodReads. Previously, he worked as a software engineer at LinkedIn, TripAdvisor, Cisco Systems, and Thomson Financial and got his BS and Masters at Cornell University. For more info, check out ybrikman.com.

Table of Contents

Preface ix

1 Why Terraform 1

The Rise of DevOps 1

What Is Infrastructure as Code? 3

Ad Hoc Scripts 4

Configuration Management Tools 5

Server Templating Tools 7

Orchestration Tools 12

Provisioning Tools 14

The Benefits of Infrastructure as Code 15

How Terraform Works 17

How Terraform Compares to Other IaC Tools 19

Configuration Management Versus Provisioning 20

Mutable Infrastructure Versus Immutable Infrastructure 21

Procedural Language Versus Declarative Language 22

Master Versus Masterless 25

Agent Versus Agentless 26

Large Community Versus Small Community 28

Mature Versus Cutting Edge 30

Using Multiple Tools Together 31

Conclusion 33

2 Getting Started with Terraform 35

Setting Up Your AWS Account 36

Install Terraform 39

Deploy a Single Server 40

Deploy a Single Web Server 48

Deploy a Configurable Web Server 55

Deploying a Cluster of Web Servers 61

Deploying a Load Balancer 64

Cleanup 73

Conclusion 74

3 How to Manage Terraform State 75

What Is Terraform State? 76

Shared Storage for State Files 77

Limitations with Terraform's Backends 85

Isolating State Files 87

Isolation via Workspaces 88

Isolation via File Layout 94

The terraform_remote_state Data Source 98

Conclusion 108

4 How to Create Reusable Infrastructure with Terraform Modules 109

Module Basics 112

Module Inputs 114

Module Locals 118

Module Outputs 120

Module Gotchas 123

File Paths 123

Inline Blocks 124

Module Versioning 126

Conclusion 132

5 Terraform Tips and Tricks: Loops, If-Statements, Deployment, and Gotchas 133

Loops 134

Loops with the count Parameter 134

Loops with for_each Expressions 140

Loops with for Expressions 146

Loops with the for String Directive 150

Conditionals 151

Conditionals with the count Parameter 151

Conditionals with for_each and for Expressions 161

Conditionals with the if String Directive 162

Zero-Downtime Deployment 163

Terraform Gotchas 175

Count and for_each Have Limitations 175

Zero-Downtime Deployment Has Limitations 178

Valid Plans Can Fail 178

Refactoring Can Be Tricky 180

Eventual Consistency Is Consistent…Eventually 183

Conclusion 184

6 Production-Grade Terraform Code 185

Why It Takes So Long to Build Production-Grade Infrastructure 187

The Production-Grade Infrastructure Checklist 189

Production-Grade Infrastructure Modules 191

Small Modules 191

Composable Modules 197

Testable Modules 205

Releasable modules 208

Beyond Terraform Modules 212

Conclusion 219

7 How to Test Terraform Code 221

Manual Tests 222

Manual Testing Basics 224

Cleaning Up After Tests 227

Automated Tests 229

Unit Tests 230

Integration Tests 259

End-to-End Tests 273

Other Testing Approaches 279

Conclusion 281

8 How to Use Terraform as a Team 283

Adopting IaC in Your Team 284

Convince Your Boss 284

Work Incrementally 287

Give Your Team the Time to Learn 288

A Workflow for Deploying Application Code 290

Use Version Control 290

Run the Code Locally 291

Make Code Changes 291

Submit Changes for Review 292

Run Automated Tests 293

Merge and Release 294

Deploy 295

A Workflow for Deploying Infrastructure Code 299

Use Version Control 300

Run the Code Locally 304

Make Code Changes 305

Submit Changes for Review 306

Run Automated Tests 308

Merge and Release 310

Deploy 310

Putting It All Together 320

Conclusion 322

A Recommended Reading 325

Index 329

From the B&N Reads Blog

Customer Reviews