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

Do you have a good way to import existing items into state? #1

Closed
Poltergeisen opened this issue Oct 29, 2024 · 6 comments
Closed

Do you have a good way to import existing items into state? #1

Poltergeisen opened this issue Oct 29, 2024 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@Poltergeisen
Copy link

For some reason terraform doesn't pick up that I have existing management groups so I can't easily generate the imports via the code.

Do you have a way to generate import statements from this variable that is set?

@mhosker
Copy link
Owner

mhosker commented Nov 8, 2024

Hi,

Sorry for the delay.

If you were wanting to import, based on the example.tf it would be something like:

terraform import module.management-groups.azurerm_management_group.level_2["MH/MH-Prod"] /providers/Microsoft.Management/managementGroups/MH-Prod

Hope that helps?

Mike :)

@mhosker mhosker added the question Further information is requested label Nov 8, 2024
@mhosker mhosker self-assigned this Nov 8, 2024
@Poltergeisen
Copy link
Author

Poltergeisen commented Nov 11, 2024

Hi, yes, that helps, but I was looking for an import block. I've been playing around with something like this:

import {
  for_each = local.level_2

  id = "/providers/Microsoft.Management/managementGroups/${basename(each.key)}"
  to = module.base.azurerm_management_group.level_2[each.key]
}

I'm not sure what's going on, but for linux it's not working. For my windows machine locally everything seems to import fine...

The message I get on linux:

╷
│ Error: Cannot import non-existent remote object
│ 
│ While attempting to import an existing object to
│ "module.base.azurerm_management_group.level_1[\"lhg\"]", the provider
│ detected that no object exists with the given id. Only pre-existing objects
│ can be imported; check that the id is correct and that it is associated
│ with the provider's configured region or endpoint, or use "terraform apply"
│ to create a new remote object for this resource.

I'm not really sure why this works on windows. I was thinking maybe because of the "\"?

My windows run produces this:
image

@Poltergeisen
Copy link
Author

Poltergeisen commented Nov 11, 2024

Sorry, somewhat debugging here as I type, sorry to blow you up.

I'm just not sure at all why this import statement isn't working in AZDO on my build agent....

image

As you can see it seems to inject \ characters into the module, or maybe the terraform output does that. Either way it's not working

I commented out the first level of import statements to see what resource it'd try to add. From what I can tell the names match up

@Poltergeisen
Copy link
Author

Poltergeisen commented Nov 11, 2024

I also created this issue here, im not really sure where the problem is on my build agents. I'm guessing it has something to do with these build tasks.

microsoft/azure-pipelines-terraform#258

Bah.... been doing more tracing.... I think it's just permissions issues. I'll close this if I get it working

@Poltergeisen
Copy link
Author

This was a permissions error. Thank you again for the help.

@mhosker
Copy link
Owner

mhosker commented Nov 17, 2024

Glad you got this resolved and thanks for commenting the outcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants