Infrastructure as Code and Terraform - Day 1
๐ What is Infrastructure as Code
Infrastructure as Code (IaC) is the managing and provisioning of infrastructure through code instead of through manual processes. The goal of Infrastructure as Code is to automate and streamline the process of managing and deploying infrastructure, making it more efficient, scalable, and consistent.
๐ List of some IAC Tools
Infrastructure as Code (IaC) tools are software solutions that facilitate the management and provisioning of computing infrastructure through source code. Below are some popular IAC tools.
Terraform
AWS CloudFormation
Azure Resource Manager
Google CDM
OpenTofu
Pulumi
Crossplane
๐ Why Terraform
Terraform, developed by HashiCorp, is a widely used open-source IaC tool. It uses a declarative syntax written in HashiCorp Configuration Language (HCL).
- Multi-Cloud Support
Terraform supports a wide range of cloud providers, including AWS, Azure, Google Cloud Platform, and many others. This multi-cloud support allows organizations to use a single tool to manage infrastructure across different cloud environments. It also supports on-premises infrastructure.
- Declarative Syntax
Terraform uses a declarative syntax, making it easier to understand and write infrastructure code. Users define the desired state of the infrastructure, and Terraform figures out how to achieve it. This simplicity contributes to faster adoption and reduced learning curve.
- Reusability of code - Terraform Modules
Terraform allows users to organize and share code using modules, making it easy to maintain and share common patterns across projects.
- Community support
Terraform has a large and active community of users, contributors, and module creators.
- Integration with other tools
Terraform integrates well with other DevOps tools and practices. It can be seamlessly integrated into continuous integration/continuous deployment (CI/CD) pipelines, allowing for automated testing and deployment of infrastructure changes.
- State management
Terraform provides a robust state management system that records the state of the infrastructure after each operation.
๐ Some important terms in Terraform
Resource
Resource in Terraform refers to a component or infrastructure object that Terraform manages. Resources could be virtual machines, databases, networks, and more. They are defined in the Terraform configuration and managed by the corresponding provider.
Provider
Provider is a plugin in Terraform that defines and manages resources. Providers are responsible for translating the Terraform configuration into API calls specific to a particular infrastructure or service provider (e.g., AWS, Azure, Google Cloud).State file
Terraform stores state about its managed infrastructure and configurations. The state file (usually
terraform.tfstate
) contains information about the resources, their configurations, and dependencies. It's critical for tracking changes and ensuring consistency. This state file is used by Terraform to map real world resources to the configuration defined in the configuration files.Configuration file
In Terraform, a configuration file is a plain text file that defines the desired state of the infrastructure. This file is written in HashiCorp Configuration Language (HCL) or JSON format. The configuration file contains the declarations of resources, variables, outputs, and other settings necessary to define and manage the infrastructure using Terraform.
๐ Terraform Lifecycle
๐ Terraform commands to be used in day to day life
terraform init -> Downloads required plugin for the provider and save locally to a .terraform directory. Terraform must initialize the provider before it can be used.
terraform plan -> The terraform plan command is used to create an execution plan. It will not modify things in infrastructure. This command is a convenient way to check whether the execution plan for a set of changes matches your expectations without making any changes to real resources or to the state.
terraform apply -> The terraform apply is used to apply the changes required to reach the desired state of the configuration. It will also write the data to the terraform.tfstate file
terraform destroy -> The terraform destroy command is used to destroy the Terraform managed infrastructure.
terraform validate -> Validates the syntax and configuration of the Terraform files in the current directory. It checks for errors in the configuration files.
terraform fmt -> The terraform fmt is used to rewrite Terraform configuration files to a canonical format and style.
terraform workspace -> Manages workspaces, allowing you to create, switch, list, and delete workspaces. Workspaces are used for managing multiple environments (e.g., dev, staging, production) with the same configuration.