Terraform user data pass instance details





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I need to set the host name & default network in the user data for my gcp instance with terraform, (user data template yml as below)



  serverInfo:
serverId: 1
hostname: ${hostname}
defaultNetwork: ${defaultNetwork}


however, after some investigation I realized that this is quite difficult. As far as I understood, the user data is competed before the actual instance creation (correct me if wrong).But the hostname and network details will become available after the instance is created. Could you please suggest me a better way to capture that info and update it in user data?










share|improve this question























  • Why do you need to set this? Also it might be worth showing a slightly expanded example where you show the Terraform code for creating your user data and also creating the instance with the user data.

    – ydaetskcoR
    Nov 25 '18 at 9:31


















1















I need to set the host name & default network in the user data for my gcp instance with terraform, (user data template yml as below)



  serverInfo:
serverId: 1
hostname: ${hostname}
defaultNetwork: ${defaultNetwork}


however, after some investigation I realized that this is quite difficult. As far as I understood, the user data is competed before the actual instance creation (correct me if wrong).But the hostname and network details will become available after the instance is created. Could you please suggest me a better way to capture that info and update it in user data?










share|improve this question























  • Why do you need to set this? Also it might be worth showing a slightly expanded example where you show the Terraform code for creating your user data and also creating the instance with the user data.

    – ydaetskcoR
    Nov 25 '18 at 9:31














1












1








1








I need to set the host name & default network in the user data for my gcp instance with terraform, (user data template yml as below)



  serverInfo:
serverId: 1
hostname: ${hostname}
defaultNetwork: ${defaultNetwork}


however, after some investigation I realized that this is quite difficult. As far as I understood, the user data is competed before the actual instance creation (correct me if wrong).But the hostname and network details will become available after the instance is created. Could you please suggest me a better way to capture that info and update it in user data?










share|improve this question














I need to set the host name & default network in the user data for my gcp instance with terraform, (user data template yml as below)



  serverInfo:
serverId: 1
hostname: ${hostname}
defaultNetwork: ${defaultNetwork}


however, after some investigation I realized that this is quite difficult. As far as I understood, the user data is competed before the actual instance creation (correct me if wrong).But the hostname and network details will become available after the instance is created. Could you please suggest me a better way to capture that info and update it in user data?







terraform






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 23:31









ImranImran

735625




735625













  • Why do you need to set this? Also it might be worth showing a slightly expanded example where you show the Terraform code for creating your user data and also creating the instance with the user data.

    – ydaetskcoR
    Nov 25 '18 at 9:31



















  • Why do you need to set this? Also it might be worth showing a slightly expanded example where you show the Terraform code for creating your user data and also creating the instance with the user data.

    – ydaetskcoR
    Nov 25 '18 at 9:31

















Why do you need to set this? Also it might be worth showing a slightly expanded example where you show the Terraform code for creating your user data and also creating the instance with the user data.

– ydaetskcoR
Nov 25 '18 at 9:31





Why do you need to set this? Also it might be worth showing a slightly expanded example where you show the Terraform code for creating your user data and also creating the instance with the user data.

– ydaetskcoR
Nov 25 '18 at 9:31












2 Answers
2






active

oldest

votes


















1















  • In case you have everything in one backend (you have one terraform state that manages everything), have you checked if any of the output parameters of the gcp instance is valid for your purpose? You can use them with a code snippet like in this examples:


google_compute_instance.name_of_your_instance.network_interface.0.network_ip



google_compute_instance.name_of_your_instance.self_link




  • If you have the gcp instance in a terraform backend different from the one in which you use the user data, maybe you can retrieve the details with terraform_remote_state (See more in https://www.terraform.io/docs/providers/terraform/d/remote_state.html). The nice thing is that this remote state is read only so nothing you do with it can affect your gcp instance.






share|improve this answer



















  • 1





    Thank you very much for the reply, it is close but I need to pass net-mask, network , and broadcast details in user data as well. I extract that info from the cidr info I provided initially, but sometimes the cidr is something like 172.16.10.0/25 but the cloud decided to create a network 172.16.8.0 and it breaks here. So trying to get the info from the actual network created.

    – Imran
    Nov 25 '18 at 22:25



















0














I found a way to fix the issue, so in case of network details, we can get hold of the network as below, and then extract the details about it such as cidr etc. For the host name however, we can specify the name in user data and the cloud-init will set the name for us.



   data "google_compute_subnetwork" "default-subnetwork" {
project = "my-project"
name = "my-subnetwork"
region = "us-central1"
}





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%2f53463322%2fterraform-user-data-pass-instance-details%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















    • In case you have everything in one backend (you have one terraform state that manages everything), have you checked if any of the output parameters of the gcp instance is valid for your purpose? You can use them with a code snippet like in this examples:


    google_compute_instance.name_of_your_instance.network_interface.0.network_ip



    google_compute_instance.name_of_your_instance.self_link




    • If you have the gcp instance in a terraform backend different from the one in which you use the user data, maybe you can retrieve the details with terraform_remote_state (See more in https://www.terraform.io/docs/providers/terraform/d/remote_state.html). The nice thing is that this remote state is read only so nothing you do with it can affect your gcp instance.






    share|improve this answer



















    • 1





      Thank you very much for the reply, it is close but I need to pass net-mask, network , and broadcast details in user data as well. I extract that info from the cidr info I provided initially, but sometimes the cidr is something like 172.16.10.0/25 but the cloud decided to create a network 172.16.8.0 and it breaks here. So trying to get the info from the actual network created.

      – Imran
      Nov 25 '18 at 22:25
















    1















    • In case you have everything in one backend (you have one terraform state that manages everything), have you checked if any of the output parameters of the gcp instance is valid for your purpose? You can use them with a code snippet like in this examples:


    google_compute_instance.name_of_your_instance.network_interface.0.network_ip



    google_compute_instance.name_of_your_instance.self_link




    • If you have the gcp instance in a terraform backend different from the one in which you use the user data, maybe you can retrieve the details with terraform_remote_state (See more in https://www.terraform.io/docs/providers/terraform/d/remote_state.html). The nice thing is that this remote state is read only so nothing you do with it can affect your gcp instance.






    share|improve this answer



















    • 1





      Thank you very much for the reply, it is close but I need to pass net-mask, network , and broadcast details in user data as well. I extract that info from the cidr info I provided initially, but sometimes the cidr is something like 172.16.10.0/25 but the cloud decided to create a network 172.16.8.0 and it breaks here. So trying to get the info from the actual network created.

      – Imran
      Nov 25 '18 at 22:25














    1












    1








    1








    • In case you have everything in one backend (you have one terraform state that manages everything), have you checked if any of the output parameters of the gcp instance is valid for your purpose? You can use them with a code snippet like in this examples:


    google_compute_instance.name_of_your_instance.network_interface.0.network_ip



    google_compute_instance.name_of_your_instance.self_link




    • If you have the gcp instance in a terraform backend different from the one in which you use the user data, maybe you can retrieve the details with terraform_remote_state (See more in https://www.terraform.io/docs/providers/terraform/d/remote_state.html). The nice thing is that this remote state is read only so nothing you do with it can affect your gcp instance.






    share|improve this answer














    • In case you have everything in one backend (you have one terraform state that manages everything), have you checked if any of the output parameters of the gcp instance is valid for your purpose? You can use them with a code snippet like in this examples:


    google_compute_instance.name_of_your_instance.network_interface.0.network_ip



    google_compute_instance.name_of_your_instance.self_link




    • If you have the gcp instance in a terraform backend different from the one in which you use the user data, maybe you can retrieve the details with terraform_remote_state (See more in https://www.terraform.io/docs/providers/terraform/d/remote_state.html). The nice thing is that this remote state is read only so nothing you do with it can affect your gcp instance.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 18:58









    ArconesArcones

    391615




    391615








    • 1





      Thank you very much for the reply, it is close but I need to pass net-mask, network , and broadcast details in user data as well. I extract that info from the cidr info I provided initially, but sometimes the cidr is something like 172.16.10.0/25 but the cloud decided to create a network 172.16.8.0 and it breaks here. So trying to get the info from the actual network created.

      – Imran
      Nov 25 '18 at 22:25














    • 1





      Thank you very much for the reply, it is close but I need to pass net-mask, network , and broadcast details in user data as well. I extract that info from the cidr info I provided initially, but sometimes the cidr is something like 172.16.10.0/25 but the cloud decided to create a network 172.16.8.0 and it breaks here. So trying to get the info from the actual network created.

      – Imran
      Nov 25 '18 at 22:25








    1




    1





    Thank you very much for the reply, it is close but I need to pass net-mask, network , and broadcast details in user data as well. I extract that info from the cidr info I provided initially, but sometimes the cidr is something like 172.16.10.0/25 but the cloud decided to create a network 172.16.8.0 and it breaks here. So trying to get the info from the actual network created.

    – Imran
    Nov 25 '18 at 22:25





    Thank you very much for the reply, it is close but I need to pass net-mask, network , and broadcast details in user data as well. I extract that info from the cidr info I provided initially, but sometimes the cidr is something like 172.16.10.0/25 but the cloud decided to create a network 172.16.8.0 and it breaks here. So trying to get the info from the actual network created.

    – Imran
    Nov 25 '18 at 22:25













    0














    I found a way to fix the issue, so in case of network details, we can get hold of the network as below, and then extract the details about it such as cidr etc. For the host name however, we can specify the name in user data and the cloud-init will set the name for us.



       data "google_compute_subnetwork" "default-subnetwork" {
    project = "my-project"
    name = "my-subnetwork"
    region = "us-central1"
    }





    share|improve this answer




























      0














      I found a way to fix the issue, so in case of network details, we can get hold of the network as below, and then extract the details about it such as cidr etc. For the host name however, we can specify the name in user data and the cloud-init will set the name for us.



         data "google_compute_subnetwork" "default-subnetwork" {
      project = "my-project"
      name = "my-subnetwork"
      region = "us-central1"
      }





      share|improve this answer


























        0












        0








        0







        I found a way to fix the issue, so in case of network details, we can get hold of the network as below, and then extract the details about it such as cidr etc. For the host name however, we can specify the name in user data and the cloud-init will set the name for us.



           data "google_compute_subnetwork" "default-subnetwork" {
        project = "my-project"
        name = "my-subnetwork"
        region = "us-central1"
        }





        share|improve this answer













        I found a way to fix the issue, so in case of network details, we can get hold of the network as below, and then extract the details about it such as cidr etc. For the host name however, we can specify the name in user data and the cloud-init will set the name for us.



           data "google_compute_subnetwork" "default-subnetwork" {
        project = "my-project"
        name = "my-subnetwork"
        region = "us-central1"
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 22 '18 at 11:10









        ImranImran

        735625




        735625






























            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%2f53463322%2fterraform-user-data-pass-instance-details%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini