Terraform Registry<\/a>.<\/p>\n\n\n\nResources<\/h4>\n\n\n\n Resources are the building blocks of Terraform language. They define specific components of the infrastructure that you want to be provisioned. For example virtual machines, networks, storage buckets, databases, local files, etc. Every provider has its own resource types along with the attributes that can be used to customize respective resource.<\/p>\n\n\n\n
Resources are defined within a resource<\/strong> block in a Terraform configuration. When defining a resource, you specify which provider the resource belongs to so that Terraform is aware which API endpoints to interact with and which operations to perform on the resource.<\/p>\n\n\n\nData Sources<\/h4>\n\n\n\n While Terraform can use resources defined within a single configuration to manage an infrastructure, sometime it may need to use other resources defined in other Terraform configurations. Thus, data sources<\/strong> provide a way to fetch and use information from existing resources or services outside of your Terraform-managed infrastructure. A data resource<\/em> is declared using a\u00a0data<\/strong>\u00a0block in a Terraform configuration.<\/p>\n\n\n\nProvisioners is a feature in Terraform that can be used to execute tasks that Terraform cannot natively handle such as executing scripts or commands on local or remote machines after a resource has been created or updated. Such scripts\/commands maybe be used to run commands to install packages, create files\/directories, configuring some services, running initial commands, copying files, e.t.c on provisioned instances.<\/p>\n\n\n\n
There are three types of provisioners;<\/p>\n\n\n\n
\nlocal-exec:<\/strong> This is used to execute commands on the local Terraform machine where the code is running.<\/li>\n\n\n\nremote-exec:<\/strong> This is used to connect to a provisioned instance (remote machine) via SSH and execute commands. As such, you need to ensure that SSH access to provisioned instances is secure and properly configured.<\/li>\n\n\n\nfile:<\/strong> This is used to copy\/or transfers files from the local machine to a provisioned remote machine.<\/li>\n<\/ul>\n\n\n\nTerraform uses variables to parameterize its configurations. It makes it easy to define dynamic values or user-defined values for customizing your infrastructure deployments based on different environments, deployment scenarios, or user inputs.<\/p>\n\n\n\n
There are different types of Terraform variables.<\/p>\n\n\n\n
\nInput variables:<\/strong> These are variables that are explicitly defined by a user when running Terraform commands, either interactively or via the use of command line arguments.<\/li>\n\n\n\nEnvironment variables<\/strong>: These are variables that are part of the environment in which a process is running. Terraform program automatically searches and read all environment variables with the TF_VAR_<\/kbd> prefix found within the environment it is running.<\/li>\n\n\n\nExpression variables<\/strong>: These are variables that hold values based off a Terraform expression.<\/li>\n<\/ul>\n\n\n\nTerraform Outputs on the other hand provide a way to expose some information about a resource after it has been created. For example, you can declare a variable to expose an IP address of an instance, an instance ID, a username, password, a computed value from a specific process e.t.c.<\/p>\n\n\n\n
Terraform modules define a collections of Terraform configurations (.tf ot .tf.json<\/strong>) residing within a directory and managed as a group. A group may include a main Terraform configuration, variable configuration, outputs configuration…They make it easy to reuse the configurations over and over.<\/p>\n\n\n\nA Terraform modules can either be root<\/strong> module, which is the main Terraform configuration or a child<\/strong> module, which is a module that is called by other main modules for inclusion in their resource configuration.<\/p>\n\n\n\nTerraform modules can reside in the local filesystem or can be published to a private or publish Terraform registry.<\/p>\n\n\n\n
Terraform checks defines the mechanism that Terraform uses to check for correctness, consistency and validity of Terraform resource configurations.<\/p>\n\n\n\n
Terraform stores the map of the current state of your infrastructure in a file called terraform.tfstate<\/strong> located in the same directory as your Terraform configuration files. The state map stores various information about managed infrastructure resources such as instances, instance IDs, virtual machines… thus helping Terraform to understand the existing resources, their attributes, and any dependencies between them. It serves as the source of truth for Terraform, enabling it to determine the necessary actions to achieve the desired state declared in your configuration files.<\/p>\n\n\n\nThe terraform<\/strong> command provides a command line interface for Terraform. The command servers as the primary interface to interact with Terraform on command line. terraform<\/strong> command provides various command line options to initialize or prepare Terraform working directory, validate the Terraform configuration, show changes required by the current configuration, create or update infrastructure or even destroy previously-created infrastructure.<\/p>\n\n\n\nHow does Terraform actually provision infrastructure resources? Here is a basic Terraform architecture with a summary of its operation.<\/p>\n\n\n\n <\/figure>\n\n\n\nThere are two main components of Terraform:<\/p>\n\n\n\n
\nTerraform Core<\/strong>: At the core of Terraform are Terraform configuration and providers\/plugins.\n\nTerraform configurations<\/strong>: These are document files written in Terraform declarative language that defines a desired state of a resource to be provisioned along with their dependencies. Terraform configurations are text files with the .tf<\/strong> file extension or .tf.json<\/strong> for the JSON variants.<\/li>\n\n\n\nState Files<\/strong>: Terraform state files, often named terraform.tfstate<\/strong>, is used to store and track the current state of the infrastructure. This file is refreshed and updated by Terraform with the current state of the infrastructure before any changes are applied. It is recommended to store this file in cloud, versioned, encrypted and secured.<\/li>\n<\/ul>\n<\/li>\n\n\n\nTerraform Providers\/Plugins<\/strong>: As already mentioned, Providers are plugins that interface with various infrastructure platforms. They offer a collection of resources and data sources that Terraform can manage.<\/li>\n<\/ul>\n\n\n\nTypical Terraform Workflow involves a number of steps, that can be controlled using Terraform CLI;<\/p>\n\n\n\n
\nWrite<\/strong> Terraform configuration to define the desired state of your infrastructure and its dependencies.<\/li>\n\n\n\nInitialize Terraform<\/strong> Working Directory. Once you are done writing the configuration, you can initialize it using terraform init<\/strong> command. The initialization command scans the Terraform configuration and downloads the required plugins from the registry. This information is stored on the .terraform<\/strong> subdirectory within the working directory. Another lock file is also created to track the provider dependencies and store checksum hashes for installed plugin binaries (to ensure integrity). The file is named as .terraform.lock.hcl<\/strong> and is stored under the working directory.<\/li>\n\n\n\nPlanning<\/strong>: After preparing your Terraform working directory, you can now execute terraform plan<\/strong> command to view the changes that Terraform will make on the infrastructure.<\/li>\n\n\n\nApplication<\/strong>: To create or update your infrastructure based on your planned actions, execute terraform apply<\/strong> command.<\/li>\n<\/ol>\n\n\n\nAnd of course, you can view more terraform<\/strong> command line options for other actions.<\/p>\n\n\n\nNow that you have a basic understanding of the core concepts and workflow of Terraform, you are ready to get started.<\/p>\n\n\n\n
Check the guide below on how to install Terraform so you can play around with it.<\/p>\n\n\n\n
Install Terraform on Ubuntu 24.04<\/a><\/p>\n\n\n\nConclusion<\/h2>\n\n\n\n That concludes our guide on basic introduction to Terraform and its basic architecture. Happy Terraforming!<\/p>\n\n\n\n
Read more on Terraform Documentation page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"In this tutorial, we will dive into introduction to Terraform so as to understand its basic architecture. With the ever evolving technology, infrastructure management has<\/p>\n","protected":false},"author":10,"featured_media":22150,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,1722],"tags":[7440,7442,7441],"class_list":["post-22143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-automation-2","tag-introduction-to-terraform","tag-terraform-core-concepts","tag-terraform-workflow","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/22143"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=22143"}],"version-history":[{"count":13,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/22143\/revisions"}],"predecessor-version":[{"id":23311,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/22143\/revisions\/23311"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/22150"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=22143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=22143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=22143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}