ruby on rails - Detecting Elastic Beanstalk Environment? -
i trying tag error messages elastic beanstalk environment. there way programmatically determine environment in ec2 instance inside elastic beanstalk?
i using ruby on rails ruby way nice, can port language.
i have rails project , doing inside in order determine environment our ec2 instance part of.
below code doing more 1 check, file check makes sure running code on ec2 instance. second set of url calls grabbing ec2 instance id along aws region being used. uses previous 2 pieces of data find environment-name using aws-sdk. it's little ugly gets job done.
require 'net/http' require 'aws-sdk' uuid = file.readlines('/sys/hypervisor/uuid', 'r') if uuid str = uuid.first.slice(0,3) if str == 'ec2' metadata_endpoint = 'http://169.254.169.254/latest/meta-data/' dynamic_endpoint = 'http://169.254.169.254/latest/dynamic/' instance_id = net::http.get( uri.parse( metadata_endpoint + 'instance-id' ) ) document = net::http.get( uri.parse( dynamic_endpoint + 'instance-identity/document') ) parsed_document = json.parse(document) region = parsed_document['region'] ec2 = aws::ec2::client.new(region: region) ec2.describe_instances({instance_ids:[instance_id]}).reservation_set[0].instances_set[0].tag_set.each |tag| if tag.key == 'elasticbeanstalk:environment-name' @env_name = tag.value end end end end
Comments
Post a Comment