keijack / python-eureka-client

A eureka client written in python. Support registering your python component to Eureka Server, as well as calling remote services by pulling the the Eureka registry.
MIT License
184 stars 43 forks source link

Load Amazon EC2 metadata automatically #33

Closed keijack closed 2 years ago

keijack commented 3 years ago

I want to load Amazon EC2 metadata when your component register to eureka server, I can find the method to do it, but I have no Amazon EC2 to test it. I will create a branch to write this feature, but I need someone help me to test it.

dinuta commented 3 years ago

I can help. Describe the steps needed.

ciphero commented 3 years ago

Getting errors loading AWS metadata with data_center_name="Amazon" set.

-[aws_info_loader]-[line:63] -ERROR: error when loading metadata from aws network/interfaces/macs/('02:49:7c:6a:c8:15', <http.client.HTTPResponse object at 0x7f6b1bce9610>)/vpc-id
-[aws_info_loader]-[line:63] -ERROR: error when loading metadata from aws public-hostname
-[aws_info_loader]-[line:63] -ERROR: error when loading metadata from aws public-ipv4
-[aws_info_loader]-[line:74] -ERROR: error when loading dynamic instance identity document from aws
keijack commented 3 years ago

@Ciphero Thanks, for submitting, I have update some code, please upgrade to 0.9.8 and try again.

ciphero commented 3 years ago

Still getting some errors.. Here's the full log:

$ python3 eurekaclient.py
[2021-04-27 10:46:15]-[aws_info_loader]-[line:63] -ERROR: error when loading metadata from aws public-hostname
Traceback (most recent call last):
  File "/home/ec2-user/.local/lib/python3.7/site-packages/py_eureka_client/__aws_info_loader.py", line 61, in get_ec2_metadata
    return http_client.load(f"{_AWS_METADATA_SERVICE_URL}meta-data/{meta_path}")[0]
  File "/home/ec2-user/.local/lib/python3.7/site-packages/py_eureka_client/http_client.py", line 146, in load
    res = http_cli.urlopen()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/py_eureka_client/http_client.py", line 113, in urlopen
    cadefault=self.cadefault, context=self.context)
  File "/usr/lib64/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib64/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/usr/lib64/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python3.7/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/usr/lib64/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/lib64/python3.7/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
[2021-04-27 10:46:15]-[aws_info_loader]-[line:63] -ERROR: error when loading metadata from aws public-ipv4
Traceback (most recent call last):
  File "/home/ec2-user/.local/lib/python3.7/site-packages/py_eureka_client/__aws_info_loader.py", line 61, in get_ec2_metadata
    return http_client.load(f"{_AWS_METADATA_SERVICE_URL}meta-data/{meta_path}")[0]
  File "/home/ec2-user/.local/lib/python3.7/site-packages/py_eureka_client/http_client.py", line 146, in load
    res = http_cli.urlopen()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/py_eureka_client/http_client.py", line 113, in urlopen
    cadefault=self.cadefault, context=self.context)
  File "/usr/lib64/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib64/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/usr/lib64/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python3.7/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/usr/lib64/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/lib64/python3.7/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
keijack commented 3 years ago

@Ciphero This happens when trying to load public-hostname from http://169.254.169.254/latest/meta-data/public-hostname. It seems that public-hostname is not available in your metadata service configuration. That may be means your EC2 have no public hostname. However, in version 0.9.9 the error logs when loading public-hostname/public-ip like you post are ignored.