How to fail aws_instance creation if user_data fails to run to completion

Is it possible to fail an aws_instance creation if script passed into user_data fails to run? e.g., with exit 1?

I have a null_resource that uses depends_on [aws_instance.myVM] to post a JSON to the instance, and I need that depends_on to fail when the user_data script fails.

Thanks!

I don’t think that’s possible; as far as AWS (and therefore Terraform) is concerned, the instance is alive once it is in the ‘running’ state, which I know from experience can be before your user_data configuration has even finished.

1 Like

Like @nhw76, I don’t know of a way to do this but you could possibly use a provisioner configured on the instance resource to run some check via SSH.

It is not the prettiest solution but should work.
If you can provide more details on what you’re trying to accomplish, maybe there are other options.