Skip to content

Instantly share code, notes, and snippets.

@kjlape
Created February 8, 2018 23:40
Show Gist options
  • Select an option

  • Save kjlape/807bc8331cf52003346f011421d8b81c to your computer and use it in GitHub Desktop.

Select an option

Save kjlape/807bc8331cf52003346f011421d8b81c to your computer and use it in GitHub Desktop.
instance_id = Net::HTTP.get URI('http://169.254.169.254/latest/meta-data/instance-id')
instance = ec2.instance(instance_id)
name = instance.tags.find { |tag| tag.key == 'tag:Name' }.value
instances = ec2.describe_instances(filters:[
{ name: 'instance-state-name', values: ['running'] },
{ name: 'tag:Environment', values: [Rails.env] },
{ name: 'tag:Name', values: [ENV['EC2_NAME_TAG']] },
])
instance_count = instances.reservations.flat_map { |reservation| reservation.instances }.length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment