# Create resources conditionally without using count

**URL:** <https://discuss.hashicorp.com/t/create-resources-conditionally-without-using-count/21025>\
**Category:** Terraform\
**Created:** [February 16, 2021, 9:21pm UTC](https://discuss.hashicorp.com/t/create-resources-conditionally-without-using-count/21025 "2021-02-16T21:21:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisadkin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/chrisadkin/32/16785_2.png) [@chrisadkin](https://discuss.hashicorp.com/u/chrisadkin)\
**Post date:** [February 16, 2021, 9:21pm UTC](https://discuss.hashicorp.com/t/create-resources-conditionally-without-using-count/21025/1 "2021-02-16T21:21:28Z")

</div>

A well known technique to control the conditional creation of resources is to use something like:

`count = var.create_resource ? 1 : 0`

and I tried this with the Azure provider on resources such as:

```
resource "azuread_service_principal_password" "auth" {
  service_principal_id = azuread_service_principal.auth.id
  value = random_string.password.result
  end_date_relative = "240h"
}

```

The problem with this is that because there is no for\_each loop involved terraform plan instructed me to use an index on (for example) value = random\_string.password.result, the problem being that this does not like an index. My question is therefore this, how can I achieve the same result as using count but without using count.

---

<div class="post-metadata">

**Author:** ![tbugfinder](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tbugfinder/32/12689_2.png) [@tbugfinder](https://discuss.hashicorp.com/u/tbugfinder)\
**Post date:** [February 16, 2021, 10:08pm UTC](https://discuss.hashicorp.com/t/create-resources-conditionally-without-using-count/21025/2 "2021-02-16T22:08:29Z")

</div>

Hi @chrisadkin ,  
as per my understanding you’d like to create \>=0 resources of type `azuread_service_principal_password` and also use unique `random_string.password`s for those.  
In principal you can use the same count or for\_each iterator on both resources, so creating equal number of resources.

---

<div class="post-metadata">

**Author:** ![chrisadkin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/chrisadkin/32/16785_2.png) [@chrisadkin](https://discuss.hashicorp.com/u/chrisadkin)\
**Post date:** [February 16, 2021, 10:20pm UTC](https://discuss.hashicorp.com/t/create-resources-conditionally-without-using-count/21025/3 "2021-02-16T22:20:28Z")

</div>

What I would like to do is create a service principle and the other resources associated with it if a variable is set to true, otherwise don’t create it at all.

---

<div class="post-metadata">

**Author:** ![tbugfinder](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tbugfinder/32/12689_2.png) [@tbugfinder](https://discuss.hashicorp.com/u/tbugfinder)\
**Post date:** [February 17, 2021, 9:00pm UTC](https://discuss.hashicorp.com/t/create-resources-conditionally-without-using-count/21025/4 "2021-02-17T21:00:15Z")

</div>

Isn’t the variable (true, count) also applicable to all resources which could be conditional?

---

<div class="post-metadata">

**Author:** ![stuart-c](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/stuart-c/32/10789_2.png) [@stuart-c](https://discuss.hashicorp.com/u/stuart-c)\
**Post date:** [February 17, 2021, 11:10pm UTC](https://discuss.hashicorp.com/t/create-resources-conditionally-without-using-count/21025/5 "2021-02-17T23:10:57Z")

</div>

As you say the `count = var.create_resource ? 1 : 0` mechanism is very common to conditionally create a resource. You would just add that count to whichever resources you want to be conditional.

As those resources now have a count you need to ensure any references to them now use an index value.

So for example

```auto
resource "azuread_service_principal_password" "auth" {
  count = var.create_resource ? 1 : 0

  service_principal_id = azuread_service_principal.auth.id
  value = random_string.password.result
  end_date_relative = "240h"
}

```

Would now be referenced as `azuread_service_principal_password.auth[0].whatever` if the resource is enabled. You need to take account of the possibility that is isn’t enabled, for example using a condition check: `variable = var.create_resource ? azuread_service_principal_password.auth[0].whatever : ""`

---

<div class="post-metadata">

**Author:** ![chrisadkin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/chrisadkin/32/16785_2.png) [@chrisadkin](https://discuss.hashicorp.com/u/chrisadkin)\
**Post date:** [February 18, 2021, 8:30pm UTC](https://discuss.hashicorp.com/t/create-resources-conditionally-without-using-count/21025/6 "2021-02-18T20:30:34Z")

</div>

Taking a step back, what I need to do is to restructure my code in order to use modules.
