Skip to content

Instantly share code, notes, and snippets.

@philchristensen
Created April 8, 2015 21:11
Show Gist options
  • Save philchristensen/daec5609bf56f85d5cb1 to your computer and use it in GitHub Desktop.
Save philchristensen/daec5609bf56f85d5cb1 to your computer and use it in GitHub Desktop.
Custom puppet function for finding sibling autoscaling group instances in other AZs
require 'aws-sdk'
module Puppet::Parser::Functions
newfunction(:xaz_host, :type => :rvalue) do |args|
current_az = lookupvar('ec2_placement_availability_zone')
asg_prefix, domain = args
Aws.config[:credentials] = Aws::Credentials.new(
"AKIAJSUKG23H6XC3FE3A",
"byQczPLjJwDMCcYjtnKgPS/7NfM1D0XpuEU9scmt"
)
Aws.config[:region] = current_az[0,current_az.length-1]
as_client = Aws::AutoScaling::Client.new
response = as_client.describe_auto_scaling_instances
xaz_instances = response.auto_scaling_instances.select do |i|
i.health_status.downcase != "unhealthy" &&
i.availability_zone != current_az &&
i.auto_scaling_group_name.start_with?(asg_prefix)
end
ec2_client = Aws::EC2::Client.new
xaz_instance_id = xaz_instances.sample.instance_id
response = ec2_client.describe_tags(
:filters => [{
:name => 'resource-id',
:values => [xaz_instance_id]
}]
)
name_tag = response.tags.select { |t| t.key == 'Name' }
name_tag[0].value + '.' + domain
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment