skip to Main Content

I have two folders with a few files in each folder

  • services
    • dns.tf
  • app
    • outputs.tf

In the dns.tf I have the following:

resource "cloudflare_record" "pgsql_master_record" {
  count   = var.pgsql_enabled ? 1 : 0
  zone_id = data.cloudflare_zone.this.id
  name    = "${var.name}.pg.${var.jurisdiction}"
  value   = module.db[0].primary.ip_address.0.ip_address
  type    = "A"
  ttl     = 3600
}

resource "cloudflare_record" "redis_master_record" {
  count   = var.redis_enabled ? 1 : 0
  zone_id = data.cloudflare_zone.this.id
  name    = "${var.name}.redis.${var.jurisdiction}"
  value   = module.redis[0].host
  type    = "A"
  ttl     = 3600
}

And in my app outputs.tf I’d like to add outputs for the above resources

output "psql_master_record" {
  value = cloudflare_record.pgsql_master_record[*].hostname
}

output "redis_master_record" {
  value = cloudflare_record.redis_master_record[*].hostname
}

But I keep getting this error:

A managed resource "cloudflare_record" "redis_master_record" has not been declared in the root module.

2

Answers


  1. As far as I know, you have to run terraform per directory. In the same directory you can have multiple terraform files and use variables from file A in file B. You are currently splitting it in 2 directories, that is only possible with a module approach. And this does not work out-of-the-box.

    This thread should clarify it.

    Login or Signup to reply.
  2. You can’t do it.
    Your dns.tf and outputs.tf should be in the same folder

    Or as example, you can use data block with remote state

    In Terraform, you can output values from a configuration using the output block. These outputs can then be referenced within the same configuration using interpolation syntax, or from another configuration using the terraform_remote_state data source.

    Here’s an example of how you might use the output block to output the value of an EC2 instance’s ID:

    resource "aws_instance" "example" {
      # ...
    }
    
    output "instance_id" {
      value = aws_instance.example.id
    }
    

    You can then reference the output value within the same configuration using "output.instance_id.value".

    To use the output value from another configuration, you’ll first need to create a data source for the remote state using the terraform_remote_state data source. Here’s an example of how you might do that:

    data "terraform_remote_state" "example" {
      backend = "s3"
      config {
        bucket = "my-tf-state-bucket"
        key    = "path/to/state/file"
        region = "us-west-2"
      }
    }
    

    Then, you can reference the output value from the remote configuration using "data.terraform_remote_state.example.output.instance_id.value".

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search