-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(LocalLambdaService): Initial implementation to Invoke Local Lamb…
…das through a Local Lambda Service (#508)
- Loading branch information
Showing
15 changed files
with
627 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
90 changes: 90 additions & 0 deletions
90
samcli/local/lambda_service/local_lambda_invoke_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
"""Local Lambda Service that only invokes a function""" | ||
|
||
import logging | ||
import io | ||
|
||
from flask import Flask, request | ||
|
||
|
||
from samcli.local.services.base_local_service import BaseLocalService, LambdaOutputParser | ||
from samcli.local.lambdafn.exceptions import FunctionNotFound | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class LocalLambdaInvokeService(BaseLocalService): | ||
|
||
def __init__(self, lambda_runner, port, host, stderr=None): | ||
""" | ||
Creates a Local Lambda Service that will only response to invoking a function | ||
Parameters | ||
---------- | ||
lambda_runner samcli.commands.local.lib.local_lambda.LocalLambdaRunner | ||
The Lambda runner class capable of invoking the function | ||
port int | ||
Optional. port for the service to start listening on | ||
host str | ||
Optional. host to start the service on | ||
stderr io.BaseIO | ||
Optional stream where the stderr from Docker container should be written to | ||
""" | ||
super(LocalLambdaInvokeService, self).__init__(lambda_runner.is_debugging(), port=port, host=host) | ||
self.lambda_runner = lambda_runner | ||
self.stderr = stderr | ||
|
||
def create(self): | ||
""" | ||
Creates a Flask Application that can be started. | ||
""" | ||
self._app = Flask(__name__) | ||
|
||
path = '/2015-03-31/functions/<function_name>/invocations' | ||
self._app.add_url_rule(path, | ||
endpoint=path, | ||
view_func=self._invoke_request_handler, | ||
methods=['POST'], | ||
provide_automatic_options=False) | ||
|
||
self._construct_error_handling() | ||
|
||
def _construct_error_handling(self): | ||
""" | ||
Updates the Flask app with Error Handlers for different Error Codes | ||
""" | ||
pass | ||
|
||
def _invoke_request_handler(self, function_name): | ||
""" | ||
Request Handler for the Local Lambda Invoke path. This method is responsible for understanding the incoming | ||
request and invoking the Local Lambda Function | ||
Parameters | ||
---------- | ||
function_name str | ||
Name of the function to invoke | ||
Returns | ||
------- | ||
A Flask Response response object as if it was returned from Lambda | ||
""" | ||
flask_request = request | ||
request_data = flask_request.get_data().decode('utf-8') | ||
|
||
stdout_stream = io.BytesIO() | ||
|
||
try: | ||
self.lambda_runner.invoke(function_name, request_data, stdout=stdout_stream, stderr=self.stderr) | ||
except FunctionNotFound: | ||
# TODO Change this | ||
raise Exception('Change this later') | ||
|
||
lambda_response, lambda_logs = LambdaOutputParser.get_lambda_output(stdout_stream) | ||
|
||
if self.stderr and lambda_logs: | ||
# Write the logs to stderr if available. | ||
self.stderr.write(lambda_logs) | ||
|
||
return self._service_response(lambda_response, {'Content-Type': 'application/json'}, 200) |
Empty file.
Oops, something went wrong.