【问题标题】:unable ssh into launched ec2 instance when launched through below script, but able to ssh when launched from AWS page通过以下脚本启动时无法 ssh 进入已启动的 ec2 实例,但从 AWS 页面启动时能够 ssh
【发布时间】:2020-05-18 17:33:52
【问题描述】:

我最近开始学习 AWS EC2 服务,昨天我刚刚启动了一个 EC2 实例,描述了我的启动配置(附件 Review Launch Configurations),我能够成功 ssh 进入我的 pem 文件,但我也想启动 EC2 实例使用我做的boto3 python。但我无法通过以下脚本 SSH 进入 EC2 实例。

#!/usr/bin/python

import boto3
client = boto3.client('ec2')
response = client.run_instances(
BlockDeviceMappings=[
    {
        'DeviceName' :'/dev/xvda',
        'Ebs' : {
            'DeleteOnTermination' : True,
        },
    },
    ],

ImageId= 'ami-04590e7389a6e577c',
InstanceType= 't2.micro',
KeyName= 'ec2-keypair',
MaxCount = 1,
MinCount = 1,
Monitoring={
    'Enabled' : False
},

 )

for instance in response['Instances']:
     print(instance['InstanceId'])

上面的脚本可以启动 EC2 实例,但是我无法从 ubuntu 子系统登录。

到目前为止已完成的故障排除:获取了我从 AWS 页面启动的 EC2 的详细信息,使用 client.describe_instances() 并在上面定义了 client.run_istances()

请教,为什么我通过上述脚本启动 EC2 实例时无法 ssh,而从 AWS 页面启动时我可以 ssh EC2。

非常感谢您在这方面的专业知识。

【问题讨论】:

    标签: python-3.x amazon-web-services amazon-ec2 ssh boto3


    【解决方案1】:

    您的run_instances() 未显示要附加的安全组。因此,将无法访问。

    通过管理控制台启动 Amazon EC2 实例时,会提供一个默认的“启动向导”安全组,其中包括 SSH(端口 22)入站访问,如您的问题所示。

    如果您想允许通过代码启动的实例执行此操作,则需要引用已打开所需端口的现有安全组。

    【讨论】:

    • 先生,我非常感谢您,我一直在努力解决这个问题。我已经用SecurityGroupIds=['sg-0721330370518f373'],SecurityGroups = ['cloud_test'], 更新了我的代码并且它有效,我可以使用boto3 登录启动的Ec2。谢谢阿吉安@John Rotenstein
    猜你喜欢
    • 2014-04-06
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 2019-06-26
    • 2016-11-26
    • 2016-09-15
    • 2020-08-26
    • 1970-01-01
    相关资源
    最近更新 更多