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

Import blocks inside child modules #1914

Open
rdash99 opened this issue Aug 14, 2024 · 7 comments
Open

Import blocks inside child modules #1914

rdash99 opened this issue Aug 14, 2024 · 7 comments
Assignees
Labels
enhancement New feature or request needs-community-input pending-decision This issue has not been accepted for implementation nor rejected. It's still open to discussion.

Comments

@rdash99
Copy link

rdash99 commented Aug 14, 2024

OpenTofu Version

OpenTofu v1.7.3
on linux_amd64
+ provider registry.opentofu.org/hashicorp/github v6.2.1

Use Cases

We are defining a lot of GitHub infrastructure into terraform files and want to be able to organize it into folders but also keep things tidy by having the import blocks live alongside the rest of the resource definitions rather than filling up the top level main file or adding many files in the top level directory.

Attempted Solutions

We have attempted to do this and upon running tofu init get the following:

Initializing the backend...
Initializing modules...
- repos.DevOPs in repos/DevOPs
- repos.DevOPs.workflows-test in repos/DevOPs/workflows-test
- repos.DevOPs.workflows-test.workflows-test in modules/repo/settings
- repos.DevOPs.workflows-test.workflows-test.branch-protection in modules/repo/branch-protection
╷
│ Error: Invalid import configuration
│
│   on repos/DevOPs/workflows-test/main.tf line 11:
│   11: import {
│
│ An import block was detected in "module.repos.module.DevOPs.module.workflows-test".
│ Import blocks are only allowed in the root module.

### Proposal

_No response_

### References

_No response_
@rdash99 rdash99 added enhancement New feature or request pending-decision This issue has not been accepted for implementation nor rejected. It's still open to discussion. labels Aug 14, 2024
@ghost
Copy link

ghost commented Aug 14, 2024

Hello and thank you for this issue! The core team regularly reviews new issues and discusses them, but this can take a little time. Please bear with us while we get to your issue. If you're interested, the contribution guide has a section about the decision-making process.

@Evi1Pumpkin
Copy link
Contributor

Hi @rdash99 ,
Thanks for posting this.
Overall, it is a reasonable thing to support.
It can be a little tricky and will require an RFC to understand how we change the current implementation of the import block to support that.
I want to get more community input about that and hear about the different use cases by more people.

@skyzyx
Copy link

skyzyx commented Aug 29, 2024

I feel like the use-case is under-specified.

It sounds like you're trying to create HCL that matches existing resources, then import the state for those existing resources. Yes?

And the issue is too many top-level files?

@carocad
Copy link

carocad commented Sep 25, 2024

Hi, I can provide some background on our use case.

It sounds like you're trying to create HCL that matches existing resources, then import the state for those existing resources. Yes?

Yes, that is correct. In our case, we have a terraform module that encapsulates the creation of an AWS RDS instance together with other terraform resources. One of the options there is to export the logs to AWS Cloudwatch with this option. When used AWS creates a log group and send the logs there.
The log group creation is outside of terraform so we have no way of control over its parameters. Currently we use the hack below to "manage" the log groups.

The same situation happens when using AWS Lambda functions.

resource "terraform_data" "log_group_retention" {
  for_each = toset(data.aws_cloudwatch_log_groups.this.log_group_names)

  triggers_replace = [var.retention_in_days]
  provisioner "local-exec" {
    command = <<EOT
  aws logs put-retention-policy --log-group-name ${each.key} --retention-in-days ${var.retention_in_days}
  EOT
  }

  provisioner "local-exec" {
    when    = destroy
    command = "aws logs delete-log-group --log-group-name ${each.key}"
  }
}

resource "terraform_data" "log_group_tags" {
  for_each = toset(data.aws_cloudwatch_log_groups.this.arns)

  triggers_replace = [aws_db_instance.this.tags_all]
  provisioner "local-exec" {
    command = "aws logs tag-resource --resource-arn ${each.key} --tags '${local.cli_tag_string}'"
  }
}

And the issue is too many top-level files?

No, the issue is that the functionality is encapsulated in a module and OpenTofu doesn't allow importing inside a module only at the root level. We could ask everyone using our modules to "just import" the resources but that defeats the purpose of encapsulating the functionality inside a module.

Hope it helps :)

@carocad
Copy link

carocad commented Oct 22, 2024

see same terraform issue and discussion: hashicorp/terraform#33474

@klolik
Copy link

klolik commented Nov 28, 2024

Hi, I can add some extra context from my case too.

I'm using Gitops like repository, which calls local submodule (source = "./modules/xyz"), so it's all in one repo.
This submodule has some logic implemented, transforming inputs with a nested for loop into a local.xyz and use it to create resources with for_each.

But I also need to

import {
  for_each = local.xyz
  id = ...
  to = ...
}

Having to move the import statements to the root means all the associated logic has to be either moved to the root or duplicated for the time of resource imports.

@abstractionfactory
Copy link
Contributor

Hey folks, I've moved this back to the queue for the core team to discuss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs-community-input pending-decision This issue has not been accepted for implementation nor rejected. It's still open to discussion.
Projects
None yet
Development

No branches or pull requests

6 participants