Terraform Cloud Project Bootcamp with Andrew Brown — Create AWS Terrahouse Module
This article is part of my Terraform journey with Terraform Bootcamp by Andrew Brown and Andrew Bayko with Chris Williams and Shala.
My wild career adventure into Cloud Computing continues with Andrews, and I highly, highly recommend you to come join us if you are interested. Check out Andrews’ free youtube learning contents. You can also buy some paid courses here to support their good cause.
Our goal in this module
Github Issue #26
- ✅ Set up directory structure for our module.
- ✅ Port our S3 bucket into the module.
Architectural update
We will divide the infrastructure into two modules.
- All the thing storage to storage module: S3 bucket, bucket policy, State website hosting, AWS caller identity current,
- All the thing Content Deliver Network to delivery module.
Refactor to modularise
We move the code in the PROJECT_ROOT
down to /modules/terrahouse_aws
. Take notes of the following:
- Modules make your terraform code more organised, portable and reusable.
main.tf
,outputs.tf
, andvariables.tf
at root are refactored.- The module
terrahouse_aws
is two levels deeper. - You can compare before & after the refactoring.
/PROJECT_ROOT
├── /modules # Refactor code from the files in PROJECT_ROOT
| /terrahouse_aws # to the corresponding files in this folder.
| ├── main.tf
| ├── outputs.tf
| ├── variables.tf
| └── README.md
├── variables.tf
├── providers.tf
├── outputs.tf
├── terraform.tfvars # the variables are shared.
├── main.tf
└── README.md
terrahouse_aws
is at a different level. How are we going to reference the variables from terrahouse_aws
module and use them at root level?
- 👉 Add the following code block to
main.tf
at root, and specify the correct path to the module.
# main.tf at root
+ module "terrahouse_aws" {
+ source = "./modules/terrahouse_aws"
+ user_uuid = var.user_uuid
+ bucket_name = var.bucket_name
+ }
Troubleshooting
In case you run into the following error, add the following to the variables.tf
at root.
# variables.tf (at root)
variable "user_uuid" {
type = string
}
variable "bucket_name" {
type = string
}
If the error still persists after updating the variables.tf
at root, the hack is to inject the values (kind of) manually. Create a terraform.tfvars
file and assign values to the variables.
user_uuid = "your_user_uuid_from_exampro_account"
bucket_name = "your_bucket_name"
Questions
After following through, you should be able to answer the following from your muscle memory.
- Know how to access outputs from nested modules.
- How do you get stuff INTO the module? and OUT OF the module?
- What is the purpose of modularisation?