简体   繁体   English

无法访问使用ssh由ruby aws-sdk lib创建的ec2实例?

[英]can't access ec2 instance which is created by ruby aws-sdk lib using ssh?

i'm using ruby aws-sdk to launch instance in amazone EC2 but i have problem with access the created instances using ssh, here what i did 我正在使用ruby aws-sdk在amazone EC2中启动实例,但是我在使用ssh访问创建的实例时遇到问题,在这里我做了什么

ec2 = AWS::EC2.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY, :ec2_endpoint =>  "ec2.sa-east-1.amazonaws.com")
securitygrp = ec2.security_groups.create('mysecgrp')
securitygrp.authorize_ingress(:tcp, 22)
ec2.instances.create(:image_id => "ami-082df215")

but i can't access the instance using ssh root@ip_address, i got connection timed out , is there something i missing, please anyone help me? 但是我无法使用ssh root @ ip_address访问该实例,我的connection timed out ,我缺少什么,请有人帮我吗?

When you launch an instance, you may optionally provide the security group. 启动实例时,可以选择提供安全组。 If you omit the :security_groups options, then it will default to the "default" security group (you can not change the security group of an instance after launch). 如果省略:security_groups选项,则它将默认为“默认”安全组(启动后无法更改实例的安全组)。 In your example you should be specifying the security group to the one you just created. 在您的示例中,您应该将安全组指定为刚创建的安全组。

Another thing to consider is specifying the key pair. 要考虑的另一件事是指定密钥对。 It will default to one if you don't set it, but you will likely need this to log into a public ami. 如果未设置,它将默认为一个,但是您可能需要使用它来登录公共ami。

Here is an example I just ran. 这是我刚运行的示例。 The AMI I use is the amazon linux ami. 我使用的AMI是Amazon Linux AMI。

AWS.config(:access_key_id => '...', :secret_access_key => '...')
ec2 = AWS::EC2.new(:ec2_endpoint => "ec2.sa-east-1.amazonaws.com")

# create a security group and authorize ssh
sg = ec2.security_groups.create('my-security-group')
sg.authorize_ingress(:tcp, 22)

# create a key pair and write it to disk
key_pair = ec2.key_pairs.create('my-key-pair')
File.open("#{ENV['HOME']}/.ssh/my-key-pair.pk", 'w') do |file|
  file.write key_pair.private_key
end
require 'fileutils'
FileUtils.chmod(0600, "#{ENV['HOME']}/.ssh/my-key-pair.pk")

instance = ec2.instances.create(
  :image_id => 'ami-3c3be421', 
  :key_name => key_pair.name,
  :security_groups => [sg])

sleep 1 while instance.status == :pending

puts instance.ip_address
#=> '1.2.3.4'

Now you should be able to ssh into the instance (sometimes there is a ~ 30 second delay from when the instances status is :available and when it responds to ssh). 现在您应该可以使用ssh进入实例了(从实例状态为:available以及它对ssh作出响应时,有时会有30秒的延迟)。

# some amis require you to login as root, others as ec2-user
$ ssh -i ~/.ssh/my-key-pair.pk ec2-user@1.2.3.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM