From bb64258a8f2fd994d40405f76440c47f12012ef9 Mon Sep 17 00:00:00 2001 From: Mike Grima Date: Tue, 4 Feb 2020 17:06:55 -0800 Subject: [PATCH] Fixed issue with Lambda invoke via ARN - Fixed an issue where Lambda invokes via an ARN was hitting real AWS. --- moto/awslambda/responses.py | 3 ++- moto/awslambda/urls.py | 1 + tests/test_awslambda/test_lambda.py | 37 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/moto/awslambda/responses.py b/moto/awslambda/responses.py index e1713ce52..bac670b8e 100644 --- a/moto/awslambda/responses.py +++ b/moto/awslambda/responses.py @@ -176,7 +176,8 @@ class LambdaResponse(BaseResponse): def _invoke(self, request, full_url): response_headers = {} - function_name = self.path.rsplit("/", 2)[-2] + # URL Decode in case it's a ARN: + function_name = unquote(self.path.rsplit("/", 2)[-2]) qualifier = self._get_param("qualifier") response_header, payload = self.lambda_backend.invoke( diff --git a/moto/awslambda/urls.py b/moto/awslambda/urls.py index 6c9b736a6..c25e58dba 100644 --- a/moto/awslambda/urls.py +++ b/moto/awslambda/urls.py @@ -12,6 +12,7 @@ url_paths = { r"{0}/(?P[^/]+)/event-source-mappings/?$": response.event_source_mappings, r"{0}/(?P[^/]+)/event-source-mappings/(?P[\w_-]+)/?$": response.event_source_mapping, r"{0}/(?P[^/]+)/functions/(?P[\w_-]+)/invocations/?$": response.invoke, + r"{0}/(?P[^/]+)/functions/(?P.+)/invocations/?$": response.invoke, r"{0}/(?P[^/]+)/functions/(?P[\w_-]+)/invoke-async/?$": response.invoke_async, r"{0}/(?P[^/]+)/tags/(?P.+)": response.tag, r"{0}/(?P[^/]+)/functions/(?P[\w_-]+)/policy/(?P[\w_-]+)$": response.policy, diff --git a/tests/test_awslambda/test_lambda.py b/tests/test_awslambda/test_lambda.py index dfd6431e7..4db13d220 100644 --- a/tests/test_awslambda/test_lambda.py +++ b/tests/test_awslambda/test_lambda.py @@ -124,6 +124,43 @@ def test_invoke_requestresponse_function(): json.loads(payload).should.equal(in_data) +@mock_lambda +def test_invoke_requestresponse_function_with_arn(): + from moto.awslambda.models import ACCOUNT_ID + + conn = boto3.client("lambda", "us-west-2") + conn.create_function( + FunctionName="testFunction", + Runtime="python2.7", + Role=get_role_name(), + Handler="lambda_function.lambda_handler", + Code={"ZipFile": get_test_zip_file1()}, + Description="test lambda function", + Timeout=3, + MemorySize=128, + Publish=True, + ) + + in_data = {"msg": "So long and thanks for all the fish"} + success_result = conn.invoke( + FunctionName="arn:aws:lambda:us-west-2:{}:function:testFunction".format( + ACCOUNT_ID + ), + InvocationType="RequestResponse", + Payload=json.dumps(in_data), + ) + + success_result["StatusCode"].should.equal(202) + result_obj = json.loads( + base64.b64decode(success_result["LogResult"]).decode("utf-8") + ) + + result_obj.should.equal(in_data) + + payload = success_result["Payload"].read().decode("utf-8") + json.loads(payload).should.equal(in_data) + + @mock_lambda def test_invoke_event_function(): conn = boto3.client("lambda", "us-west-2")