Terraform Fundamentals -  Part -1

Terraform Fundamentals - Part -1

What is IaC?

Infrastructure as code (IAC) is the process of managing and provisioning computer data centres through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools. With IAC, infrastructure is treated as code, enabling users to automate the deployment, scaling, and management of their infrastructure.

What is Terraform?

  • Terraform is an open-source Infrastructure as Code (IAC) tool developed by HashiCorp.

  • It allows users to define, provision, and manage infrastructure resources as code, using a high-level, declarative language called HashiCorp Configuration Language (HCL).

  • Terraform is an IT infrastructure automation tool for building, changing, and versioning infrastructure safely and efficiently.

  • Terraform can manage existing as well as custom in-house solutions.

  • It turns your infrastructure as code IAC i.e your computing environment has some of the same attributes as your application:

    • Your infrastructure is versionable.

    • Your infrastructure is reusable.

    • Your infrastructure is testable.

What are the advantages of Terraform?

  • Declarative Language: Terraform's HashiCorp Configuration Language (HCL) provides a high-level, human-readable language for describing infrastructure as code.

  • Provider-agnostic: Terraform supports a wide range of infrastructure providers, allowing users to manage their infrastructure across multiple platforms.

  • Multi-cloud deployments: Terraform enables users to manage multiple cloud providers from a single configuration file, simplifying infrastructure management.

  • State management: Terraform tracks the state of infrastructure, making it easier to manage changes and rollbacks.

  • Version control: Terraform configurations can be version controlled, enabling collaboration, auditing, and rollback capabilities.

  • Modularity: Terraform configurations can be broken down into reusable modules, promoting the organization and reducing code duplication.

  • Large community: Terraform has a large and active community of users, providing support, resources, and third-party plugins and modules.

  • Efficient infrastructure management: Terraform provides a streamlined, automated approach to infrastructure management, reducing manual error and increasing efficiency.

  • Terraform gives Immutable Infrastructure.

Features of Terraform?

  • Infrastructure as Code

    - Infrastructure is described using a high-level configuration syntax.

    - Provides single unified syntax.

  • Execution Plans

    - Terraform has a "planning" step where it generates an execution plan.

    - The execution plan shows what Terraform will do before making the actual changes.

  • Resource Graph

    - Terraform builds a graph of all your resources and parallelizes the creation and modification of any non-dependent resources.

    - Terraform builds infrastructure as efficiently as possible.

  • Change Automation

    - Complex change sets can be applied to your infrastructure with minimal human interaction.

    - With the previously mentioned execution plan and resource graph, you know exactly what Terraform will change and in what order, avoiding many possible human errors.

How Terraform Works?

  • Configuration: Users write Terraform configurations using HashiCorp Configuration Language (HCL) to describe the desired state of their infrastructure.

  • Planning: Terraform takes the configuration and compares it to the current state of the infrastructure, determining the necessary changes to reach the desired state.

  • Execution: Terraform executes the necessary changes, creating or updating resources in the infrastructure.

  • State Management: Terraform maintains a record of the state of the infrastructure, known as the Terraform state file, which is used to track changes and manage dependencies between resources.

  • Repeat: As infrastructure changes, users update the Terraform configuration, and Terraform performs a new planning and execution cycle to bring the infrastructure back to the desired state.

What is HCL?

HashiCorp Configuration Language (HCL) is a declarative language developed by HashiCorp for use with their infrastructure as code (IAC) tool Terraform. HCL is designed to be human-readable and easy to use, allowing users to describe the desired state of their infrastructure in a high-level, simple syntax. With HCL, Terraform users can define the resources they need, their properties, and the relationships between them, in a single file. The HCL file is then processed by Terraform, which creates or updates the infrastructure to match the defined state. HCL is designed to be flexible, supporting a wide range of resources and providers, and enabling users to manage their infrastructure across multiple platforms. By using HCL with Terraform, users can define their infrastructure as code, making it easier to manage, version, and test their infrastructure, and reducing the potential for manual errors.

  • Terraform can also read JSON configurations that are named with the .tf.json

  • HCL syntax is composed of blocks that define a configuration in Terraform.

  • Blocks are comprised of key = value pairs.

  • Single-line comments start with # & Multi-line are commented with /* and */.

  • Lists values are made with square brackets ([]), for example ["foo", "bar", "baz"].

  • Maps can be made with braces ({}) and colons (:), for example { "foo": "bar", "bar": "baz" }.

Syntax of HCL :

block 1 {
    type = "string"
}

block 2 {
    key = value
}

"In conclusion, this first part of the series has provided a comprehensive overview of the topic at hand. We have covered the fundamentals, key concepts, and some important tips to get started. Stay tuned for the second part where we will delve deeper into the subject and explore more advanced techniques. Thank you for reading and we hope you found this information useful. Let us know your thoughts and feedback in the comments section below. Until next time!".