Comments (3)
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 thedb_username
anddb_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.
from terraform-up-and-running-code.
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.
from terraform-up-and-running-code.
Glad to help and happy to hear you're enjoying the book!
from terraform-up-and-running-code.
Related Issues (20)
- EC2 instance not responding on port 8080 HOT 3
- Issue with code update for EC2 HOT 3
- Chapter 3: How to Manage Terraform State
- Ch7: K8s example doesn't start HOT 1
- failed to lock s3 state: 2 errors occurred: HOT 1
- Looking for an full example
- Chapter 3 - Isolation Via Workspaces HOT 1
- Chapter 3 HOT 3
- Chapter 3: webserver-cluster ALB unable to access HOT 1
- Chapter 3: db.t2.micro instance type is deprecated
- Why this web server cluster definition doesn't have backend configuration? HOT 2
- Ed. 2, Ch. 4 - Terraform modules. Remote state location for webserver uses key for mysql HOT 1
- Chapter 2, 3rd Edition, failed to retrieve auto scaling groups HOT 4
- Chapter 7, 3rd edition - IAM role required to create new AWS account failed to allow login acces HOT 1
- Chapter 2 - "Deploy a Single Server" needs updating now that EC2 classic is retired. HOT 3
- Chapter-3 - webserver not showing proper html format. HOT 2
- Chapter 2, 3rd Edition: one-webserver ended up with 2 running instances HOT 2
- aws_launch_configuration: "couldn't find resource" on terraform apply HOT 2
- Chapter 5 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from terraform-up-and-running-code.