O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting Started with Terraform - Second Edition

Book Description

Build, Manage and Improve your infrastructure effortlessly.

About This Book

  • An up-to-date and comprehensive resource on Terraform that lets you quickly and efficiently launch your infrastructure
  • Learn how to implement your infrastructure as code and make secure, effective changes to your infrastructure
  • Learn to build multi-cloud fault-tolerant systems and simplify the management and orchestration of even the largest scale and most complex cloud infrastructures

Who This Book Is For

This book is for developers and operators who already have some exposure to working with infrastructure but want to improve their workflow and introduce infrastructure as a code practice. Knowledge of essential Amazon Web Services components (EC2, VPC, IAM) would help contextualize the examples provided. Basic understanding of Jenkins and Shell scripts will be helpful for the chapters on the production usage of Terraform.

What You Will Learn

  • Understand what Infrastructure as Code (IaC) means and why it matters
  • Install, configure, and deploy Terraform
  • Take full control of your infrastructure in the form of code
  • Manage complete infrastructure, starting with a single server and scaling beyond any limits
  • Discover a great set of production-ready practices to manage infrastructure
  • Set up CI/CD pipelines to test and deliver Terraform stacks
  • Construct templates to simplify more complex provisioning tasks

In Detail

Terraform is a tool used to efficiently build, configure, and improve the production infrastructure. It can manage the existing infrastructure as well as create custom in-house solutions.

This book shows you when and how to implement infrastructure as a code practices with Terraform. It covers everything necessary to set up the complete management of infrastructure with Terraform, starting with the basics of using providers and resources. It is a comprehensive guide that begins with very small infrastructure templates and takes you all the way to managing complex systems, all using concrete examples that evolve over the course of the book. The book ends with the complete workflow of managing a production infrastructure as code—this is achieved with the help of version control and continuous integration. The readers will also learn how to combine multiple providers in a single template and manage different code bases with many complex modules. It focuses on how to set up continuous integration for the infrastructure code.

The readers will be able to use Terraform to build, change, and combine infrastructure safely and efficiently.

Style and approach

This book will help and guide you to implement Terraform in your infrastructure. The readers will start by working on very small infrastructure templates and then slowly move on to manage complex systems, all by using concrete examples that will evolve during the course of the book.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Infrastructure Automation
    1. What is Infrastructure as Code and why is it needed?
    2. Declarative versus procedural tools for Infrastructure as Code
    3. Infrastructure as Code in the Cloud
    4. Requirements for infrastructure provisioner
      1. Supports a wide variety of services
      2. Idempotency
      3. Dependency resolution
      4. Robust integration with existing tools
      5. Platform agnosticism
      6. Smart update management
      7. Ease of extension
    5. Which tools exist for infrastructure provisioning?
      1. Scripting
      2. Configuration management
      3. CloudFormation/Heat
      4. Terraform
    6. A short overview of Terraform
    7. Journey ahead and how to read this book
    8. Summary
  3. Deploying First Server
    1. History of Terraform
    2. Preparing work environment
    3. The many Terraform providers
    4. Short introduction to AWS
    5. Using Elastic Compute Cloud
      1. Creating an instance through the Management Console
      2. Creating an instance with AWS CLI
    6. Configuring AWS provider
      1. Static credentials
      2. Environment variables
      3. Credentials file
    7. Creating an EC2 instance with Terraform
    8. Working with state
    9. Handling resource updates
    10. Destroying everything we've built
    11. Summary
  4. Resource Dependencies and Modules
    1. Creating an AWS Virtual Private Cloud
    2. Understanding dependency graph
    3. Playing with Terraform graphs
    4. Controlling dependencies with depends_on and ignore_changes
    5. Making sense of our template
    6. Removing duplication with modules
    7. Configuring modules
    8. Retrieving module data with outputs
    9. Using root module outputs
    10. Summary
  5. Storing and Supplying Configuration
    1. Understanding variables
      1. Using map variables
      2. Using list variables
      3. Supplying variables inline
      4. Using Terraform environment variables
      5. Using variable files
    2. Configuring data sources
      1. Providing configuration with template_file
      2. Providing data from anywhere with external_data
    3. Exploring Terraform configuration resources
    4. Taking a quick look at Consul
    5. Summary
  6. Connecting with Other Tools
    1. Returning data with outputs
    2. Testing servers with Inspec
    3. Provisioners
      1. Provisioning with local-exec and Ansible
      2. Provisioning with Chef
      3. Provisioning with remote-exec and Puppet
      4. Uploading files with a file provisioner
    4. Reprovisioning machines with null_resource
    5. Using third-party plugins
    6. Summary
  7. Scaling and Updating Infrastructure
    1. Counting servers
    2. Bringing in high availability
    3. Load balancing and simulating conditionals
    4. Immutable infrastructure
    5. Baking images with Packer
    6. Rolling out AMI upgrades with Terraform
    7. Performing blue-green deployments
    8. Refreshing infrastructure
    9. Importing resources
    10. Summary
  8. Collaborative Infrastructure
    1. Version control with Git 101
    2. Moving templates to Git
    3. Protecting secrets in a Git repository
    4. Storing state files remotely
    5. Connecting remote states together
    6. Storing modules remotely
    7. Locking state files with Terragrunt
    8. Moving infrastructure updates to the CI pipeline
    9. Integration testing of Terraform modules
    10. Summary
  9. Future of Terraform
    1. Infrastructure as code and Terraform replacements
    2. Learning AWS and compiling Terraform
    3. Learning Consul
    4. Provisioning and configuration management
    5. Immutable infrastructure
    6. Collaboration and CI/CD
    7. The many tools around Terraform
    8. The rapid development of Terraform
    9. Closing thoughts on the future of Terraform
    10. Summary