Terraform - Getting Started with Modules

by
Jeremy Canfield |
Updated: July 21 2023
| Terraform articles
Let's say you have the following files on your Terraform server. In this example, there are 3 modules.
- root module (the base directory that contains the provider.tf file)
- foo module (the foo directory that contains it's own .tf files)
- bar module (the bar directory that contains it's own .tf files)
├── locals.tf
├── modules.tf
├── outputs.tf
├── provider.tf
├── terraform.tfstate
├── variables.tf
├── foo (directory, child module)
│ ├── data.tf
│ ├── outputs.tf
│ ├── resources.tf
├── bar (directory, child module)
│ ├── data.tf
│ ├── outputs.tf
│ ├── resources.tf
For example, let's say you are using Terraform with the Amazon Web Services (AWS) provider. In this scenario, provider.tf might have something like this.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
access_key = var.access_key
secret_key = var.secret_key
region = var.region
}
And modules.tf could have something like this, to use the .tf files in the foo directory.
module "foo" {
source = "./foo"
}
Did you find this article helpful?
If so, consider buying me a coffee over at