Module 5: Infrastructure as Code (Terraform)

Provisioning and managing infrastructure using declarative configuration files.

5.1 Introduction to IaC

Infrastructure as Code (IaC) is the managing and provisioning of computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.

5.2 Terraform Basics

Terraform is an open-source infrastructure as code software tool created by HashiCorp. Users define and provide data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL).

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

5.3 State Management

Terraform stores state about your managed infrastructure and configuration. This state is used by Terraform to map real world resources to your configuration, keep track of metadata, and to improve performance for large infrastructures.

5.4 Modules

Modules are containers for multiple resources that are used together. A module consists of a collection of .tf and/or .tf.json files kept together in a directory.

🎯 Practical Exercise

Write a Terraform configuration to provision an AWS EC2 instance and an S3 bucket. Use a remote backend to store the state file.