MongoDB switches from primary state to secondary during script

You have one data node and an arbiter, that does not make a valid replica set. You may as well remove your arbiter as it provides nothing beneficial to your deployment. A replica set should be at least 3 data nodes. If you really, really, really, … cannot run 3 data nodes then 2 data nodes and an arbiter.

You will need to look at the logs from the data node for clues as well as the system logs and metrics(cpu, ram, swap, disk io). This has the signature of a mongod under too much load, becoming unresponsive and triggering an election. That lack of performance could be dues to lack or resources or configuration issues.

Did the container get restarted by the orchestrator(k8s), are there resource constraints on the container?

Even though it is running in a container still look to the Production Notes and Operations Checklist. Transparent Huge Pages, Filesystem Type, swappiness can have impact on your deployment.

There are tips specifically for AWS that may or may not apply.