Infrastructure as Code (IaC)
Introduction
Infrastructure as Code (IaC) is a key practice in modern cloud computing and DevOps that allows you to manage and provision computing infrastructure through machine-readable definition files, rather than through physical hardware configuration or interactive configuration tools.
What is IaC?
Infrastructure as Code treats infrastructure the same way developers treat application code:
- Version controlled: Infrastructure definitions are stored in version control systems
- Reproducible: The same infrastructure can be created multiple times with identical results
- Automated: Infrastructure deployment and management can be automated
- Testable: Infrastructure changes can be tested before being applied to production
Why IaC for MLOps?
In MLOps, IaC becomes crucial for several reasons:
- Consistency: Ensure that development, staging, and production environments are more consistent
- Scalability: Easily replicate infrastructure for different models or experiments
- Collaboration: Teams can collaborate on infrastructure changes through code reviews
- Disaster Recovery: Quickly rebuild infrastructure from code definitions
- Cost Control: Track and manage cloud resources more effectively
IaC Tools
Several tools can be used for IaC:
- AWS CloudFormation: Native AWS service for infrastructure provisioning
- AWS CDK: Code-based approach using familiar programming languages
- AWS SAM: Simplified approach specifically for serverless applications
- Pulumi: Modern IaC using real programming languages
- Terraform: Popular multi-cloud IaC tool
- OpenTofu: Community-driven fork of Terraform
OpenTofu: Fun Fact!
A few years ago, HashiCorp decided to change Terraform's license to a more restrictive model, which raised concerns about the project's freedom and sustainability.
In response, the community created an open fork to maintain collaboration and ensure transparency in development.
Initially called OpenTF and later renamed to OpenTofu, the project became maintained by the Linux Foundation, ensuring open governance and compatibility with Terraform.
Thus, it became a free and reliable alternative for users and companies that depend on IaC.
For this class, we'll focus on Terraform as it's widely adopted across the industry and provides excellent multi-cloud support.
Terraform Overview
Terraform is an open-source infrastructure as code software tool created by HashiCorp. It enables users to define and provision infrastructure using a declarative configuration language.
Key benefits of Terraform:
- Multi-cloud: Works with AWS, Azure, GCP, and many other providers
- Declarative: Describe what you want, not how to get there
- State Management: Tracks infrastructure state and manages changes
- Plan and Apply: Preview changes before applying them
- Modular: Create reusable modules for common patterns
Setting Up the Environment
Install Terraform
Install the latest version of Terraform following the instructions for your operating system:
Extra
If needed, access more information directly on the Terraform website.
$ sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
$ wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
$ gpg --no-default-keyring --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg --fingerprint
$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
$ sudo apt update
$ sudo apt-get install terraform
- Download the Terraform binary from https://developer.hashicorp.com/terraform/install
- Extract the ZIP file to a directory (e.g.,
C:\terraform
) - Add the directory to the system PATH Tutorial 1 Tutorial 2
Verify the installation:
Configure AWS Profile
Make sure you have AWS credentials configured. Set the AWS profile for the session:
Verify your AWS configuration:
Question 1
Getting Started with Terraform: S3 Bucket Experiment
Before diving into a more complex project, let's start with a simple Terraform experiment to understand the basics.
Create S3 Bucket Experiment
Let's create a simple S3 bucket using Terraform to understand the workflow.
Basic Terraform Configuration for S3
In Terraform, the main.tf
file is where we define our infrastructure resources.
Question 2
Variables Configuration for S3 Experiment
The variables.tf
file is where we define the input variables for our Terraform configuration.
Question 3
Variable Values for S3 Experiment
We can define the variable values in a separate file called terraform.tfvars
.
Question 4
Outputs Configuration for S3 Experiment
The outputs.tf
file is where we define the output values for our Terraform configuration.
The output values are used to extract information from the resources created by Terraform.
Question 5
Deploy the S3 Bucket
Question 6
Verify S3 Bucket Creation
Question 7
Test S3 Bucket Functionality
Let's test our bucket by uploading and downloading a file:
Question 8
Question 9
Question 10
Question 11
Understanding Terraform State
Now, we are going to explore Terraform state management.
Question 12
Question 13
Question 14
Make Changes and Update
Let's modify our infrastructure to understand how Terraform handles changes.
Question 15
Question 16
Question 17
Question 18
Clean Up S3 Experiment
When you're ready to move to the next experiment, clean up these resources!
Question 19
Question 20
Question 21
Question 22
Now, you can go to the next activity!