Closed 0x1uke closed 1 year ago
I'm working through Terraform Up and Running. I'm on chapter 4, and I notice in the example code a module is only created for the webserver-cluster service but not for the MySQL datastore. Is there a reason why one can't modularize MySQL to follow the DRY principle?
The book does turn the MySQL code into a module, but it's a bit later, in chapter 7, under the "Working with Multiple AWS Regions" section.
I gave it shot, but I'm stuck with a cycle error in my MySQL module between the db_username and dp_password input variables and trying to pass those values to the child modules as output variables. The idea I'm getting at is one would pass the db_username and db_password as environment variables and the child module will have access to them to create the database using the parent module. I suppose I could create a variables.tf in the prod and stage MySQL directories defining the db_username and db_password variables, but that also seems repetitive. I've included a link below to my code for more detail.
https://github.com/0x1uke/terraform-up-and-running
Thank you!
From a quick look at your code:
To pass the username and password to the module you do need another variables.tf
in staging and prod, with a copy of the db_username
and db_password
variables. So instead of https://github.com/0x1uke/terraform-up-and-running/blob/master/stage/data-stores/mysql/main.tf#L25-L26, you'd have in staging and in prod:
# Define these in variables.tf in staging and in prod
variable "db_username" {
description = "The username for the database"
type = string
sensitive = true
}
variable "db_password" {
description = "The password for the database"
type = string
sensitive = true
}
module "mysql" {
# ... (skipping other params for brevity)...
# Pass the variables through to the module
db_username = var.db_username
db_password = var.db_password
}
Does it feel non-DRY to have to copy/paste those variable definitions? I agree! But that is how Terraform is designed. In some cases, you can reduce this copy/paste by using Terragrunt, as described later in the book.
You shouldn't be using terraform_remote_state
to read the database's state in the database module: https://github.com/0x1uke/terraform-up-and-running/blob/master/modules/data-stores/mysql/main.tf#L27-L35. You should only need that to read the database's state from other modules.
Yevgeniy,
Thank you for your reply. In case I get stuck, I'll take a look at the DB module in Chapter 7, but I really appreciate the sanity check that a variables.tf is required in the DB staging and prod because I wasn't sure if I was missing something. I'll also fix the terraform_remote_state
piece. I'll close this issue out.
Thank you again, and thank you for the awesome book.
Glad to help and happy to hear you're enjoying the book!
I'm working through Terraform Up and Running. I'm on chapter 4, and I notice in the example code a module is only created for the webserver-cluster service but not for the MySQL datastore. Is there a reason why one can't modularize MySQL to follow the DRY principle?
I gave it shot, but I'm stuck with a cycle error in my MySQL module between the db_username and dp_password input variables and trying to pass those values to the child modules as output variables. The idea I'm getting at is one would pass the db_username and db_password as environment variables and the child module will have access to them to create the database using the parent module. I suppose I could create a variables.tf in the prod and stage MySQL directories defining the db_username and db_password variables, but that also seems repetitive. I've included a link below to my code for more detail.
https://github.com/0x1uke/terraform-up-and-running
Thank you!