aws-samples / aws-lambda-extensions

A collection of sample extensions to help you get started with AWS Lambda Extensions
MIT No Attribution
446 stars 146 forks source link

Custom Extension Name #15

Closed miztiik closed 3 years ago

miztiik commented 3 years ago

Is this variable customizable?

LAMBDA_AGENT_NAME_HEADER_KEY = "Lambda-Extension-Name"

https://github.com/aws-samples/aws-lambda-extensions/blob/main/s3-logs-extension-demo-zip-archive/extensionssrc/extensions/logs_api_http_extension/extensions_api_client.py#L11

or

https://github.com/aws-samples/aws-lambda-extensions/blob/main/python-example-logs-api-extension/extensions/logs_api_http_extension/extensions_api_client.py#L12

When i change this value for example to this, LAMBDA_AGENT_NAME_HEADER_KEY = "Custom-Lambda-Extension-Name" I am getting "Failed to register extensionsAPI" errors

START RequestId: 9f34a0b2-dd6c-4894-9c96-0f58351354f5 Version: $LATEST
extension.extensions_api_client: Registering Extension at ExtensionsAPI address: http://127.0.0.1:9001/2020-01-01/extension
Traceback (most recent call last):
  File "/opt/extensions/logs_api_http_extension/extensions_api_client.py", line 32, in register
    resp = urllib.request.urlopen(req)
  File "/var/lang/lib/python3.8/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/var/lang/lib/python3.8/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/var/lang/lib/python3.8/urllib/request.py", line 640, in http_response
    response = self.parent.error(
  File "/var/lang/lib/python3.8/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/var/lang/lib/python3.8/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/var/lang/lib/python3.8/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/extensions/logs_api_http_extension.py", line 134, in <module>
    main()
  File "/opt/extensions/logs_api_http_extension.py", line 130, in main
    ext = LogsAPIHTTPExtension(os.path.basename(__file__), _REGISTRATION_BODY, _SUBSCRIPTION_BODY)
  File "/opt/extensions/logs_api_http_extension.py", line 57, in __init__
    self.agent_id = self.extensions_api_client.register(self.agent_name, registration_body)
  File "/opt/extensions/logs_api_http_extension/extensions_api_client.py", line 41, in register
    raise Exception(f"Failed to register to ExtensionsAPI: on {self.runtime_api_base_url}/register \
Exception: Failed to register to ExtensionsAPI: on http://127.0.0.1:9001/2020-01-01/extension/register                 with agent_unique_name:logs_api_http_extension.py                  and registration_body:{'events': ['INVOKE', 'SHUTDOWN']}
Error: HTTP Error 403: Forbidden
EXTENSION   Name: logs_api_http_extension.py    State: Started  Events: []
END RequestId: 9f34a0b2-dd6c-4894-9c96-0f58351354f5
REPORT RequestId: 9f34a0b2-dd6c-4894-9c96-0f58351354f5  Duration: 7617.86 ms    Billed Duration: 7618 ms    Memory Size: 128 MB Max Memory Used: 26 MB  
RequestId: 9f34a0b2-dd6c-4894-9c96-0f58351354f5 Error: exit status 1
Extension.Crash
stawecki commented 3 years ago

Lambda-Extension-Name is simply the header key under which you provide the name of your extension. If you look at the registration call to the Extensions API, the header value for that key is your extension's name kept in variable agent_unique_name:

https://github.com/aws-samples/aws-lambda-extensions/blob/2e5c35cc44e1a4755a60c25f8448412bfb479ce0/s3-logs-extension-demo-zip-archive/extensionssrc/extensions/logs_api_http_extension/extensions_api_client.py#L26-L28

The agent_unique_name, is actually set through a contructor in main() using os.path.basename(__file__):

https://github.com/aws-samples/aws-lambda-extensions/blob/2e5c35cc44e1a4755a60c25f8448412bfb479ce0/s3-logs-extension-demo-zip-archive/extensionssrc/extensions/logs_api_http_extension.py#L130

That's because the extension name has to match the filename. To summarise:

  1. You can't change the header key as it's part of the Extensions API. (Probably why it's returning 403, when you do it)
  2. You can change its value and change the extension's name, but remember it has to match the filename of your extension's main executable.
miztiik commented 3 years ago

Thanks for clarification. I was able to get it work after modifying just the main file name and not updating the header key.