Reputation: 13
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
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?
Upvotes: 1
Views: 1185
Reputation: 6528
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). 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.
Here is an example I just ran. The AMI I use is the 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).
# some amis require you to login as root, others as ec2-user
$ ssh -i ~/.ssh/my-key-pair.pk [email protected]
Upvotes: 1