ansible-collections / amazon.aws

Ansible Collection for Amazon AWS
GNU General Public License v3.0
308 stars 339 forks source link

Improve amazon.aws.ec2_metadata_facts module performance #2216

Open tzfromaz opened 2 months ago

tzfromaz commented 2 months ago

Summary

The amazon.aws.ec2_metadata_facts module performance can be improved to eliminate a 20s+ delay especially focusing on better fact gathering.

For example:

import logging
logger = logging.getLogger(__name__)
logging.basicConfig(
    format='%(asctime)s %(levelname)-8s %(message)s',
    level=logging.WARNING,
    datefmt='%Y-%m-%d %H:%M:%S')
def get_facts_from_ec2_metadata():
    ec2_metadata_facts = {}
    logger.info("faking metadata using ec2_metadata module")
    from ec2_metadata import ec2_metadata
    from ec2_metadata import NetworkInterface
    import inspect
    import requests
    for attribute_name in dir(ec2_metadata):
        if not attribute_name.startswith('_'):
            try:
                attribute = getattr(ec2_metadata, attribute_name)
                logger.info(f"{attribute_name}:  {type(getattr(ec2_metadata, attribute_name))}  {getattr(ec2_metadata, attribute_name)}")
                if (not inspect.ismethod(attribute)) and (attribute_name != "network_interfaces"):
                    ec2_metadata_facts[attribute_name] = attribute
            except requests.exceptions.HTTPError as ex:
                logger.info(f"Failed to get {attribute_name}: {ex}")
    return ec2_metadata_facts
...
    #ec2_metadata_facts = Ec2Metadata(module).run()

    ec2_metadata_facts = get_facts_from_ec2_metadata()
...

Issue Type

Feature Idea

Component Name

amazon.aws.ec2_metadata_facts

Additional Information

This improves performance for the module overall and speeds up any runbooks using the module.

Code of Conduct

alinabuzachis commented 2 months ago

@tzfromaz Thank you for reporting this issue. Would you be willing to take the time to work on a fix and open a pull request?