Coder Social home page Coder Social logo

Comments (3)

brikis98 avatar brikis98 commented on September 27, 2024

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.

from terraform-up-and-running-code.

0x1uke avatar 0x1uke commented on September 27, 2024

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.

brikis98 avatar brikis98 commented on September 27, 2024

Glad to help and happy to hear you're enjoying the book!

from terraform-up-and-running-code.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.