O'Reilly logo
live online training icon Live Online training

Managing Infrastructure as Code with Terraform

Get started with basic syntax, modules, and best practices

Yevgeniy Brikman

Terraform has emerged as a key tool for managing infrastructure as code across a variety of platforms, including AWS, Google Cloud, and Azure. In just three hours, Yevgeniy Brikman gets you up and running with Terraform, covering everything from basic syntax to managing state, building reusable infrastructure modules, managing immutable infrastructure, and best practices for using Terraform as a team.

What you'll learn-and how you can apply it

By the end of this live, online course, you’ll understand:

  • Why you should manage your infrastructure as code
  • Where Terraform fits in among other infrastructure as code tools (Chef, Puppet, Docker, Packer, etc.)
  • How to integrate Terraform into your continuous integration and delivery practices
  • How to avoid tricky Terraform gotchas
  • How to use Terraform as a team

And you’ll be able to:

  • Write Terraform code
  • Manage Terraform state
  • Create reusable infrastructure modules
  • Use advanced Terraform syntax to do loops, conditionals, and zero-downtime deployments
  • Combine Terraform with Docker and Packer to manage immutable infrastructure
  • Write automated tests for your Terraform code
  • Version your Terraform code and promote changes across environments

This training course is for you because...

You’re a sysadmin, engineer, developer, CTO, or anyone else responsible for code after it has been written who wants to learn how to programmatically define and manage your entire tech stack because you’re tired of managing infrastructure manually and dealing with outages at 3:00am.

Prerequisites

  • A basic familiarity with programming, the command line, and server-based software (e.g., websites)

Required materials and setup:

A machine with Terraform, Git, and the text editor of your choice (as long as it supports Terraform syntax—Sublime, Vim, Emacs, Notepad++, IntelliJ, Visual Studio Code, Atom, and TextMate are all good choices) installed
An AWS account with admin permissions and access keys (free tier is fine)
The course GitHub repository cloned to your machine

Recommended preparation:

Terraform: Up and Running (book)

A Comprehensive Guide to Terraform (blog post series)

Infrastructure as Code: Running Microservices on AWS Using Docker, Terraform, and ECS (video and slideshow)

About your instructor

  • 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.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Introduction to infrastructure as code (15 minutes)
- Lecture: Why infrastructure as code is important; infrastructure-as-code tools (Chef, Puppet, Ansible, Docker, Packer, etc.); where Terraform fits into this ecosystem

Basic Terraform syntax (35 minutes)

  • Demonstration: The Terraform code for deploying a server in AWS; using the plan and apply commands to deploy the server; making changes to the code and using the plan and apply commands to deploy the changes; input and output variables; dependencies and the graph command; using the destroy command to undeploy the server
  • Hands-on exercise: Use sample code to deploy and undeploy the server in your AWS accounts

Break (10 minutes)

Terraform state (10 minutes)

  • Lecture: How Terraform stores state; why storing state in version control is not a good idea; locking
  • Demonstration: Terraform backends; configuring S3 as a backend; the terraform init command

Terraform modules (10 minutes)

  • Lecture: The idea behind modules
  • Demonstration: Creating a reusable module to deploy a server; the role of input and output variables in modules; using the same module to deploy several servers in AWS

Advanced Terraform syntax (35 minutes)

  • Lecture: The trade-offs of declarative languages; interpolation; conditionals and ternary syntax; the lifecycle block and create_before_destroy; how to combine Terraform with tools like Docker and Packer to manage immutable infrastructure
  • Demonstration: Doing loops in Terraform using the count parameter function; using the count parameter to do if statements; zero-downtime deployments using create_before_destroy
  • Hands-on exercise: Use sample code to create modules and try some of the advanced syntax in your AWS account

Break (10 minutes)

Terraform gotchas (10 minutes)

  • Lecture: Common Terraform gotchas, including “don’t make out-of-band changes,” eventual consistency bugs, inline resources, and dynamic data in the count parameter; avoiding downtime

Managing multiple environments (10 minutes)

  • Lecture: The implications of Terraform state on multiple environments; a file and folder structure that helps keep environments isolated; file-naming conventions; how Terragrunt keeps Terraform code DRY

Using Terraform as a team (20 minutes)

  • Lecture: How to create automated tests for Terraform code
  • Demonstration: Managing code, PRs, and deployment; versioning; promoting an immutable version of your infrastructure across multiple environments

Wrap-up and Q&A (15 minutes)