Terraform Architecture Overview - Structure and Workflow
Terraform Architecture Overview - Structure and Workflow
Overview of Terraform:
Terraform is an open-source Infrastructure as Code (IaC) tool by HashiCorp that lets you
define, provision, and manage infrastructure using a declarative configuration language
(HCL). Terraform automates the process of setting up infrastructure in a safe, repeatable,
and consistent manner.
Key Features:
• Declarative Approach: You declare the desired state of your infrastructure, and
Terraform makes it happen.
• Multi-Cloud: Supports multiple cloud providers (AWS, Azure, GCP) and on-premise
infrastructure.
Example: A retail company experiencing seasonal traffic spikes often takes days to
manually provision new resources (EC2 instances, load balancers) for their e-commerce
platform. Using Terraform, they automate this process, scaling resources up during peak
times and down afterward, reducing the risk of downtime and saving operational costs.
Terraform configurations are stored in text files. These files can be managed in version
control systems like Git, allowing teams to collaborate and track changes to their
infrastructure.
• Collaboration: Multiple team members can work on the same infrastructure without
conflicts by branching, reviewing, and merging changes.
Example: An IT services company uses Terraform stored in a Git repository. Every change to
their AWS infrastructure (e.g., adding new security groups or modifying an RDS instance) is
committed to Git. This gives them the ability to review changes through pull requests,
ensuring no untested changes go live in production.
• Read: Query the current state of infrastructure using the Terraform plan and state
commands, which show whether resources are in sync with the configurations.
• Delete: Terraform can destroy resources when they are no longer needed, avoiding
resource sprawl and reducing costs.
Example: A streaming company uses Terraform to manage its cloud infrastructure. During
high-traffic events like sports finals, Terraform creates additional EC2 instances, increases
database size, and updates network configurations. Once the event is over, unused
resources are deleted, optimizing cloud costs.
Inventory/Resource Management
Terraform tracks every resource it provisions using a state file (terraform.tfstate). This file
acts as a source of truth for what resources exist and their configuration in the cloud.
• State Management: By maintaining the state file, Terraform knows the exact state of
the infrastructure and can apply only the necessary changes when updates are made.
• Inventory Management: Teams can use Terraform’s state to track and monitor
resources provisioned across various environments, ensuring efficient resource
allocation.
Example: A financial services company manages hundreds of VMs across AWS and Azure.
They use Terraform’s state file to track their inventory, ensuring that resources are being used
efficiently and reducing cloud waste.
• Resource Tagging: Apply tags to resources for better cost tracking and reporting.
• Spot Instances: Terraform can deploy cheaper spot instances on AWS or
preemptible VMs on GCP when workloads are not time-sensitive.
Example: A data analytics company uses Terraform to automatically scale their cloud
infrastructure during busy periods, like end-of-month reporting. During downtime, terraform
scales down unused instances, and they use spot instances where possible to save costs.
Example: A tech company uses Terraform to deploy a complex application that includes EC2
instances, RDS databases, and security groups. Terraform ensures the RDS database is
created first, followed by the security groups, and only then the EC2 instances, preventing
failures caused by missing dependencies.
• Modules: These are containers for multiple resources that can be reused across
different projects and environments. Modules help enforce consistency and reduce
the risk of configuration drift.
• Roles: Terraform doesn’t have a specific concept of "roles" like some configuration
management tools, but you can use modules to define reusable infrastructure
components that serve specific purposes.
Example: A consulting firm has standardized infrastructure modules for VPCs, EC2
instances, and security groups. These modules are reused across client projects, ensuring
that all environments adhere to best practices and reducing the time spent on setting up
similar infrastructure repeatedly.
• Consistency: Using the same Terraform configurations, you can ensure identical
infrastructure across environments (development, testing, production).
Example: A SaaS provider uses Terraform to manage their AWS production environment and
Azure test environment using a single Terraform configuration file. This allows them to switch
cloud providers without rewriting their infrastructure code.
Advantages of Terraform
• Resource Graph: Terraform creates a resource graph to ensure that resources are
provisioned in the right order.
• Modularity: Use modules to package and reuse code across multiple environments.
Disadvantages of Terraform
• State File Management: In larger teams, managing the Terraform state file can
become complex, especially when dealing with remote environments.
Example: An enterprise with multiple teams using Terraform struggles to keep the state files
synchronized, leading them to implement remote state backends (e.g., using AWS S3 with
DynamoDB locking) to avoid conflicts.
Example: A telecom company uses Terraform to ensure that any infrastructure changes are
applied automatically as part of their CI/CD pipeline, ensuring consistency across test and
production environments.
To prevent certain files from being committed to version control, a .gitignore file can be used.
This is critical for sensitive files like Terraform’s state file, which may contain confidential
information.
*.tfstate
*.tfstate.backup
Setting Up Terraform
• Windows: Download the Terraform binary and add it to your system’s PATH.
• MacOS/Linux: Use package managers like brew (MacOS) or apt (Linux) to install
Terraform.
Example:
• Steps:
o Under System Variables, find the Path variable and edit it.
To create an EC2 instance, you define the instance in your Terraform configuration:
• Ingress: Rules that define what inbound traffic is allowed to reach your resources.
• Egress: Rules that define what outbound traffic is allowed from your resources.
Example: You configure a security group to allow HTTP traffic (ingress) to a web server while
permitting the server to access the internet (egress).
Creating a Security Group Using Terraform
Terraform Workflow:
Workflow Steps:
Common Commands:
• terraform plan: Creates an execution plan showing the changes that will be made to
the infrastructure.
• terraform apply: Applies the changes required to reach the desired state of the
configuration.