以编程方式将现有 SSH 密钥导入 AWS EC2

Import existing SSH key to AWS EC2 programmatically

Importing Your Own Key Pair to Amazon EC2 的官方文档缺少有关如何以编程方式生成和导入密钥对的详细信息。

如何做到最好?

创建密钥对(最多 2048 位):

ssh-keygen -t rsa -b 2048 -C "ec2@aws" -N "" -f ec2_ssh
chmod 400 ec2_ssh*

将 public 密钥导入 EC2:

aws ec2 --region=eu-west-1 import-key-pair --key-name ec2_ssh --public-key-material "file://ec2_ssh.pub"

此命令未提及 file://... 功能,但它是 described here

来源: