给出确切路径时,AWS Boto3“密钥对不存在”错误

Daw*_*y33 3 python amazon-ec2 amazon-web-services boto3

我正在运行以下脚本:

from __future__ import print_function

import paramiko
import boto3


#print('Loading function')

paramiko.util.log_to_file("/tmp/Dawny.log")

# List of EC2 variables
region = 'us-east-1'
image = 'ami-<>'
keyname = '<>.pem'

ec2 = boto3.resource('ec2')


instances = ec2.create_instances(ImageId=image, MinCount=1, MaxCount=1, InstanceType = 't2.micro', KeyName=keyname)

instance = instances[0]
instance.wait_until_running()

instance.load()

print(instance.public_dns_name)



def lambda_handler(event, context):
    instances = ec2.create_instances(ImageId=image, MinCount=1, MaxCount=1, InstanceType = 't2.micro', KeyName=keyname)

    instance = instances[0]
    instance.wait_until_running()

    instance.load()

    print(instance.public_dns_name)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我收到此错误

botocore.exceptions.ClientError: An error occurred (InvalidKeyPair.NotFound) when calling the RunInstances operation: The key pair '<>.pem' does not exist
Run Code Online (Sandbox Code Playgroud)

即使我将完整路径添加到密钥对,boto3 也会给我同样的错误。另外,我也试过这个:https : //stackoverflow.com/a/34410564/4993513

还是不行。

fra*_*ijo 5

KeyName在参数create_instances()指的是创建一个在AWS时所赋予的密钥对的名称。

名称通常是“ KeyName.pem”。传递不带.pem扩展名的字符串。中的KeyName参数create_instances()只需要密钥对的名称,而不是实际的密钥文件。

例如:
如果密钥文件是myinstance.pem,则myinstance除非密钥文件已被重命名,否则KeyName 将是。您将能够从控制台查看您拥有的所有密钥对(也可以与 cli 和 api 一起列出)。

  • 只是为了进一步解释......该命令不需要访问您在磁盘上的密钥对。相反,它将密钥对的名称发送到 EC2,然后 EC2 将访问已存储在 AWS 上的命名密钥对,并将在启动实例时使用。 (3认同)