Bash script to create a replica set

I am a junior devOps, and i am building (for training) a high availability and fail over infrastructure. So having 3 app instances running a app homepage with autoscaling and load balancer, and as a database a mongodb in replicaset accross 3 availability zones in aws.

The point of this training is to spin up automatically all the infrastructure by running terrafform.

Everything else work and i am missing just the mongodb part.

Thats why i need this script, so when my instance is spinned up, it will have all the configuration in place to work,automating everything without having to run any command once the infrastructure is up and running.

i tried to delay the script by 2minutes before to execute it, but still nothing. if after 3 minute i ssh in the instance and run rs.status() i get:

> rs.status()
{
	"info" : "run rs.initiate(...) if not yet done for the set",
	"ok" : 0,
	"errmsg" : "no replset config has been received",
	"code" : 94
}