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 - plage 126 - error - This object does not have an attribute named "address". #48

Closed martin-flower closed 4 years ago

martin-flower commented 4 years ago

I am reading the book and working using the code from this repository. I am on page 126 and get the error detailed below. What I think this means is that chapter 4 expects (some) things from previous chapters to be there. If this is the case, is there a (simple) way of implementing all the pre-requisites - maybe manually? I am also confused about using terraform destroy as this sometimes returns an error when attempting to destroy the s3 bucket containing the terraform state. Will chapter 4 still work if the bucket is destroyed?

pwd 04-terraform-module/modules/stage/services/webserver-cluster
terraform init
terraform plan
Error: Unsupported attribute
  on ../../../modules/services/webserver-cluster/main.tf line 25, in data "template_file" "user_data":
  25:     db_address  = data.terraform_remote_state.db.outputs.address
    |----------------
    | data.terraform_remote_state.db.outputs is object with 2 attributes
This object does not have an attribute named "address".

Error: Unsupported attribute
  on ../../../modules/services/webserver-cluster/main.tf line 26, in data "template_file" "user_data":
  26:     db_port     = data.terraform_remote_state.db.outputs.port
    |----------------
    | data.terraform_remote_state.db.outputs is object with 2 attributes
This object does not have an attribute named "port".
martin-flower commented 4 years ago

update - it seems I was referring to the s3 bucket I created in chapter two. If I use the S3 bucket I created in chapter 3, then the errors go away. It seems to me that the exercises assume that the same bucket is used throughout.

martin-flower commented 4 years ago

and I get no S3 errors with terraform destroy/init/plan/apply in the stage environment

martin-flower commented 4 years ago

In the prod environment I get the following message. Is this related to the comment on page 116 the production database doesn't actually exist yet

Error: Unable to find remote state
  on ../../../modules/services/webserver-cluster/main.tf line 149, in data "terraform_remote_state" "db":
 149: data "terraform_remote_state" "db" {
No stored state was found for the given workspace in the given backend.
brikis98 commented 4 years ago

What I think this means is that chapter 4 expects (some) things from previous chapters to be there.

Each chapter builds on the examples from previous chapters.

If this is the case, is there a (simple) way of implementing all the pre-requisites

If you skipped previous chapters, use the code from this very repo as your starting point!

I am also confused about using terraform destroy as this sometimes returns an error when attempting to destroy the s3 bucket containing the terraform state.

From the error you listed, I'm guessing you did the following:

  1. Ran apply in a DB module
  2. Ran apply in a web server module that read data from the DB module's state via terraform_remote_state
  3. Ran destroy in the DB module
  4. Now you get errors in the web server module, because it can no longer read in that state

This is not surprising, as the web server module depends on the DB module, and if you ran destroy in the DB module first, that dependency is now broken!

To fix it, either:

  1. Re-run apply on the DB module or...
  2. (Temporarily) remove the terraform_remote_state references from the web server module and replace them with hard-coded values
brikis98 commented 4 years ago

update - it seems I was referring to the s3 bucket I created in chapter two. If I use the S3 bucket I created in chapter 3, then the errors go away. It seems to me that the exercises assume that the same bucket is used throughout.

Ah, if you deleted the S3 bucket where state was stored, then yes, terraform_remote_state data sources won't be able to read the state from that bucket!

brikis98 commented 4 years ago

In the prod environment I get the following message. Is this related to the comment on page 116 the production database doesn't actually exist yet

Correct! The terraform_remote_state data source is used to read a state file written by another module. In the prod environment, if you never deployed the DB module, then terraform_remote_state won't be able to read it's state! Deploying such a module is left as an exercise for you, the reader. Hint: deploying the prod DB module should be nearly identical to deploying the stage one.

martin-flower commented 4 years ago

Thanks Yevgeniy - it's slowly making sense ...