Set defaults or ignore when remote state file is not found

HI Team,

I am trying to read and use the remote state from s3 bucket as below

data “terraform_remote_state” “green” {
backend = “s3”
config = {
bucket = test
key = green.tfstate
region = us-east-1
profile = “aas”
}
}

resource “aws_lb_target_group_attachment” “tg-green” {
count = length(data.terraform_remote_state.green) > 0 ? 1 : 0
target_group_arn = xxxxx
target_id = data.terraform_remote_state.green.outputs.instance_id
}

The problem in this is there are cases where there will be no remote state present in the s3 bucket for that I have set in the next block
count = length(data.terraform_remote_state.green) > 0 ? 1 : 0 so that will proceed only when file has data.

When I run terraform apply will fail stating the remote file is not found in s3 bucket.

I am looking to set defaults or ignore a remote state block if the state file is not found.

Please suggest me on this.

The error I see when I run apply is as shown below

Error: Unable to find remote state

with data.terraform_remote_state.green,
on remote_states.tf line 35, in data “terraform_remote_state” “green”:
35: data “terraform_remote_state” “green” {

No stored state was found for the given workspace in the given backend.

I would looking to skip the block when the state file is not found and proceed to next step.

Please suggest.