Excluding resource in Terraform module











up vote
1
down vote

favorite












I have a Terraform module which I built that deploys two instances of the module to a separate region. In this module there is a key vault. I only want the key vault to be present in one of the two regions.



Here is my module:



resource "azurerm_resource_group" "test" {
name = "test"
location = "${var.location}"
}

resource "azurerm_key_vault" "keyvault" {
name = "keyvault"
}


Here is my main.tf



module "test_uswest2" {
source = "modules/test"

location = "westus2"
environment = "${var.environment}"
}

module "test_westcentralus" {
source = "modules/test"

location = "centralus"
environment = "${var.environment}"
}


I want to exclude the key vault in the second region/location.



Terraform doesn't seem to support if/else so I'm not sure what my options are.










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have a Terraform module which I built that deploys two instances of the module to a separate region. In this module there is a key vault. I only want the key vault to be present in one of the two regions.



    Here is my module:



    resource "azurerm_resource_group" "test" {
    name = "test"
    location = "${var.location}"
    }

    resource "azurerm_key_vault" "keyvault" {
    name = "keyvault"
    }


    Here is my main.tf



    module "test_uswest2" {
    source = "modules/test"

    location = "westus2"
    environment = "${var.environment}"
    }

    module "test_westcentralus" {
    source = "modules/test"

    location = "centralus"
    environment = "${var.environment}"
    }


    I want to exclude the key vault in the second region/location.



    Terraform doesn't seem to support if/else so I'm not sure what my options are.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a Terraform module which I built that deploys two instances of the module to a separate region. In this module there is a key vault. I only want the key vault to be present in one of the two regions.



      Here is my module:



      resource "azurerm_resource_group" "test" {
      name = "test"
      location = "${var.location}"
      }

      resource "azurerm_key_vault" "keyvault" {
      name = "keyvault"
      }


      Here is my main.tf



      module "test_uswest2" {
      source = "modules/test"

      location = "westus2"
      environment = "${var.environment}"
      }

      module "test_westcentralus" {
      source = "modules/test"

      location = "centralus"
      environment = "${var.environment}"
      }


      I want to exclude the key vault in the second region/location.



      Terraform doesn't seem to support if/else so I'm not sure what my options are.










      share|improve this question















      I have a Terraform module which I built that deploys two instances of the module to a separate region. In this module there is a key vault. I only want the key vault to be present in one of the two regions.



      Here is my module:



      resource "azurerm_resource_group" "test" {
      name = "test"
      location = "${var.location}"
      }

      resource "azurerm_key_vault" "keyvault" {
      name = "keyvault"
      }


      Here is my main.tf



      module "test_uswest2" {
      source = "modules/test"

      location = "westus2"
      environment = "${var.environment}"
      }

      module "test_westcentralus" {
      source = "modules/test"

      location = "centralus"
      environment = "${var.environment}"
      }


      I want to exclude the key vault in the second region/location.



      Terraform doesn't seem to support if/else so I'm not sure what my options are.







      terraform terraform-provider-azure






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 9:18









      ydaetskcoR

      20.3k45372




      20.3k45372










      asked Nov 7 at 17:49









      sleepyj

      186




      186
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          You can conditionally create resources by setting the count meta parameter to 0 and using the conditional ternary operator.



          So you would have something like this:



          variable "create_key_vault" {
          default = true
          }

          resource "azurerm_key_vault" "keyvault" {
          count = "${var.create_key_vault ? 0 : 1}"
          name = "keyvault"
          }


          Then call your module with create_key_vault set to false to not create it:



          module "test_westcentralus" {
          source = "modules/test"

          location = "centralus"
          environment = "${var.environment}"
          create_key_vault = false
          }





          share|improve this answer























          • Just after posting this I realized the count. Thanks!
            – sleepyj
            Nov 14 at 16:09











          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',
          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%2f53194995%2fexcluding-resource-in-terraform-module%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          You can conditionally create resources by setting the count meta parameter to 0 and using the conditional ternary operator.



          So you would have something like this:



          variable "create_key_vault" {
          default = true
          }

          resource "azurerm_key_vault" "keyvault" {
          count = "${var.create_key_vault ? 0 : 1}"
          name = "keyvault"
          }


          Then call your module with create_key_vault set to false to not create it:



          module "test_westcentralus" {
          source = "modules/test"

          location = "centralus"
          environment = "${var.environment}"
          create_key_vault = false
          }





          share|improve this answer























          • Just after posting this I realized the count. Thanks!
            – sleepyj
            Nov 14 at 16:09















          up vote
          5
          down vote



          accepted










          You can conditionally create resources by setting the count meta parameter to 0 and using the conditional ternary operator.



          So you would have something like this:



          variable "create_key_vault" {
          default = true
          }

          resource "azurerm_key_vault" "keyvault" {
          count = "${var.create_key_vault ? 0 : 1}"
          name = "keyvault"
          }


          Then call your module with create_key_vault set to false to not create it:



          module "test_westcentralus" {
          source = "modules/test"

          location = "centralus"
          environment = "${var.environment}"
          create_key_vault = false
          }





          share|improve this answer























          • Just after posting this I realized the count. Thanks!
            – sleepyj
            Nov 14 at 16:09













          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          You can conditionally create resources by setting the count meta parameter to 0 and using the conditional ternary operator.



          So you would have something like this:



          variable "create_key_vault" {
          default = true
          }

          resource "azurerm_key_vault" "keyvault" {
          count = "${var.create_key_vault ? 0 : 1}"
          name = "keyvault"
          }


          Then call your module with create_key_vault set to false to not create it:



          module "test_westcentralus" {
          source = "modules/test"

          location = "centralus"
          environment = "${var.environment}"
          create_key_vault = false
          }





          share|improve this answer














          You can conditionally create resources by setting the count meta parameter to 0 and using the conditional ternary operator.



          So you would have something like this:



          variable "create_key_vault" {
          default = true
          }

          resource "azurerm_key_vault" "keyvault" {
          count = "${var.create_key_vault ? 0 : 1}"
          name = "keyvault"
          }


          Then call your module with create_key_vault set to false to not create it:



          module "test_westcentralus" {
          source = "modules/test"

          location = "centralus"
          environment = "${var.environment}"
          create_key_vault = false
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 8 at 18:08

























          answered Nov 7 at 19:19









          ydaetskcoR

          20.3k45372




          20.3k45372












          • Just after posting this I realized the count. Thanks!
            – sleepyj
            Nov 14 at 16:09


















          • Just after posting this I realized the count. Thanks!
            – sleepyj
            Nov 14 at 16:09
















          Just after posting this I realized the count. Thanks!
          – sleepyj
          Nov 14 at 16:09




          Just after posting this I realized the count. Thanks!
          – sleepyj
          Nov 14 at 16:09


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194995%2fexcluding-resource-in-terraform-module%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







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings