# Conditional s3 bucket creation

**URL:** <https://discuss.hashicorp.com/t/conditional-s3-bucket-creation/42185>\
**Category:** Terraform\
**Tags:** hcp-terraform\
**Created:** [July 19, 2022, 6:43pm UTC](https://discuss.hashicorp.com/t/conditional-s3-bucket-creation/42185 "2022-07-19T18:43:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chell0veck](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/chell0veck/32/29546_2.png) [@chell0veck](https://discuss.hashicorp.com/u/chell0veck)\
**Post date:** [July 19, 2022, 6:43pm UTC](https://discuss.hashicorp.com/t/conditional-s3-bucket-creation/42185/1 "2022-07-19T18:43:47Z")

</div>

Hi there!

Quick question.  
Is there any non documented functionality to make bucket creation optional depending on binary flag?

Something like:

```auto
resource "aws_s3_bucket" "this" {
  count = var.create_bucket ? 1 : 0
  bucket = "name"

```

Closest I found - [amazon web services - How do I conditionally create an S3 bucket - Stack Overflow](https://stackoverflow.com/questions/70342102/how-do-i-conditionally-create-an-s3-bucket).

Alternatively it can be declared in separate in tf file but how to make it added into execution optionally in native way and without mangling with shell?

Regards,  
Serhii

---

<div class="post-metadata">

**Author:** ![maxb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/maxb/32/26655_2.png) [@maxb](https://discuss.hashicorp.com/u/maxb)\
**Post date:** [July 19, 2022, 7:43pm UTC](https://discuss.hashicorp.com/t/conditional-s3-bucket-creation/42185/2 "2022-07-19T19:43:47Z")

</div>

Exactly what you already wrote in your post.

---

<div class="post-metadata">

**Author:** ![chell0veck](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/chell0veck/32/29546_2.png) [@chell0veck](https://discuss.hashicorp.com/u/chell0veck)\
**Post date:** [July 19, 2022, 8:32pm UTC](https://discuss.hashicorp.com/t/conditional-s3-bucket-creation/42185/3 "2022-07-19T20:32:47Z")

</div>

Thanks @maxb,

Just tested …  
But let’s say we have existing module with plenty of resources to be dependent on bucket.  
I need to make creation optional but adding `count` brings index into the picture and change interface.  
Is it possible to avoid adding index and use true boolean - not count?

Update:

Also using count with constant name resulting rather a bit confusing result with 3 resources in state that points to the same actual bucket.

```auto
terraform state list                                                                                                                                                               
aws_s3_bucket.this[0]
aws_s3_bucket.this[1]
aws_s3_bucket.this[2]

```

Regards,  
Serhii

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [July 20, 2022, 12:17am UTC](https://discuss.hashicorp.com/t/conditional-s3-bucket-creation/42185/4 "2022-07-20T00:17:39Z")

</div>

Hi @chell0veck,

Both `count` and `for_each` cause Terraform to track the resource as a collection of instances and create the possibility of there being zero instances of the resource. Without either of those arguments, there is always exactly one instance of the resource, whose address matches the address of the resource itself.

It’s intentional that this changes how you must refer to the instances of the resource elsewhere in your module, because you will need to now write expressions that can support there being zero instances, whereas with a singleton resource (one without `count` or `for_each`) you can always assume that a reference to the resource will yield exactly one resource object.

---

<div class="post-metadata">

**Author:** ![chell0veck](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/chell0veck/32/29546_2.png) [@chell0veck](https://discuss.hashicorp.com/u/chell0veck)\
**Post date:** [July 20, 2022, 8:48am UTC](https://discuss.hashicorp.com/t/conditional-s3-bucket-creation/42185/5 "2022-07-20T08:48:10Z")

</div>

Thanks a lot @apparentlymart ,

May I ask if it possible to make `resource` execution optional, just like commenting the block but within hcl?

Off-top - I assume that’s expected but in case of below example:

```auto
resource "aws_s3_bucket" "this" {
  count = 3
  bucket = "test-bucket-1-887788"
}

output "buckets" {
  value = aws_s3_bucket.this[*].arn
}

```

that gives an collection of 3 but with 1 actual resource.

```auto
buckets = [
  "arn:aws:s3:::test-bucket-1-887788",
  "arn:aws:s3:::test-bucket-1-887788",
  "arn:aws:s3:::test-bucket-1-887788",
]

```

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [July 22, 2022, 12:19am UTC](https://discuss.hashicorp.com/t/conditional-s3-bucket-creation/42185/6 "2022-07-22T00:19:37Z")

</div>

Hi @chell0veck,

In Terraform there is a distinction between a _resource_, which is more or less the `resource` block you write in the configuration, and _instances of a resource_, which are Terraform’s representation of the remote objects you’re managing with Terraform.

_Resource instances_ can be declared dynamically, using `count` and `for_each`, but resources themselves are static: if they are written in the configuration they exist, and if they aren’t written in the configuration they aren’t.

It might help to consider an analogy to a variable declaration vs. a variables value in a general-purpose programming language. Ignoring some very dynamic languages that blur this distinction by allowing you to modify the scope dynamically at runtime, you generally don’t get to conditionally decide whether a variable is _declared_ or not, but you do get to dynamically decide what to associate with it. For example, in the Go programming language:

```auto
var example []string
if someCondition {
  example = []string{"hello"}
} else {
  example = []string{}
}
// (this is not really idiomatic Go, but I'm
// writing things out more explicitly for the
// sake of the analogy)

```

This `example` variable exists regardless of whether `someCondition` is true or false, but in some cases it’s a zero-length list and in other cases it’s a one-element list. There is no way to dynamically decide not to declare `example` at all, but you can comment it out in the source code if you want the compiler to ignore it.

A `resource` block with `count` set is analogous to the `var example []string` declaration in the above, and the `count` expression itself is _roughly_ analogous to the `if` statement that follows it.

I’m not sure what you were intending to ask about this example with three S3 buckets, but as with my analogy above you can dynamically decide between having three elements or zero elements (by putting a conditional expression in the `count` argument), but you can’t dynamically decide that `resource "aws_s3_bucket" "this"` isn’t declared at all.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/hashicorp/original/1X/79bf76adfd92a75631996933098f9ce29d37abf3.svg) [@system](https://discuss.hashicorp.com/u/system)\
**Post date:** [April 2, 2024, 8:51am UTC](https://discuss.hashicorp.com/t/conditional-s3-bucket-creation/42185/7 "2024-04-02T08:51:11Z")

</div>

This topic was automatically closed 62 days after the last reply. New replies are no longer allowed.
