Infrastructure as Code with Terraform
Infrastructure as Code with Terraform is an advanced-level course of 5 lessons (about 1 h 30 min), with 16 exercises and 15 quiz questions. Prerequisites: Deploying an Application to the Cloud. The course needs a full project environment, planned for a later phase of the platform.
- Cloud and DevOps
- Advanced
- 5 lessons
- 1 h 30 min
- 16 exercises
- Full project environment (phase 2)
The course in brief
This course turns hand-built infrastructure into code that is versioned, reviewed and applied automatically. You will learn the init, plan, apply cycle, state management in a remote backend, variables, loops and modules, environment separation, and then how to integrate Terraform into CI with plan review, drift detection and adoption of existing resources. The exercises use local providers so you get verifiable results without a cloud account.
What you will be able to do
- Write a Terraform configuration and read a plan before applying it
- Manage state in an encrypted, locked remote backend
- Parameterize a configuration with variables, outputs, locals, count and for_each
- Design and reuse versioned modules
- Isolate staging and production with folders, workspaces, tfvars files and separate accounts
- Automate fmt, validate, plan and apply in CI, detect drift and adopt existing resources with import and moved
Syllabus: 5 lessons
- Describe infrastructure as declarative HCL files
- Chain the init, plan, apply and destroy cycle while reading the plan correctly
- Understand the role of the state file and store it in a locked remote backend
Declarative infrastructure as codeProvidersThe init, plan, apply, destroy cycleState file and backend
- 02
Variables, outputs, locals and loops
- Parameterize a configuration with typed, validated variables
- Expose values with outputs and factor out repetition with locals
- Create multiple resources with count and for_each
- 03
Modules: reuse and compose
- Write a local module with its variables and outputs
- Call a module several times and use its outputs
- Use a registry module with a pinned version
- 04
Managing multiple environments
- Choose between per-environment folders and workspaces
- Isolate the state of each environment
- Parameterize differences between environments with tfvars files
Terraform environmentsWorkspacesPer-environment variable files
- 05
Plan review, CI and evolving existing infrastructure
- Automate fmt, validate and plan in CI on every pull request
- Apply exactly the reviewed plan using a plan file
- Detect drift and evolve existing infrastructure with import and moved
Terraform in continuous integrationPlan fileDrift detectionImport and moved blocks
Final project
Put an application's infrastructure under Terraform
Take the application deployed by hand in the CL2 course (container service, managed database, bucket, secrets) and describe it in Terraform: reusable modules, separate staging and production environments with distinct states, existing resources imported without recreation, and a GitHub Actions pipeline that checks, plans on pull requests and applies after merge. Without a cloud account, build the same structure with the local and random providers.
Deliverables
- An infra/ repository with modules/ and environments/staging, environments/prod, each with its own backend and terraform.tfvars
- At least two documented modules (variables with descriptions, outputs) called from both environments
- The import blocks used to adopt the existing resources, and the plan showing 0 destructions
- A GitHub Actions workflow: fmt -check, validate, plan -out on pull requests, apply of the plan file on main, OIDC authentication
- A README describing the change workflow, the promotion from staging to production and the procedure in case of drift
Assessment criteria
- No state or secret is committed; .terraform.lock.hcl is
- Staging and production have separate states and credentials
- Modules embed no hard-coded provider and their versions are pinned
- The plan of the final configuration against the existing infrastructure shows no unexpected destruction or replacement
- The pipeline blocks the merge if fmt, validate or plan fail, and apply only happens on main
Frequently asked questions
Do I need a cloud account to follow this course?
No. The exercises use the local and random providers, which create local files and values: you can practice the whole Terraform cycle without an account or billing. The course examples show the equivalent resources on AWS and Google Cloud.
What is the difference between Terraform and OpenTofu?
OpenTofu is an open source fork of Terraform, created after Terraform's license change in 2023. The language and commands covered in this course are largely common to both; the command is called tofu instead of terraform.
Does Terraform replace Docker or Kubernetes?
No, they are complementary. Terraform creates the infrastructure (network, databases, buckets, clusters, managed services); Docker packages the application, and Kubernetes or a managed platform runs it.
Can I take over infrastructure that was already created by hand?
Yes. The import block attaches an existing resource to the configuration without recreating it. The last lesson shows the process, as well as how to detect manual changes.
This course, told for your job
Say what you want to be able to do: “Infrastructure as Code with Terraform” takes its place in your path, with examples from your field. The concepts stay those of the course.