hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.53k stars 4.61k forks source link

referencing data about an azurerm_image #2013

Closed mjdavies closed 5 years ago

mjdavies commented 5 years ago

Hi there

I need to get the id of an azurerm_image in order to create further vm's.

Read this

https://www.terraform.io/docs/providers/azurerm/d/image.html

The issue I'm having, is that I can't run terraform plan with the data resource type in, as it complains that the azurerm_image doesn't exist, which is true, as I'm running the plan.

If I comment out the data resource type, run plan and apply, then uncomment it, it all works fine.

I'm obviously doing something stupid, could someone confirm if that's the way it's supposed to work, or if there's a less stupid way of doing this?

Thanks

mjdavies commented 5 years ago

A big thanks to Rob Coward for explaining a very simple solution that for some reason I could not see!!

So, if I understand your question correctly, in your terraform config you are using a resource “azurerm_image” “new_image” {…. definition to create a new image, and you want to then use that new image to create VMs ?

If that is the case, there’s no need to use a datasource - just use “${azurerm_image.new_image.id}” instead of your “${data.azurerm_image.search.id}”.

That wasn't immediately obvious to me to be honest, as all the examples I'd seen were using the data object, with no warning that it wouldn't work in a terraform plan.

Unless I'm doing something else wrong?

tombuildsstuff commented 5 years ago

hey @mjdavies

Thanks for opening this issue :)

If I comment out the data resource type, run plan and apply, then uncomment it, it all works fine.

A Data Source in Terraform is used to obtain information about a pre-existing resource, whereas a Resource is used to manage a resource (including creating, updating and deleting it) as needed. In this case the item being specified in the Data Source doesn't exist - such that Terraform will return an error since the information in it is needed.

That wasn't immediately obvious to me to be honest, as all the examples I'd seen were using the data object, with no warning that it wouldn't work in a terraform plan.

This behaviour (where a Data Source requires that the Resource it references exists) isn't specific specific to the AzureRM Provider, but rather is the same for all Data Sources - as such it's documented this on the Data Source page.

Specifically within the AzureRM Provider we're trying to ensure the language used in the Data Sources documentation reflects that this resource is required to exist (for example - Use this data source to access the properties of an existing Azure Public IP Address) - however I'd be interested to know if you think there's a way this could have been clearer so that we can help here in the future? :)

Since it appears this question has been resolved I'm going to close this issue for the moment (but we'll continue responding 😄)

Thanks!

mjdavies commented 5 years ago

Thanks for getting back to me Tom

https://www.terraform.io/docs/providers/azurerm/d/image.html

That was the page I was looking at, and it's description is

Use this data source to access information about an Image.

Notice the missing "existing"?

I couldn't work out how you could access data about something that doesn't exist myself, and you obviously can't, but that one word missing in the docs, and me not having used data objects before was a lethal combination!!

Anyway, we got it sorted, and your docs, product and support is excellent on the whole Tom, thanks again.

Matt

tombuildsstuff commented 5 years ago

hey @mjdavies

Thanks for the info - I've opened #2033 which updates the Data Source documentation to ensure this is consistent :)

Thanks!

ghost commented 5 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!