'aws metadata v2 usage in ruby chef

Am using the following snippet to get instance_id in chef ruby sdk which is using AWS metadata version 1 to retrieve the aws instance id. As AWS is recommended to use metadata v2 which is using http tokens to retrieve the data, am struggling to get the alternate replacement for the below code to be refactored to use metadata v2.

def instance_id 
    metadata_endpoint = node['aws_core']['aws_metadata_endpoint'] 
    Net::HTTP.get(URI.parse(metadata_endpoint + 'instance_id')) 
end 

Could someone help on how the aws metadata v2 using http_tokens can be used.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source