Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle docker container creation error #6922

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions samcli/commands/local/invoke/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
from samcli.commands.local.lib.exceptions import InvalidIntermediateImageError
from samcli.lib.telemetry.metric import track_command
from samcli.lib.utils.version_checker import check_newer_version
from samcli.local.docker.exceptions import ContainerNotStartableException, PortAlreadyInUse
from samcli.local.docker.exceptions import (
ContainerNotStartableException,
DockerContainerCreationFailedException,
PortAlreadyInUse,
)

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -220,7 +224,7 @@ def do_cli( # pylint: disable=R0914
PortAlreadyInUse,
) as ex:
raise UserException(str(ex), wrapped_from=ex.__class__.__name__) from ex
except DockerImagePullFailedException as ex:
except (DockerImagePullFailedException, DockerContainerCreationFailedException) as ex:
raise UserException(str(ex), wrapped_from=ex.__class__.__name__) from ex
except ContainerNotStartableException as ex:
raise UserException(str(ex), wrapped_from=ex.__class__.__name__) from ex
Expand Down
3 changes: 3 additions & 0 deletions samcli/local/apigw/local_apigw_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from samcli.local.apigw.path_converter import PathConverter
from samcli.local.apigw.route import Route
from samcli.local.apigw.service_error_responses import ServiceErrorResponses
from samcli.local.docker.exceptions import DockerContainerCreationFailedException
from samcli.local.events.api_event import (
ContextHTTP,
ContextIdentity,
Expand Down Expand Up @@ -730,6 +731,8 @@ def _request_handler(self, **kwargs):
)
except LambdaResponseParseException:
endpoint_service_error = ServiceErrorResponses.lambda_body_failure_response()
except DockerContainerCreationFailedException as ex:
endpoint_service_error = ServiceErrorResponses.container_creation_failed(ex.message)

if endpoint_service_error:
return endpoint_service_error
Expand Down
9 changes: 9 additions & 0 deletions samcli/local/apigw/service_error_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,12 @@ def route_not_found(*args):
"""
response_data = jsonify(ServiceErrorResponses._MISSING_AUTHENTICATION)
return make_response(response_data, ServiceErrorResponses.HTTP_STATUS_CODE_403)

@staticmethod
def container_creation_failed(message):
"""
Constuct a Flask Response for when container creation fails for a Lambda Function
:return: a Flask Response
"""
response_data = jsonify({"message": message})
return make_response(response_data, ServiceErrorResponses.HTTP_STATUS_CODE_501)
20 changes: 17 additions & 3 deletions samcli/local/docker/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@

import docker
import requests
from docker.errors import NotFound as DockerNetworkNotFound
from docker.errors import (
APIError as DockerAPIError,
)
from docker.errors import (
NotFound as DockerNetworkNotFound,
)

from samcli.lib.constants import DOCKER_MIN_API_VERSION
from samcli.lib.utils.retry import retry
from samcli.lib.utils.stream_writer import StreamWriter
from samcli.lib.utils.tar import extract_tarfile
from samcli.local.docker.effective_user import ROOT_USER_ID, EffectiveUser
from samcli.local.docker.exceptions import ContainerNotStartableException, PortAlreadyInUse
from samcli.local.docker.exceptions import (
ContainerNotStartableException,
DockerContainerCreationFailedException,
PortAlreadyInUse,
)
from samcli.local.docker.utils import NoFreePortsError, find_free_port, to_posix_path

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -227,7 +236,12 @@ def create(self):
if self._extra_hosts:
kwargs["extra_hosts"] = self._extra_hosts

real_container = self.docker_client.containers.create(self._image, **kwargs)
try:
real_container = self.docker_client.containers.create(self._image, **kwargs)
except DockerAPIError as ex:
raise DockerContainerCreationFailedException(
f"Container creation failed: {ex.explanation}, check template for potential issue"
)
self.id = real_container.id

self._logs_thread = None
Expand Down
6 changes: 6 additions & 0 deletions samcli/local/docker/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ class InvalidRuntimeException(UserException):
"""
Raised when an invalid runtime is specified for a Lambda Function
"""


class DockerContainerCreationFailedException(UserException):
"""
Docker Container Creation failed. It could be due to invalid template
"""
25 changes: 25 additions & 0 deletions samcli/local/lambda_service/lambda_error_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class LambdaErrorResponses:

NotImplementedException = ("NotImplemented", 501)

ContainerCreationFailed = ("ContainerCreationFailed", 501)

PathNotFoundException = ("PathNotFoundLocally", 404)

MethodNotAllowedException = ("MethodNotAllowedLocally", 405)
Expand Down Expand Up @@ -204,6 +206,29 @@ def generic_method_not_allowed(*args):
exception_tuple[1],
)

@staticmethod
def container_creation_failed(message):
"""
Creates a Container Creation Failed response
Parameters
----------
args list
List of arguments Flask passes to the method
Returns
-------
Flask.Response
A response object representing the ContainerCreationFailed Error
"""
error_type, status_code = LambdaErrorResponses.ContainerCreationFailed
return BaseLocalService.service_response(
LambdaErrorResponses._construct_error_response_body(
LambdaErrorResponses.LOCAL_SERVICE_ERROR,
message,
),
LambdaErrorResponses._construct_headers(error_type),
status_code,
)

@staticmethod
def _construct_error_response_body(error_type, error_message):
"""
Expand Down
3 changes: 3 additions & 0 deletions samcli/local/lambda_service/local_lambda_invoke_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from samcli.commands.local.lib.exceptions import UnsupportedInlineCodeError
from samcli.lib.utils.stream_writer import StreamWriter
from samcli.local.docker.exceptions import DockerContainerCreationFailedException
from samcli.local.lambdafn.exceptions import FunctionNotFound
from samcli.local.services.base_local_service import BaseLocalService, LambdaOutputParser

Expand Down Expand Up @@ -178,6 +179,8 @@ def _invoke_request_handler(self, function_name):
return LambdaErrorResponses.not_implemented_locally(
"Inline code is not supported for sam local commands. Please write your code in a separate file."
)
except DockerContainerCreationFailedException as ex:
return LambdaErrorResponses.container_creation_failed(ex.message)

lambda_response, is_lambda_user_error_response = LambdaOutputParser.get_lambda_output(
stdout_stream_string, stdout_stream_bytes
Expand Down
6 changes: 5 additions & 1 deletion samcli/local/lambdafn/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from samcli.lib.utils.packagetype import ZIP
from samcli.local.docker.container import Container
from samcli.local.docker.container_analyzer import ContainerAnalyzer
from samcli.local.docker.exceptions import ContainerFailureError
from samcli.local.docker.exceptions import ContainerFailureError, DockerContainerCreationFailedException
from samcli.local.docker.lambda_container import LambdaContainer

from ...lib.providers.provider import LayerVersion
Expand Down Expand Up @@ -116,6 +116,10 @@ def create(
self._container_manager.create(container)
return container

except DockerContainerCreationFailedException:
LOG.warning("Failed to create container for function %s", function_config.full_path)
raise

except KeyboardInterrupt:
LOG.debug("Ctrl+C was pressed. Aborting container creation")
raise
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/commands/local/invoke/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from unittest.mock import patch, Mock
from parameterized import parameterized, param

from samcli.local.docker.exceptions import ContainerNotStartableException, PortAlreadyInUse
from samcli.local.docker.exceptions import (
ContainerNotStartableException,
PortAlreadyInUse,
DockerContainerCreationFailedException,
)
from samcli.local.lambdafn.exceptions import FunctionNotFound
from samcli.lib.providers.exceptions import InvalidLayerReference
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
Expand Down Expand Up @@ -263,6 +267,10 @@ def test_must_raise_user_exception_on_invalid_env_vars(self, get_event_mock, Inv
PortAlreadyInUse("Container cannot be started, provided port already in use"),
"Container cannot be started, provided port already in use",
),
param(
DockerContainerCreationFailedException("Container creation failed, check template for potential issue"),
"Container creation failed, check template for potential issue",
),
]
)
@patch("samcli.commands.local.cli_common.invoke_context.InvokeContext")
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/local/apigw/test_local_apigw_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
LambdaResponseParseException,
PayloadFormatVersionValidateException,
)
from samcli.local.docker.exceptions import DockerContainerCreationFailedException
from samcli.local.lambdafn.exceptions import FunctionNotFound
from samcli.commands.local.lib.exceptions import UnsupportedInlineCodeError

Expand Down Expand Up @@ -457,6 +458,26 @@ def test_request_handles_error_when_invoke_function_with_inline_code(

self.assertEqual(response, not_implemented_response_mock)

@patch.object(LocalApigwService, "get_request_methods_endpoints")
@patch("samcli.local.apigw.local_apigw_service.ServiceErrorResponses")
@patch("samcli.local.apigw.local_apigw_service.LocalApigwService._generate_lambda_event")
def test_request_handles_error_when_container_creation_failed(
self, generate_mock, service_error_responses_patch, request_mock
):
generate_mock.return_value = {}
container_creation_failed_response_mock = Mock()
self.api_service._get_current_route = MagicMock()
self.api_service._get_current_route.return_value.payload_format_version = "2.0"
self.api_service._get_current_route.return_value.authorizer_object = None
self.api_service._get_current_route.methods = []

service_error_responses_patch.container_creation_failed.return_value = container_creation_failed_response_mock

self.lambda_runner.invoke.side_effect = DockerContainerCreationFailedException("container creation failed")
request_mock.return_value = ("test", "test")
response = self.api_service._request_handler()
self.assertEqual(response, container_creation_failed_response_mock)

@patch.object(LocalApigwService, "get_request_methods_endpoints")
def test_request_throws_when_invoke_fails(self, request_mock):
self.lambda_runner.invoke.side_effect = Exception()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import TestCase
from unittest.mock import Mock, patch, ANY, call

from samcli.local.docker.exceptions import DockerContainerCreationFailedException
from samcli.local.lambda_service import local_lambda_invoke_service
from samcli.local.lambda_service.local_lambda_invoke_service import LocalLambdaInvokeService, FunctionNamePathConverter
from samcli.local.lambdafn.exceptions import FunctionNotFound
Expand Down Expand Up @@ -111,6 +112,29 @@ def test_invoke_request_function_contains_inline_code(self, lambda_error_respons

lambda_error_responses_mock.not_implemented_locally.assert_called()

@patch("samcli.local.lambda_service.local_lambda_invoke_service.LambdaErrorResponses")
def test_invoke_request_container_creation_failed(self, lambda_error_responses_mock):
request_mock = Mock()
request_mock.get_data.return_value = b"{}"
local_lambda_invoke_service.request = request_mock

lambda_runner_mock = Mock()
lambda_runner_mock.invoke.side_effect = DockerContainerCreationFailedException("container creation failed")

lambda_error_responses_mock.container_creation_failed.return_value = "Container creation failed"

service = LocalLambdaInvokeService(lambda_runner=lambda_runner_mock, port=3000, host="localhost")

response = service._invoke_request_handler(function_name="FunctionContainerCreationFailed")

self.assertEqual(response, "Container creation failed")

lambda_runner_mock.invoke.assert_called_once_with(
"FunctionContainerCreationFailed", "{}", stdout=ANY, stderr=None
)

lambda_error_responses_mock.container_creation_failed.assert_called()

@patch("samcli.local.lambda_service.local_lambda_invoke_service.LocalLambdaInvokeService.service_response")
@patch("samcli.local.lambda_service.local_lambda_invoke_service.LambdaOutputParser")
def test_request_handler_returns_process_stdout_when_making_response(
Expand Down
Loading