There is no way built in to Terraform to ask it to exit without waiting for a remote object to be confirmed created, because in common Terraform usage there will tend to be other operations depending on the result of that operation and thus no further work would be possible.
Unfortunately I think the closest you could get to what you are trying for with current Terraform is to create the elasticsearch domain some other way – via the AWS CLI, perhaps – and then use
terraform import to connect that object id with the resource declared in your Terraform configuration. That rather seems to defeat the object though, since Terraform won’t really be doing anything with the elasticsearch domain in that case.
My answer above is trying to accommodate your requirement that everything be in the same state file, but honestly in situations like yours where there are multiple “phases” to deal with my instinct would be to write a separate configuration with a separate state for each one, because then it’s easier to orchestrate the steps knowing that each step is isolated from the others, and we can use data sources to pass data between the steps… we can just use normal
terraform apply for each one, rather than developing a non-standard workflow.
I’d also like to note that Lambda isn’t really an ideal environment for Terraform to run in because if Terraform is interrupted before it runs to completion that could result in an inconsistent state: Terraform current commits a snapshot of the current state to the backend only at completion or if it’s explicitly interrupted with
SIGINT. It’s been a while since I tried running Terraform in Lambda so perhaps things have changed in the meantime, but in my earlier experiments I found that Lambda could potentially just issue Terraform a
SIGKILL with no prior signals, giving it no opportunity to commit a snapshot of the in-memory state.