Skip to content

Instantly share code, notes, and snippets.

@DoctahPopp871
Created October 28, 2016 16:11
Show Gist options
  • Save DoctahPopp871/9b0224deaa98c9457c59d42f45b8227b to your computer and use it in GitHub Desktop.
Save DoctahPopp871/9b0224deaa98c9457c59d42f45b8227b to your computer and use it in GitHub Desktop.
#Fact : ec2_tags
#
#Purpose : To provide the ec2_tags of an instance.
#
#Resolution : Retrieve the ec2_tags for the instance by querying the machine.
#
#
Facter.add(:ec2_tags) do
confine do
begin
require 'aws-sdk-core'
true
rescue LoadError
false
end
end
confine do
Facter.value(:ec2_metadata)['iam']['info']
end
setcode do
Aws.use_bundled_cert!
instance_id = Facter.value('ec2_metadata')['instance-id']
region = Facter.value(:ec2_metadata)['placement']['availability-zone'][0..-2]
ec2 = Aws::EC2::Client.new(region: region)
instance = ec2.describe_instances(instance_ids: [instance_id])
tags = instance.reservations[0].instances[0].tags
taghash = { }
tags.each do |tag|
taghash[tag['key'].downcase] = tag['value'].downcase
end
taghash
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment