Terraform Data with No Results

I am new to Terraform. I have written data element in terraform but some scenarios data results is not exist so terraform execution is failed without resource creation.
Below is the sample code. When we have appname exist in OKTA then i am not getting any error but i am creating resources only when data okta_app_oauth result not exist. Please let me know how to get this.
data “okta_app_oauth” “example” {
count = var.fedtype==“OIDC” ?1: 0
label = var.appname
active_only = true
skip_users = true
skip_groups = true
}

In general data sources will fail if the resource being requested doesn’t exist. They are expected to be used to reference resources that are not managed by Terraform (or by a different Terraform root module/state).

It sounds like you are wanting to have Terraform create a resource unless the resource already exists. This isn’t something you can do. What you can do is pass in a flag (e.g. via a variable) to decide if you want to create the resource or use an existing one. Alternatively if the resource exists and you want it to be managed by Terraform look at terraform import.

Thanks for the update. Actually we are planning to automate using CI-CD pipeline process. Is there way to add try catch block on data source to continue on terraform execution if data source doesn’t exist ? Can we call rest API to get response for data source (external datasource), if its possible then if you can route me to link or sample snippet to call rest API for data source it would be great help.

You should only use the data source if the resource exists.

If you want some code that could either use an existing resource or create a new one that can be implemented using a boolean and count:

data "something" "example" {
  count = !var.create_resource ? 1 : 0
  ...
}

resource "something" "example" {
  count = var.create_resource ? 1 : 0
  ...
}

variable "create_resource" {
  type    = bool
  default = true
}