Terraform Series - Introduction to Terraform
This article was last updated on: July 24, 2024 am
Article Series
Preface
I’ve recently been using Terraform to provision OCI’s Always Free Tier and found it to be very useful. Here is a summary of the basics of Terraform.
What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool that allows you to securely and efficiently build, change, and version control cloud and local resources.
HashiCorp Terraform is an infrastructure-as-code tool that lets you define cloud and local resources in human-readable configuration files that you can version control, reuse and share. You can then configure and manage all of your infrastructure throughout its lifecycle using consistent workflows. terraform can manage low-level components such as compute, storage and network resources, as well as high-level components such as DNS entries and SaaS capabilities.
Key Features of Terraform
-
Infrastructure as Code (IaC): Infrastructure is described using high-level configuration syntax. This allows for version control of the data center blueprint and treats it like any other code. In addition, the infrastructure can be shared and reused.
-
Execution Plan: Terraform has a “Plan” step that generates an execution plan. The execution plan shows the actions that Terraform will perform when you invoke apply. This allows you to avoid any surprises when Terraform operates your infrastructure.
-
Resource Map: Terraform builds a map of all resources and parallelizes the creation and modification of any non-dependent resources. Because of this, Terraform builds infrastructure as efficiently as possible, and operators can gain insight into the dependencies in their infrastructure.
-
Change Automation: Complex change sets can be applied to your infrastructure with minimal human interaction. With the execution plans and resource maps mentioned earlier, you know exactly what Terraform will change and in what order, avoiding many possible human errors.
*** Translated with www.DeepL.com/Translator (free version) ***
Features of Terraform
Here is a brief list, due to the author’s level, there may be missing, please help to add:
- IaC.
- WorkSpace: Workspace allows the same configuration code to have more than one State file. This means that when deploying to multiple environments, there is no need to copy and paste configuration code into different folders. Each workspace can use its own variable definition file to parameterize the environment.
- Variable: User-supplied values that can parameterize Terraform modules without modifying the source code.
- Run - Plan & Apply
- Resource Map
- Provider: Providers are the logical abstraction of upstream APIs. They are responsible for understanding API interactions and exposing resources. For example: AWS/Azure/GCP/Kubernetes/Aliyun/OCI Providers
- Modules: Modules are separate packages of Terraform configurations that allow related resources to be combined together to create reusable components. To use modules, you don’t need to know how they work, just how to set up the inputs and outputs. For improving software abstraction and code reuse, modules are useful tools. Similar to building blocks or Python’s library.
- Registry: Discover Terraform providers that provide support for all Terraform resource types, or find modules for rapid deployment of common infrastructure configurations. Similar to Docker Registry or Python PypI.
- Declarative Programming: Expresses computational logic (what to do), but does not describe the flow of control (how to do it). Its focus is on the result, not the process.
- Terraform is cloud agnostic, making it as easy to deploy infrastructure to AWS as it is to GCP, Azure or even a private cloud. It is not tied to a specific cloud provider, such as AWS CloudFormation, and does not require learning a completely new technology every time you change cloud providers.
- Expressive and highly extensible: By using conditional statements, for expressions, directives, template files, dynamic blocks, variables, and many built-in functions, we can easily write code to achieve our goals.Terraform offers the following features:
- 115+ functions
- Support for conditional statements
- Support for for loops
- Support for multiple types: strings, numbers, lists, maps, booleans, objects, complex types
- Plug-in support
- Modularity
- Waiting conditions
- Template
- Dynamic blocks
- Collaboration: Collaborate with friends and colleagues using Terraform’s Backend, registry, workspaces and code repositories.
- Lifecycle Management: Create, Read, Update, Delete (or Destory) and customizable lifecycle
create_before_destory
prevent_destory
ignore_changes
flags. - Testing: Terraform code can be tested by
go test
with fixture. - HCL: Human-friendly structured configuration language for command line tools. Although intended for general use, it is primarily targeted at devops tools, servers, etc. HCL is a syntax and API specifically designed for building structured configuration formats. attempts to strike a compromise between generic serialization formats such as JSON and configuration formats built around full programming languages such as Ruby.
- Security and Key Management: Security and key management is achieved through seamless integration with HashiCorp (Terraform’s parent company) Vault.
*** Translated with www.DeepL.com/Translator (free version) ***
Comparison of Terraform and Competitors
Based on the above key features and functions of Terraform, we make a preliminary comparison between Terraform and its competitor:
📝 Disclaimer
The following is only the author’s personal opinion and is not necessarily accurate due to limited eyesight and knowledge. For reference only.
- Compared to Ansible/Chef/Puppet/SaltStack etc. Configuration management tools. Terraform is not a configuration management tool, its primary role is to provision resources, and Terraform focuses on higher-level abstractions of data centers and related services. Also, Ansible/Chef/Puppet/SaltStack etc. are primarily imperative, Terraform is declarative. But they all have rich extensions and ecologies.
- Compared to AWS CloudFormation/GCP Deployment Manager/Azure Resource Manager. Terraform is better in that it is cloud agnostic and supports combinations and permutations of multiple providers and services. Terraform also separates the planning phase from the execution phase by using the concept of execution plans to ensure that it is fully compliant with expectations.
- Compared to Pulumi. Pulumi and Terraform are most similar, the only difference is that Pulumi is not declarative. Each has its own strengths and weaknesses.
- Compared to CrossPlane, Terraform is weaker than CrossPlane in terms of abstraction on the user/developer side, and also in terms of ensuring that the configuration doesn’t drift. But Terraform’s ecosystem (vendors and modules) is much stronger than CrossPlane’s.
Summary
This article discussed what Terraform is, its advantages, features, usage scenarios, and what are the advantages and disadvantages over other IaC tools.
This article is basically conceptual, not yet in the real world, so stay tuned for further updates.
EOF