diff --git a/moto/awslambda/exceptions.py b/moto/awslambda/exceptions.py index d759060c5..19174d401 100644 --- a/moto/awslambda/exceptions.py +++ b/moto/awslambda/exceptions.py @@ -14,6 +14,14 @@ class CrossAccountNotAllowed(LambdaClientError): ) +class FunctionAlreadyExists(LambdaClientError): + code = 409 + + def __init__(self, function_name: str) -> None: + message = f"Function already exist: {function_name}" + super().__init__("ResourceConflictException", message) + + class InvalidParameterValueException(LambdaClientError): def __init__(self, message: str): super().__init__("InvalidParameterValueException", message) diff --git a/moto/awslambda/responses.py b/moto/awslambda/responses.py index 2135a349e..b7349323f 100644 --- a/moto/awslambda/responses.py +++ b/moto/awslambda/responses.py @@ -6,6 +6,7 @@ from urllib.parse import unquote from moto.core.utils import path_url from moto.utilities.aws_headers import amz_crc32, amzn_request_id from moto.core.responses import BaseResponse, TYPE_RESPONSE +from .exceptions import FunctionAlreadyExists, UnknownFunctionException from .models import lambda_backends, LambdaBackend @@ -316,9 +317,14 @@ class LambdaResponse(BaseResponse): return 200, {}, json.dumps(result) def _create_function(self) -> TYPE_RESPONSE: - fn = self.backend.create_function(self.json_body) - config = fn.get_configuration(on_create=True) - return 201, {}, json.dumps(config) + function_name = self.json_body["FunctionName"].rsplit(":", 1)[-1] + try: + self.backend.get_function(function_name, None) + except UnknownFunctionException: + fn = self.backend.create_function(self.json_body) + config = fn.get_configuration(on_create=True) + return 201, {}, json.dumps(config) + raise FunctionAlreadyExists(function_name) def _create_function_url_config(self) -> TYPE_RESPONSE: function_name = unquote(self.path.split("/")[-2]) diff --git a/tests/test_awslambda/test_lambda.py b/tests/test_awslambda/test_lambda.py index 474c7469f..5a041343e 100644 --- a/tests/test_awslambda/test_lambda.py +++ b/tests/test_awslambda/test_lambda.py @@ -1236,19 +1236,20 @@ def test_create_function_with_already_exists(): Publish=True, ) - response = conn.create_function( - FunctionName=function_name, - Runtime="python2.7", - Role=get_role_name(), - Handler="lambda_function.lambda_handler", - Code={"S3Bucket": bucket_name, "S3Key": "test.zip"}, - Description="test lambda function", - Timeout=3, - MemorySize=128, - Publish=True, - ) + with pytest.raises(ClientError) as exc: + conn.create_function( + FunctionName=function_name, + Runtime="python2.7", + Role=get_role_name(), + Handler="lambda_function.lambda_handler", + Code={"S3Bucket": bucket_name, "S3Key": "test.zip"}, + Description="test lambda function", + Timeout=3, + MemorySize=128, + Publish=True, + ) - assert response["FunctionName"] == function_name + assert exc.value.response["Error"]["Code"] == "ResourceConflictException" @mock_lambda diff --git a/tests/test_awslambda/test_lambda_layers.py b/tests/test_awslambda/test_lambda_layers.py index ed04b9c62..609149089 100644 --- a/tests/test_awslambda/test_lambda_layers.py +++ b/tests/test_awslambda/test_lambda_layers.py @@ -115,6 +115,7 @@ def test_get_lambda_layers(): assert result["LayerVersions"] == [] # Test create function with non existant layer version + function_name = str(uuid4())[0:6] # Must be different than above with pytest.raises(ClientError) as exc: conn.create_function( FunctionName=function_name,