How can I generate ssh key in an instance

I’d like to generate ssh key in my first instance and add it to authorized_keys on all other instances so that I can passwordless ssh from it.

I can’t find a reference or guide on how to do so. I’m assuming there is a neat terraform method for doing so other than using shell commands etc. If so, can anyone direct me to something or provide a snippet?

Here is what I’m currently doing. A key is generated on the first instance during a previous script execution. I then do as follows, which seems messy to me.

resource "null_resource" "distribute_server_ssh_key" {
        count = var.instance_count
        provisioner "local-exec" {
             command = <<EOF
                  <get ssh key stuff from> [0]
        EOF
        }
        provisioner "local-exec" {
             command = <<EOF
                  <distribute ssh key stuff> [count.index]
        EOF
        }
 }