Reputation: 757
So I'm writing a rails app that will allow a user to create an instance on ec2. So far I've been able to get things up and running quite nicely, with one exception: I can't seem to give the instance a name. It becomes confusing when I look in the management console and see a bunch of no-name instances. I've looked at the documentation for creating the instances and none of the available options seem to fit the bill, but maybe I'm missing something.
tl;dr How can I give an instance a name when I create it with the AWS SDK for ruby?
EDIT:
It was suggested that I use ec2.client.create_tags
like this:
ec2.client.create_tags(:resources => [@instance.instance_id, :tags => [
{ :key => 'Name', :value => @instance.instance_name },
]])
But i get this error: expected string value for member 2 of option resources
Both @instance.instance_name
and @instance.instance_id
are strings so I don't know what's going on.
EDIT2: the error above was due to a misplaced bracket.
Upvotes: 2
Views: 2086
Reputation: 84132
After you've created the instance you need to add a tag to it. You can add up to 10 arbitrary key value pairs and if the key is "Name" then the aws console will display that value by default. You can also configure the console to display any other tags you find useful.
Something like this should do the trick, assuming your instance had the is "i-123abc".
ec2 = AWS::EC2.new
ec2.client.create_tags(:resources => ["i-123abc"], :tags => [
{ :key => 'Name', :value => 'an instance' }
])
Upvotes: 4
Reputation: 1451
I would try the following...
1. (setup the AWS)
ec2 = AWS::EC2.new(
:access_key_id => 'YOUR_ACCESS_KEY_ID',
:secret_access_key => 'YOUR_SECRET_ACCESS_KEY')
2.(run an Instance)
ec2.instances.create(:image_id => "ami-8c1fece5")
3. (check if instance is running or not)
i = ec2.instances["i-12345678"]
i.exists?
Done.
Upvotes: 0