awslambda: create_function() should fail on duplicate (#6626)

This commit is contained in:
Scott Bailey 2023-08-15 17:24:01 -04:00 committed by GitHub
parent 824f22a794
commit b71723b99a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 15 deletions

View File

@ -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)

View File

@ -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])

View File

@ -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

View File

@ -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,