You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Infrastructure as Code (IaC) is the practice of managing and provisioning computing infrastructure through machine-readable configuration files rather than through manual processes or interactive tools. Instead of clicking through a web console to create servers, databases, and networks, you write code that describes exactly what you want, and an automation tool builds it for you.
Before IaC, teams provisioned infrastructure manually. A system administrator would log into a console, click through menus, fill out forms, and hope they remembered every setting. This approach suffered from several critical problems:
IaC solves all of these problems by treating infrastructure the same way we treat application code: it is versioned, reviewed, tested, and automated.
There are two fundamental approaches to IaC:
| Approach | Description | Example Tools |
|---|---|---|
| Declarative | You describe the desired state and the tool figures out how to achieve it | CloudFormation, Terraform, Pulumi |
| Imperative | You describe the exact steps to execute in order | Bash scripts, AWS CLI scripts, Ansible playbooks |
Declarative example — you say "I want a VPC with two subnets and an EC2 instance" and the tool creates whatever is missing.
Imperative example — you say "First create the VPC, then create subnet A, then create subnet B, then launch an instance."
Most modern IaC tools use the declarative approach because it is simpler, safer, and easier to maintain over time.
An idempotent operation produces the same result whether you run it once or a hundred times. If your template says "create a VPC with CIDR 10.0.0.0/16" and that VPC already exists, the tool should recognise it and make no changes. This property is essential for safe, repeatable deployments.
IaC templates are plain text files that belong in a version control system such as Git. This gives you:
The same template should produce the same infrastructure every time it is applied, regardless of who runs it or when. This makes it trivial to:
| Benefit | Description |
|---|---|
| Speed | Provision entire environments in minutes instead of days |
| Consistency | Every environment built from the same template is identical |
| Safety | Changes can be reviewed, tested, and approved before deployment |
| Documentation | The code itself documents your infrastructure |
| Cost control | Tear down environments you no longer need with a single command |
| Collaboration | Multiple team members can contribute to infrastructure definitions |
AWS provides several tools for practising IaC:
CloudFormation is the native AWS IaC service. You define your infrastructure in JSON or YAML templates and submit them to CloudFormation, which creates and manages the resources for you. CloudFormation is deeply integrated with all AWS services.
The CDK lets you define infrastructure using familiar programming languages such as TypeScript, Python, Java, C#, and Go. Under the hood, the CDK generates CloudFormation templates, so you get the best of both worlds — the expressiveness of a programming language and the reliability of CloudFormation.
SAM is an extension of CloudFormation specifically designed for serverless applications. It provides shorthand syntax for Lambda functions, API Gateways, and DynamoDB tables.
Tools such as Terraform (by HashiCorp), Pulumi, and Ansible also support AWS and are widely used in the industry. Each has its own strengths, but they all share the core IaC principles described above.
Here is a minimal CloudFormation YAML template that creates an S3 bucket:
AWSTemplateFormatVersion: '2010-09-09'
Description: A simple S3 bucket
Resources:
MyBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-example-bucket-12345
This template is:
A typical IaC workflow follows these steps:
This workflow mirrors the software development lifecycle, which is exactly the point — infrastructure changes should be as disciplined and well-tested as application changes.
Infrastructure as Code transforms infrastructure management from a manual, error-prone process into a disciplined, automated practice. By treating infrastructure definitions as code, teams gain speed, consistency, safety, and collaboration. AWS CloudFormation is the native IaC service for AWS, and in the next lesson we will dive deep into how it works.