Retrieve Azure VNET subnet IDs with Terraform












0















I want to retrieve/pull the subnet IDs from an existing VNET in Azure using the azurerm_virtual_network data source. My code below is not working as expected. Any help is appreciated.



data "azurerm_virtual_network" "vnet" {
name = "my-vnet"
resource_group_name = "my-resource-group"
}

output "my-subnets-ids" {
value = "${data.azurerm_virtual_network.vnet.subnets.id}"
}


I am receiving the following error when I execute.



output.my-subnets: Resource 'data.azurerm_virtual_network.vnet' 
does not have attribute 'subnets.id' for variable
'data.azurerm_virtual_network.vnet.subnets.id'









share|improve this question



























    0















    I want to retrieve/pull the subnet IDs from an existing VNET in Azure using the azurerm_virtual_network data source. My code below is not working as expected. Any help is appreciated.



    data "azurerm_virtual_network" "vnet" {
    name = "my-vnet"
    resource_group_name = "my-resource-group"
    }

    output "my-subnets-ids" {
    value = "${data.azurerm_virtual_network.vnet.subnets.id}"
    }


    I am receiving the following error when I execute.



    output.my-subnets: Resource 'data.azurerm_virtual_network.vnet' 
    does not have attribute 'subnets.id' for variable
    'data.azurerm_virtual_network.vnet.subnets.id'









    share|improve this question

























      0












      0








      0








      I want to retrieve/pull the subnet IDs from an existing VNET in Azure using the azurerm_virtual_network data source. My code below is not working as expected. Any help is appreciated.



      data "azurerm_virtual_network" "vnet" {
      name = "my-vnet"
      resource_group_name = "my-resource-group"
      }

      output "my-subnets-ids" {
      value = "${data.azurerm_virtual_network.vnet.subnets.id}"
      }


      I am receiving the following error when I execute.



      output.my-subnets: Resource 'data.azurerm_virtual_network.vnet' 
      does not have attribute 'subnets.id' for variable
      'data.azurerm_virtual_network.vnet.subnets.id'









      share|improve this question














      I want to retrieve/pull the subnet IDs from an existing VNET in Azure using the azurerm_virtual_network data source. My code below is not working as expected. Any help is appreciated.



      data "azurerm_virtual_network" "vnet" {
      name = "my-vnet"
      resource_group_name = "my-resource-group"
      }

      output "my-subnets-ids" {
      value = "${data.azurerm_virtual_network.vnet.subnets.id}"
      }


      I am receiving the following error when I execute.



      output.my-subnets: Resource 'data.azurerm_virtual_network.vnet' 
      does not have attribute 'subnets.id' for variable
      'data.azurerm_virtual_network.vnet.subnets.id'






      azure terraform






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 18:21









      chew224chew224

      658




      658
























          2 Answers
          2






          active

          oldest

          votes


















          1














          I would combine this with the subnet data resource and use it to get all your ids. This code is close, I don't have time to pull it up and work out the full syntax.



          data "azurerm_subnet" "test" {
          name = "${data.azurerm_virtual_network.vnet.subnets[count.index]}"
          virtual_network_name = "${data.azurerm_virtual_network.vnet.name}"
          resource_group_name = "${data.azurerm_virtual_network.vnet.resource_group_name"
          count = "${count(data.azurerm_virtual_network.vnet.subnets)}"
          }

          output "subnet_ids" {
          value = "${data.azurerm_subnet.test.*.id}"
          }





          share|improve this answer
























          • that kinda does the trick, but doesnt make a lot of sense, since you would need a DATA block for every subnet, easier to just use existing vnet data block

            – 4c74356b41
            Jan 3 at 18:57











          • You don't have to add a data block for every subnet as I am using the count and interpolation to get it. It does stink.

            – Jamie
            Jan 3 at 19:25











          • Jamie is right. This will pull all the subnets in the VNET without having a data block for every subnet. Very clean. Thanks!

            – chew224
            Jan 3 at 21:34



















          0














          according to this it only contains list of names of the subnets, not their ids, so you would need to construct those manually. easiest way - take vnet.id and add '/subnets/${each subnet name goes here}'






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027756%2fretrieve-azure-vnet-subnet-ids-with-terraform%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I would combine this with the subnet data resource and use it to get all your ids. This code is close, I don't have time to pull it up and work out the full syntax.



            data "azurerm_subnet" "test" {
            name = "${data.azurerm_virtual_network.vnet.subnets[count.index]}"
            virtual_network_name = "${data.azurerm_virtual_network.vnet.name}"
            resource_group_name = "${data.azurerm_virtual_network.vnet.resource_group_name"
            count = "${count(data.azurerm_virtual_network.vnet.subnets)}"
            }

            output "subnet_ids" {
            value = "${data.azurerm_subnet.test.*.id}"
            }





            share|improve this answer
























            • that kinda does the trick, but doesnt make a lot of sense, since you would need a DATA block for every subnet, easier to just use existing vnet data block

              – 4c74356b41
              Jan 3 at 18:57











            • You don't have to add a data block for every subnet as I am using the count and interpolation to get it. It does stink.

              – Jamie
              Jan 3 at 19:25











            • Jamie is right. This will pull all the subnets in the VNET without having a data block for every subnet. Very clean. Thanks!

              – chew224
              Jan 3 at 21:34
















            1














            I would combine this with the subnet data resource and use it to get all your ids. This code is close, I don't have time to pull it up and work out the full syntax.



            data "azurerm_subnet" "test" {
            name = "${data.azurerm_virtual_network.vnet.subnets[count.index]}"
            virtual_network_name = "${data.azurerm_virtual_network.vnet.name}"
            resource_group_name = "${data.azurerm_virtual_network.vnet.resource_group_name"
            count = "${count(data.azurerm_virtual_network.vnet.subnets)}"
            }

            output "subnet_ids" {
            value = "${data.azurerm_subnet.test.*.id}"
            }





            share|improve this answer
























            • that kinda does the trick, but doesnt make a lot of sense, since you would need a DATA block for every subnet, easier to just use existing vnet data block

              – 4c74356b41
              Jan 3 at 18:57











            • You don't have to add a data block for every subnet as I am using the count and interpolation to get it. It does stink.

              – Jamie
              Jan 3 at 19:25











            • Jamie is right. This will pull all the subnets in the VNET without having a data block for every subnet. Very clean. Thanks!

              – chew224
              Jan 3 at 21:34














            1












            1








            1







            I would combine this with the subnet data resource and use it to get all your ids. This code is close, I don't have time to pull it up and work out the full syntax.



            data "azurerm_subnet" "test" {
            name = "${data.azurerm_virtual_network.vnet.subnets[count.index]}"
            virtual_network_name = "${data.azurerm_virtual_network.vnet.name}"
            resource_group_name = "${data.azurerm_virtual_network.vnet.resource_group_name"
            count = "${count(data.azurerm_virtual_network.vnet.subnets)}"
            }

            output "subnet_ids" {
            value = "${data.azurerm_subnet.test.*.id}"
            }





            share|improve this answer













            I would combine this with the subnet data resource and use it to get all your ids. This code is close, I don't have time to pull it up and work out the full syntax.



            data "azurerm_subnet" "test" {
            name = "${data.azurerm_virtual_network.vnet.subnets[count.index]}"
            virtual_network_name = "${data.azurerm_virtual_network.vnet.name}"
            resource_group_name = "${data.azurerm_virtual_network.vnet.resource_group_name"
            count = "${count(data.azurerm_virtual_network.vnet.subnets)}"
            }

            output "subnet_ids" {
            value = "${data.azurerm_subnet.test.*.id}"
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 18:50









            JamieJamie

            1,208920




            1,208920













            • that kinda does the trick, but doesnt make a lot of sense, since you would need a DATA block for every subnet, easier to just use existing vnet data block

              – 4c74356b41
              Jan 3 at 18:57











            • You don't have to add a data block for every subnet as I am using the count and interpolation to get it. It does stink.

              – Jamie
              Jan 3 at 19:25











            • Jamie is right. This will pull all the subnets in the VNET without having a data block for every subnet. Very clean. Thanks!

              – chew224
              Jan 3 at 21:34



















            • that kinda does the trick, but doesnt make a lot of sense, since you would need a DATA block for every subnet, easier to just use existing vnet data block

              – 4c74356b41
              Jan 3 at 18:57











            • You don't have to add a data block for every subnet as I am using the count and interpolation to get it. It does stink.

              – Jamie
              Jan 3 at 19:25











            • Jamie is right. This will pull all the subnets in the VNET without having a data block for every subnet. Very clean. Thanks!

              – chew224
              Jan 3 at 21:34

















            that kinda does the trick, but doesnt make a lot of sense, since you would need a DATA block for every subnet, easier to just use existing vnet data block

            – 4c74356b41
            Jan 3 at 18:57





            that kinda does the trick, but doesnt make a lot of sense, since you would need a DATA block for every subnet, easier to just use existing vnet data block

            – 4c74356b41
            Jan 3 at 18:57













            You don't have to add a data block for every subnet as I am using the count and interpolation to get it. It does stink.

            – Jamie
            Jan 3 at 19:25





            You don't have to add a data block for every subnet as I am using the count and interpolation to get it. It does stink.

            – Jamie
            Jan 3 at 19:25













            Jamie is right. This will pull all the subnets in the VNET without having a data block for every subnet. Very clean. Thanks!

            – chew224
            Jan 3 at 21:34





            Jamie is right. This will pull all the subnets in the VNET without having a data block for every subnet. Very clean. Thanks!

            – chew224
            Jan 3 at 21:34













            0














            according to this it only contains list of names of the subnets, not their ids, so you would need to construct those manually. easiest way - take vnet.id and add '/subnets/${each subnet name goes here}'






            share|improve this answer




























              0














              according to this it only contains list of names of the subnets, not their ids, so you would need to construct those manually. easiest way - take vnet.id and add '/subnets/${each subnet name goes here}'






              share|improve this answer


























                0












                0








                0







                according to this it only contains list of names of the subnets, not their ids, so you would need to construct those manually. easiest way - take vnet.id and add '/subnets/${each subnet name goes here}'






                share|improve this answer













                according to this it only contains list of names of the subnets, not their ids, so you would need to construct those manually. easiest way - take vnet.id and add '/subnets/${each subnet name goes here}'







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 18:37









                4c74356b414c74356b41

                32.3k42557




                32.3k42557






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027756%2fretrieve-azure-vnet-subnet-ids-with-terraform%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas