ebook img

Terraform: Up & Running: Writing Infrastructure as Code PDF

365 Pages·2019·11.102 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Terraform: Up & Running: Writing Infrastructure as Code

S e E c dio tin d o n Terraform Up & Running Writing Infrastructure as Code Yevgeniy Brikman SECOND EDITION Terraform: Up & Running Writing Infrastructure as Code Yevgeniy Brikman BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo Terraform: Up & Running by Yevgeniy Brikman Copyright © 2019 Yevgeniy Brikman. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Acquisitions Editor: John Devins Indexer: Ellen Troutman-Zaig Developmental Editors: Virginia Wilson Interior Designer: David Futato Production Editor: Nan Barber Cover Designer: Karen Montgomery Copyeditor: Kim Cofer Illustrator: Rebecca Demarest Proofreader: Octal Publishing, LLC September 2019: Second Edition Revision History for the Second Edition 2019-08-30: First Release 2019-10-04: Second Release 2019-11-15: Third Release 2020-07-10: Fourth Release 2021-01-22: Fifth Release See http://oreilly.com/catalog/errata.csp?isbn=9781492046905 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Terraform: Up & Running, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the authors, and do not represent the publisher’s views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-492-04690-5 [LSCH] To Mom, Dad, Lyalya, and Molly 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 v 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 147 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 163 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 vi | Table of Contents 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 213 Conclusion 220 7. How to Test Terraform Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Manual Tests 224 Manual Testing Basics 226 Cleaning Up After Tests 229 Automated Tests 231 Unit Tests 232 Integration Tests 261 End-to-End Tests 275 Other Testing Approaches 281 Conclusion 283 8. How to Use Terraform as a Team. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Adopting IaC in Your Team 286 Convince Your Boss 286 Work Incrementally 289 Give Your Team the Time to Learn 290 A Workflow for Deploying Application Code 292 Use Version Control 292 Run the Code Locally 293 Make Code Changes 293 Submit Changes for Review 294 Run Automated Tests 295 Merge and Release 296 Deploy 297 A Workflow for Deploying Infrastructure Code 301 Use Version Control 302 Run the Code Locally 306 Table of Contents | vii Make Code Changes 307 Submit Changes for Review 308 Run Automated Tests 310 Merge and Release 312 Deploy 312 Putting It All Together 322 Conclusion 324 A. Recommended Reading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 viii | Table of Contents

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.