Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use resource/datasource attribute from a 'generate' block as an input #3519

Open
RyanW8 opened this issue Oct 25, 2024 · 3 comments
Open

Use resource/datasource attribute from a 'generate' block as an input #3519

RyanW8 opened this issue Oct 25, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@RyanW8
Copy link

RyanW8 commented Oct 25, 2024

Hi,

So lets say our terragrunt.hcl looks like:

terraform {
  source = "${get_parent_terragrunt_dir()}//some_module"
}

inputs = {
  some_value = "${datasource.some_datasource.this}"
  another_value = "${resource.some_resource.this}"
}


generate "extra_resources" {
  path = "app_extras.tf"
  if_exists = "overwrite_terragrunt"
  contents = <<EOF
datasource "some_datasource" "this" {}
resource "some_resource" "this" {}
EOF
}

I appreciate we could put this datasource in the module but we don't want to do that. We have a generic 'application' terraform module that engineering based applications use to deploy cloud resources for their app and in some scenarios they want to provision an extra datasource or an extra resource which wouldn't fit well within the standard application module. So we have a requirement to use the generate block ontop a module and be able to reference that as a Terragrunt input.

How can we achieve this?

@RyanW8 RyanW8 added the bug Something isn't working label Oct 25, 2024
@denis256
Copy link
Member

Hello,
It is possible to create a separate dependency that will generate the datasource and produce the required outputs. These outputs can then be used in other places where they are needed.

@denis256 denis256 self-assigned this Oct 25, 2024
@RyanW8
Copy link
Author

RyanW8 commented Oct 25, 2024

Yeah I get that pattern but it's not clean IMO and scatters an application configuration across multiple directories. It'd be nice to be able to define it all in a single Terragrunt stack

@yhakbar
Copy link
Collaborator

yhakbar commented Nov 22, 2024

Hey @RyanW8 ,

Fundamentally, Terragrunt does not load OpenTofu/Terraform providers when it runs. That's by design, as it deals with different problems than OpenTofu/Terraform. That's also required for use of data sources, as far as I know.

From a first principles perspective why do you want to use that data source? If your module always needs access to that data, why not include it in the module, or make it something that you can enable with a flag that's off by default, and on when you use a count?

If you want the module to stay simple and agnostic to where inputs are coming from, why not use something like run_cmd to fetch the value for you? My guess is that it will actually load the relevant data faster than an equivalent call to a provider data source, as it won't have to spin up a provider binary to get that data (this might depend on a lot of factors, though).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants