How To Manage Infrastructure with Terraform

264.2k views

Terraform is a popular open source Infrastructure as Code (IAC) tool that automates provisioning of your infrastructure in the cloud and manages the full lifecycle of all deployed resources, which are defined in source code. Its resource-managing behavior is predictable and reproducible, so you can plan the actions in advance and reuse your code configurations for similar infrastructure.

In this series, you will build out examples of Terraform projects to gain an understanding of the IAC approach and how it’s applied in practice to facilitate creating and deploying reusable and scalable infrastructure architectures.

  • In this article, we define Infrastructure as Code (IaC) and explain the benefits of IaC in development and deployment workflows. We also start to learn about Terraform's role in the IaC workflow and understand "what is 'state'?" in IaC.
  • Terraform is a tool for building and managing infrastructure in an organized way. In this tutorial, you'll install and use Terraform to create an infrastructure on DigitalOcean that consists of two Nginx servers that are load balanced by a DigitalOcean Load Balancer.
  • Structuring Terraform projects appropriately according to their use cases and perceived complexity is essential to ensure their maintainability and extensibility in day-to-day operations. In this tutorial, you'll learn about structuring Terraform projects according to their general purpose and complexity. Then, you'll create a project with a simple structure using the more common features of Terraform: variables, locals, data sources, and provisioners.
  • Hashicorp Configuration Language (HCL), which Terraform uses, like other programming languages provides a few different types of loops. In this article, we'll review the data structures HCL provides, its looping features for resources (the count key, for_each, and for), writing conditionals to handle known and unknown values, as well as explicitly specifying dependency relationships between resources.
  • Terraform outputs are used to extract information about the infrastructure resources from the project state. In this tutorial, you'll learn about Terraform outputs their syntax and parameters by creating a simple infrastructure that deploys Droplets. You'll also learn about parsing the outputs programmatically, by converting them to JSON.
  • Terraform modules encapsulate distinct logical components of your infrastructure by grouping their resources together. In this tutorial, you'll create a Terraform module that will set up multiple Droplets behind a Load Balancer for redundancy. You'll also use the for_each and count looping features of the Hashicorp Configuration Language (HCL) to deploy multiple customized instances of the module at the same time.
  • One of the main benefits of Infrastructure as Code is reusing parts of the defined infrastructure. In this tutorial, we'll consider some of the ways of defining and reusing code in Terraform projects. You'll reference modules from the Terraform Registry, separate development and production environments using modules, learn about templates and how they are used, and how to specify resource dependencies explicitly using the depends_on meta argument.
  • Terraform provides automation to provision your infrastructure in the cloud. To do this, Terraform authenticates with cloud providers to deploy the resources and perform the planned actions. However, the information Terraform needs for authentication is very valuable. In this tutorial, you'll hide sensitive data in outputs during execution and store your state in a secure cloud object storage, which encrypts data at rest. You'll also use tfmask to censor values in the Terraform execution.
  • Ansible and Terraform are not competing solutions, as they resolve different phases of infrastructure and software deployment. In this tutorial, you'll deploy Droplets using Terraform, and then immediately after their creation, you'll bootstrap the Droplets using Ansible.
  • Some advanced features Terraform offers become useful when your project grows in size and complexity. It's possible to alleviate the cost of maintaining complex infrastructure definitions for multiple environments by structuring your code to minimize repetitions and by introducing tool-assisted workflows for easier testing and deployment. In this tutorial, you'll first deploy multiple infrastructure instances using different workspaces.
  • Similarly to developing with other programming languages and frameworks, setting log levels in Terraform to gain insight into its internal workflows with the necessary verbosity is a feature that can help you when troubleshooting. In this tutorial, you'll verify that variables always have sensible values and you'll specify exactly which versions of providers and modules you need to prevent conflicts.
  • In this tutorial, you'll create an organization in Terraform Cloud to which you'll connect your project. You'll then use your orgnanization to set up workspaces and resources. You will store your state in the managed cloud so it is always available. You'll also set up the pg backend with an accompanying managed Postgres database.