diff --git a/moto/awslambda/responses.py b/moto/awslambda/responses.py index d145f4760..f68f44efa 100644 --- a/moto/awslambda/responses.py +++ b/moto/awslambda/responses.py @@ -33,13 +33,20 @@ class LambdaResponse(BaseResponse): else: raise ValueError("Cannot handle request") + def invoke_async(self, request, full_url, headers): + self.setup_class(request, full_url, headers) + if request.method == 'POST': + return self._invoke_async(request, full_url) + else: + raise ValueError("Cannot handle request") + def _invoke(self, request, full_url): response_headers = {} lambda_backend = self.get_lambda_backend(full_url) path = request.path if hasattr(request, 'path') else request.path_url function_name = path.split('/')[-2] - + if lambda_backend.has_function(function_name): fn = lambda_backend.get_function(function_name) payload = fn.invoke(self.body, self.headers, response_headers) @@ -48,6 +55,20 @@ class LambdaResponse(BaseResponse): else: return 404, response_headers, "{}" + def _invoke_async(self, request, full_url): + response_headers = {} + lambda_backend = self.get_lambda_backend(full_url) + + path = request.path if hasattr(request, 'path') else request.path_url + function_name = path.split('/')[-3] + if lambda_backend.has_function(function_name): + fn = lambda_backend.get_function(function_name) + fn.invoke(self.body, self.headers, response_headers) + response_headers['Content-Length'] = str(0) + return 202, response_headers, "" + else: + return 404, response_headers, "{}" + def _list_functions(self, request, full_url, headers): lambda_backend = self.get_lambda_backend(full_url) return 200, {}, json.dumps({ diff --git a/moto/awslambda/urls.py b/moto/awslambda/urls.py index c63135766..d6045d229 100644 --- a/moto/awslambda/urls.py +++ b/moto/awslambda/urls.py @@ -11,4 +11,5 @@ url_paths = { '{0}/(?P[^/]+)/functions/?$': response.root, '{0}/(?P[^/]+)/functions/(?P[\w_-]+)/?$': response.function, '{0}/(?P[^/]+)/functions/(?P[\w_-]+)/invocations/?$': response.invoke, + '{0}/(?P[^/]+)/functions/(?P[\w_-]+)/invoke-async/?$': response.invoke_async, } diff --git a/tests/test_awslambda/test_lambda.py b/tests/test_awslambda/test_lambda.py index 007516f56..9e99e1609 100644 --- a/tests/test_awslambda/test_lambda.py +++ b/tests/test_awslambda/test_lambda.py @@ -469,3 +469,28 @@ def test_invoke_lambda_error(): assert 'FunctionError' in result assert result['FunctionError'] == 'Handled' + +@mock_lambda +def test_invoke_async_function(): + conn = boto3.client('lambda', 'us-west-2') + conn.create_function( + FunctionName='testFunction', + Runtime='python2.7', + Role='test-iam-role', + Handler='lambda_function.handler', + Code={ + 'ZipFile': get_test_zip_file1(), + }, + Description='test lambda function', + Timeout=3, + MemorySize=128, + Publish=True, + ) + + success_result = conn.invoke_async( + FunctionName='testFunction', + InvokeArgs=json.dumps({ 'test': 'event' }) + ) + + print(success_result) + success_result['Status'].should.equal(202)