Infrastructure as Code
Infrastructure as Code (IaC) is the practice of managing and provisioning cloud infrastructure through machine-readable configuration files rather than manual processes or interactive tools.
What is Infrastructure as Code in cloud hosting?
Infrastructure as Code (IaC) is the practice of defining and managing cloud infrastructure using configuration files instead of manual processes. Rather than clicking through a web interface or running individual commands to create instances (virtual machines), networks, and storage volumes, you write code that describes the desired state of your infrastructure. The IaC tool then reads this code and creates, modifies, or deletes resources to match your specifications.
IaC configuration files are typically written in declarative languages such as YAML, JSON, or domain-specific languages like HCL (HashiCorp Configuration Language). These files can be stored in version control systems like Git, allowing you to track changes, review modifications, and roll back to previous configurations when needed.
Related Terms
- Heat is the native OpenStack orchestration service that uses YAML templates to define and deploy cloud resources, such as creating a stack with three instances, a private network, and a load balancer from a single template file.
- Terraform is a vendor-neutral IaC tool that can provision resources across multiple cloud platforms, such as deploying identical infrastructure to both OpenStack and AWS using the same configuration language.
- OpenStack API provides the programmatic interface that IaC tools use to communicate with cloud services, such as sending HTTP requests to create instances or attach volumes without human interaction.
- CLI (Command Line Interface) tools like the OpenStack CLI can be used in IaC scripts and pipelines, such as automating repetitive tasks through shell scripts that call CLI commands in sequence.
- Instance is a virtual machine resource commonly managed by IaC, such as defining instance specifications (flavor, image, network) in a configuration file and having the IaC tool create or destroy it automatically.
Why Infrastructure as Code Exists
Before IaC, administrators created cloud resources manually through web dashboards or by typing individual commands. This approach caused several problems.
Manual provisioning is slow and error-prone. Creating a complex environment with multiple instances, networks, security groups, and storage volumes requires many steps, and missing one step or entering a wrong value causes failures that are difficult to diagnose.
Manual processes are not repeatable. Two administrators following the same written instructions will often produce slightly different results. Recreating an environment identically after a disaster or in a new region becomes nearly impossible.
Manual changes are not tracked. When someone modifies a firewall rule or resizes an instance through the dashboard, there is no automatic record of who made the change, when, or why. Debugging production issues becomes difficult when you cannot determine what changed.
IaC solves these problems by treating infrastructure like software. Configuration files are versioned, reviewed, tested, and deployed through automated pipelines. The same configuration produces identical results every time it runs.
What Does Infrastructure as Code Actually Do?
- Reads configuration files that describe desired infrastructure resources and their properties
- Compares the desired state in configuration files to the current state of existing resources
- Calculates a plan showing which resources will be created, modified, or deleted
- Executes API calls to the cloud platform to make the actual changes
- Stores state information that tracks which resources are managed and their current configuration
- Detects configuration drift when someone manually changes a managed resource outside of IaC
- Enables rollback by reapplying previous versions of configuration files from version control
When Would I Use Infrastructure as Code?
Use IaC when you need to create the same environment multiple times. Development, staging, and production environments should be identical, and IaC ensures consistency across all of them.
Use IaC when multiple team members manage infrastructure. Configuration files in Git show exactly who changed what and when. Code reviews catch mistakes before they affect production.
Use IaC when disaster recovery is important. If your infrastructure is destroyed, IaC can rebuild it quickly from configuration files. Manual reconstruction takes hours or days and introduces errors.
Use IaC when you need to comply with audit requirements. Regulators often require documentation of all infrastructure changes. IaC provides this automatically through version control history.
Use IaC when you want to test infrastructure changes before applying them. IaC tools can generate plans showing what will change, allowing you to review modifications before execution.
When Would I NOT Use Infrastructure as Code?
Do not use IaC for one-time experiments. If you are testing a new instance type for an hour, creating it manually through the dashboard is faster than writing configuration files you will never use again.
Do not use IaC without understanding your cloud platform first. IaC abstracts away the underlying APIs, but troubleshooting requires understanding what the tool is doing. Learn to create resources manually before automating them.
Do not use IaC for resources that change constantly through external processes. If an autoscaler frequently adjusts instance counts, IaC may detect this as drift and try to revert the changes. Configure IaC to ignore attributes that other systems manage.
Do not use IaC without a state management strategy. IaC tools track resource state, and losing or corrupting state files causes serious problems. Plan how you will store, back up, and share state before adopting IaC.
Real-World Example
Company A runs a web application on OpenStack with three tiers: a load balancer, four application instances, and a database cluster. They maintain separate development, staging, and production environments.
Before IaC, creating a new environment took two days. An administrator manually created each resource through the Horizon Dashboard (web interface), referring to documentation and previous environments. Environments frequently had subtle differences that caused bugs to appear only in production.
Company A adopted Terraform with their OpenStack cloud. They wrote configuration files defining all resources: networks, subnets, security groups, instances, and volumes. Each environment uses the same base configuration with different variable values for sizing.
Now, creating a new environment takes 15 minutes. Running terraform apply creates all resources automatically. When a developer needs an isolated environment for testing, they copy the configuration, change a few variables, and deploy. When finished, terraform destroy removes everything cleanly.
Configuration files are stored in Git. All changes go through pull requests. Team members review infrastructure changes like code changes. The Git history shows exactly when the production database was resized and who approved it.
Frequently Asked Questions
What IaC tools work with OpenStack? Several IaC tools support OpenStack. Heat is the native OpenStack orchestration service included with most deployments. Terraform supports OpenStack through its provider plugin. Ansible can manage OpenStack resources using its cloud modules. Pulumi also offers OpenStack support for teams preferring general-purpose programming languages.
Do I need to learn programming to use IaC? You do not need traditional programming skills. Most IaC tools use declarative configuration languages where you describe what you want, not how to create it. The syntax resembles structured data formats like YAML or JSON. However, understanding basic programming concepts like variables, loops, and conditionals helps when working with complex configurations.
What happens if I change a resource manually after IaC created it? The IaC tool detects this as configuration drift on the next run. Depending on the tool and settings, it may report the difference, revert the manual change, or update its state to match reality. To avoid confusion, establish a policy that all changes go through IaC, not manual intervention.
How do I handle sensitive data like passwords in IaC? Never store secrets directly in configuration files that go into version control. Use environment variables, secret management tools like HashiCorp Vault, or your cloud platform's secret storage. IaC tools support referencing external secrets without embedding them in configuration.
Can IaC delete my production environment by accident? Yes, running a destroy command or applying a configuration with resources removed will delete infrastructure. Protect against accidents by using separate state files for each environment, requiring approval for destructive changes, and enabling deletion protection on critical resources. Review plans carefully before applying them.
Summary
- Infrastructure as Code (IaC) is the practice of managing cloud resources through machine-readable configuration files instead of manual processes
- IaC enables repeatable, consistent deployments across multiple environments by applying the same configuration files
- Configuration files stored in version control provide audit trails showing who changed infrastructure, when, and why
- IaC tools compare desired state to current state and make only the changes necessary to reach the target configuration
- Adopting IaC requires planning for state management, secret handling, and team workflows around configuration changes
