brikis98 / terraform-up-and-running-code

Code samples for the book "Terraform: Up & Running" by Yevgeniy Brikman
http://www.terraformupandrunning.com/
MIT License
2.87k stars 1.92k forks source link

Chapter 4 - database output #44

Closed bkonicek closed 4 years ago

bkonicek commented 4 years ago

I didn't see any reference to this in the chapter, but after following the suggested exercise to move the mysql configuration into a module and deploy for prod (pg 116), the webserver-cluster deployments will fail as the address and port outputs aren't being called by the prod and stage configurations. You need to add an outputs.tf to both prod and stage configurations for mysql so that the webservice-cluster deployments can read the values from the mysql remote state.

brikis98 commented 4 years ago

Not sure I fully understand! The database module has an outputs.tf in chapter 3... If you add a staging DB, you should of course copy that outputs.tf. Is there something in the book that is suggesting otherwise or misleading?

bkonicek commented 4 years ago

Thanks, I think the confusion I'm seeing is mostly based off of the fact that the database module was mentioned as a bonus exercise, and this chapter is mostly focused on the webserver_cluster module. I do see a mention of the environment-specific outputs.tf on page 122, but I don't recall reading anywhere that the module's outputs.tf should be copied into the environment's directory. Maybe for future editions just having a note somewhere around this section to keep in mind that the Terraform State won't include any module outputs unless they're also added to the environment outputs could be helpful?

brikis98 commented 4 years ago

Thx for the feedback!

I do see a mention of the environment-specific outputs.tf on page 122, but I don't recall reading anywhere that the module's outputs.tf should be copied into the environment's directory.

If you want to have outputs available in your state, you must add output variables, which, by convention, live in outputs.tf.

bkonicek commented 4 years ago

Fair enough, I think I was over (or under) thinking it a little bit. I appreciate the discussion!

On Tue, Dec 31, 2019 at 1:06 PM Yevgeniy Brikman notifications@github.com wrote:

Thx for the feedback!

I do see a mention of the environment-specific outputs.tf on page 122, but I don't recall reading anywhere that the module's outputs.tf should be copied into the environment's directory.

If you want to have outputs available in your state, you must add output variables, which, by convention, live in outputs.tf.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/brikis98/terraform-up-and-running-code/issues/44?email_source=notifications&email_token=ABYOBGUBWGHMGY7XWJJ36ZTQ3OC4HA5CNFSM4KABSIKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH4QJ4A#issuecomment-569967856, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABYOBGWEYYRXC7W4Y2HN47DQ3OC4HANCNFSM4KABSIKA .

brikis98 commented 4 years ago

I believe this issue was resolved. Closing.

ValeriyDP commented 3 years ago

For Googlers:

Error: Unsupported attribute
│ 
│   on ../../../modules/services/webserver-cluster/main.tf line 148, in data "template_file" "user_data":
│  148:     db_address = data.terraform_remote_state.db.outputs.address
│     ├────────────────
│     │ data.terraform_remote_state.db.outputs is object with no attributes
│ 
│ This object does not have an attribute named "address".
╵
╷
│ Error: Unsupported attribute
│ 
│   on ../../../modules/services/webserver-cluster/main.tf line 149, in data "template_file" "user_data":
│  149:     db_port = data.terraform_remote_state.db.outputs.port
│     ├────────────────
│     │ data.terraform_remote_state.db.outputs is object with no attributes
│ 
│ This object does not have an attribute named "port".

ch4/modules/services/data-stores/mysql/outputs.tf

output "address" {
  value = aws_db_instance.example.address
  description = "Connect to the database at this endpoint"
}

output "port" {
  value = aws_db_instance.example.port
  description = "The port the database is listening on"
}

ch4/stage/services/data-stores/mysql/outputs.tf

output "address" {
  value = module.data_stores.address
  description = "Connect to the database at this endpoint"
}

output "port" {
  value = module.data_stores.port
  description = "The port the database is listening on"
}