For_each depends_on

In the log we can see that the repository creation is completing before Terraform tries to create the lifecycle policy:

module.ecr.aws_ecr_repository.ecr["foobaar"]: Creation complete after 0s [id=foobaar]
module.ecr.aws_ecr_lifecycle_policy.lifecycle_policy["foobaar"]: Creating...

So it looks like the dependencies in the configuration are correct, but that the aws_ecr_repository implementation in the AWS provider is returning success before the repository has fully committed. If that is true, unfortunately I don’t think there’s any way to strengthen that dependency from within the Terraform language: the provider would need to make sure the repository is ready to use before indicating that the creation has completed successfully.

Although the error message is not exactly the same, the overall behavior here seems similar to the following issue in the AWS provider:

Since that was originally opened in 2017, I think it’s plausible that the exact error message text returned from the remote API could’ve changed in the meantime, because both your message and the one reported there both seem to be saying (in different words) that the policy refers to a non-existing repository.