title: create terraform resource (S3 Bucket Object) if already doesn't exists
body: <h3>Create the terraform resource based on the data source output condition.</h3>
  <p>Hi all,
  I want to achieve the below requirement in Terraform.</p>
  <p><strong>Requirement:</strong></p>
  <p>I want to create object (with key_name) in s3 bucket.<br />
  Before creating the object need to check whether the object with same key already exists or not using data source.<br />
  If it is already exists, do not create object. If not do create the object in s3 bucket.</p>
  <p><strong>Source Code</strong></p>
  <pre><code>variable &quot;key_name&quot; {
    default = &quot;test.txt&quot;
  }
  variable &quot;bucket_name&quot; {
    default = &quot;test-bucket-654321&quot;
  }
  
  data &quot;aws_s3_bucket_objects&quot; &quot;my_objects&quot; {
    bucket = var.bucket_name
  }
  
  locals {
    key_present = anytrue([
      for key in data.aws_s3_bucket_objects.my_objects.keys: key == var.key_name
    ])
  }
  
  resource &quot;aws_s3_bucket_object&quot; &quot;examplebucket_object&quot; {
    count = local.key_present ? 0 : 1
    
    key     = var.key_name
    bucket  = var.bucket_name
    content = &quot;Test Value&quot;
  }
  
  output &quot;my_objects&quot; {
    value = data.aws_s3_bucket_objects.my_objects.keys
  }
  output &quot;key_present&quot; {
    value = local.key_present
  }
  </code></pre>
  <p>Let's assume, key is does not exists in the bucket (For example, it is a new bucket with no objects)</p>
  <ol>
  <li>On applying 1st time, it creates the object in bucket.</li>
  <li>On applying 2ns time, it destroy the created object in bucket.</li>
  <li>On applying 3rd time, it creates the object in bucket.</li>
  </ol>
  <blockquote>
  <p>On 1st time, data source returns empty value. so it creates the object in bucket.<br />
  On 2nd time, data source returns created object name. so the key is already exists and it destroys the object in bucket.</p>
  </blockquote>
  <p>Likewise every odd number of times it creates the objects and every even number of times it destroys the created object.</p>
  <p><strong>Output:</strong></p>
  <pre><code>ubuntu@test-instance:~$ terraform apply --auto-approve
  aws_s3_bucket_object.examplebucket_object[0]: Creating...
  aws_s3_bucket_object.examplebucket_object[0]: Creation complete after 0s [id=test.txt]
  
  Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  
  Outputs:
  key_present = false
  my_objects = tolist([])
  
  ubuntu@test-instance:~$ terraform apply --auto-approve
  aws_s3_bucket_object.examplebucket_object[0]: Refreshing state... [id=test.txt]
  aws_s3_bucket_object.examplebucket_object[0]: Destroying... [id=test.txt]
  aws_s3_bucket_object.examplebucket_object[0]: Destruction complete after 0s
  
  Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
  
  Outputs:
  key_present = true
  my_objects = tolist([
    &quot;test.txt&quot;,
  ])
  </code></pre>
  <p>Could anyone give me the solution for creating a terraform resource based on the data source output ?</p>