Courses / DevOps
1. Infrastructure as Code (IaC)
A practice that involves managing infrastructure through code to ensure consistency and security.
How It Works
Think of IaC like a recipe: just as a recipe outlines the steps and ingredients to make a dish, IaC scripts define the resources and configurations needed for your infrastructure.
You write code to define your infrastructure requirements.
The code is executed by an IaC tool to provision resources automatically.
Changes to the code can be versioned and tracked like software code.
You can replicate environments easily by running the same code.
See It
The code defines a simple infrastructure with two servers and a network configuration, returning it as a JSON string. You could try adding another server to see how it changes the output.
import json
def create_infrastructure():
infrastructure = {
"servers": [
{"name": "web-server", "type": "t2.micro"},
{"name": "db-server", "type": "t2.micro"}
],
"network": {"subnet": "10.0.0.0/24"}
}
return json.dumps(infrastructure)
print(create_infrastructure())Use It
IaC is used to automatically provision cloud resources for web applications.
import json
def provision_web_app_infrastructure():
infrastructure = {
"servers": [
{"name": "app-server", "type": "t2.medium"},
{"name": "db-server", "type": "t2.medium"}
],
"network": {"subnet": "10.0.1.0/24"}
}
return json.dumps(infrastructure)
print(provision_web_app_infrastructure())IaC enables rapid and consistent deployment of cloud resources, reducing the risk of human error.
Break It
What will this print?
import json
def create_infrastructure():
infrastructure = {
"servers": [
{"name": "web-server", "type": "t2.micro"},
{"name": "db-server", "type": "t2.micro"}
],
"network": {"subnet": "10.0.0.0/24"}
}
return infrastructure
print(create_infrastructure())The function returns a dictionary, which is printed directly, resulting in a Python dictionary format instead of JSON. The surprise is that it does not automatically convert to JSON format.
To fix this, you should use `json.dumps()` to convert the dictionary to a JSON string before returning it.
import json
def create_infrastructure():
infrastructure = {
"servers": [
{"name": "web-server", "type": "t2.micro"},
{"name": "db-server", "type": "t2.micro"}
],
"network": {"subnet": "10.0.0.0/24"}
}
return json.dumps(infrastructure)
print(create_infrastructure())Level Up
Using Terraform for IaC: Terraform is a popular tool for implementing Infrastructure as Code, allowing for scalable and reproducible infrastructure management.
Version Control for Infrastructure: Storing IaC scripts in version control systems like Git helps track changes and collaborate effectively.
Prove It
Create a function that defines an infrastructure with at least three different servers and a network configuration.
Python
Infrastructure as Code (IaC)
Infrastructure as Code allows you to manage your infrastructure using code instead of manual processes.
"I want to ensure my servers are set up the same way every time without manual errors."
Infrastructure as Code is a practice that automates the management and provisioning of infrastructure through code, ensuring consistency and reducing human error.
"I want to ensure my servers are set up the same way every time without manual errors."
Infrastructure as Code is a practice that automates the management and provisioning of infrastructure through code, ensuring consistency and reducing human error.