How to enumerate running ec2 instances and load th

2019-07-17 08:22发布

I'm using aws-sdk gem, I can't figure how to list all the running ec2 instances and load them to a database.

I need an approach on how to do it.

3条回答
淡お忘
2楼-- · 2019-07-17 08:30

Install the AWS ClI and run the following to get a list of running instance ids:

aws ec2 describe-instances --filter "Name=instance-state-name,Values=running" \
| grep InstanceId | awk '{print $2}' | sed 's/^\"//g' | sed 's/\",$//g'
查看更多
可以哭但决不认输i
3楼-- · 2019-07-17 08:45
require 'aws-sdk-v1'
ec2 = AWS::EC2.new(
    access_key_id: 'YOUR_ACCESS_KEY_ID',
    secret_access_key: 'YOUR_SECRET_ACCESS_KEY',
    region: 'YOUR_EC2_REGION'
)

ec2.instances

http://docs.amazonwebservices.com/AWSRubySDK/latest/frames.html

查看更多
劳资没心,怎么记你
4楼-- · 2019-07-17 08:51

The answer above will return all instances, not just running instances. You can use a filter to get only running instances:

ec2 = AWS::EC2.new
ec2.instances.filter('instance-state-name', 'running')
查看更多
登录 后发表回答