2

I am new to Terraform and trying to understand data sources. I have read the documentation and this StackOverflow post, but I'm still unclear about the use cases of data source.

I have the following block of code:

resource "azurerm_resource_group" "rg" {
  name     = "example-resource-group"
  location = "West US 2"
}


data "azurerm_resource_group" "test" {
  name = "example-resource-group"
}

But I get a 404 error:

  • data.azurerm_resource_group.test: data.azurerm_resource_group.test: resources.GroupsClient#Get: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="ResourceGroupNotFound" Message="Resource group 'example-resource-group' could not be found."

I don't understand why the resource group is not found. Also, I am unclear about the difference between data and variable and when should I use which.

Thanks

2 Answers 2

3

I have provided a detailed explanation of what a data source is in this SO answer. To summarize:

  • Data sources provide dynamic information about entities that are not managed by the current Terraform configuration
  • Variables provide static information

Your block of code doesn't work because the resource your data source is referencing hasn't been created yet. During the planning phase, Terraform will try to find a resource group named example-resource-group, but it won't find it, and so it aborts the whole run. The ordering of the blocks makes no difference to the order they are applied.

If you remove the data block, run terraform apply, and then add the data block back in, it should work. However, data sources are used to retrieve data about entities that are not managed by your Terraform configuration. In your case, you don't need the data.azurerm_resource_group.test data source, you can simply use the exported attributes from the resource. In the case of azurerm_resource_group, this is a single id attribute.

3
  • @d4nynyll Your answer is good, but i think you have a mistake - you attach a link on outputs in the "exported_attributes" part. As far as i know, those are 2 completely different entities. Mar 6, 2019 at 8:50
  • 2
    @Rotemya To clarify then - each module uses an output block to export attributes. From the link - 'Resource instances managed by Terraform each export attributes whose values can be used elsewhere in configuration.' If you can find a better link please feel free to update it :)
    – d4nyll
    Mar 6, 2019 at 11:34
  • Can't you get access to a resource that was created by Terraform by the data? Jul 19, 2022 at 14:48
0

Think of a data source as a value you want to read from somewhere else.

A variable is something you define when you run the code.

When you use the data source for azurerm_resource_group terraform will search for an existing resource that has the name you defined in your data source block.

Example

    data "azurerm_resource_group" "test" {
      name = "example-resource-group"
    }

Quoting @ydaetskcoR from the comment below about 404 error:

It's 404ing because the data source is running before the resource creates the thing you are looking for. You would use a data source when the resource has already been created previously, not in the same run as the resource you are creating.

3
  • Thank you for your help. I have updated the question. I still get 404 error given that my azurerm_resource_group "resource" is on top of azurerm_resource_group "data"
    – Amir
    Apr 10, 2018 at 20:29
  • The 404 is probably because terraform is calling that before creating the resource. Maybe share an example of the code? You don't have to use the data source, you can access the response of the resource if you need to know the values.
    – Stephen
    Apr 10, 2018 at 21:22
  • 2
    It's 404ing because the data source is running before the resource creates the thing you are looking for. You would use a data source when the resource has already been created previously, not in the same run as the resource you are creating. The example in terraform.io/docs/providers/azurerm/d/resource_group.html gives a good example where a resource group has been previously created in another Terraform context (or potentially by another process entirely) but you want to know the resource group location for creating another resource.
    – ydaetskcoR
    Apr 12, 2018 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.