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

fix: Handler path mapping for layer-wrapped esbuild functions #5450

Merged
merged 3 commits into from
Jul 5, 2023
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
4 changes: 4 additions & 0 deletions samcli/lib/build/bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

LOG = logging.getLogger(__name__)

LAYER_PREFIX = "/opt"
ESBUILD_PROPERTY = "esbuild"


Expand Down Expand Up @@ -157,6 +158,9 @@ def _should_update_handler(self, handler: str, name: str) -> bool:
if not handler_filename:
LOG.debug("Unable to parse handler, continuing without post-processing template.")
return False
if handler_filename.startswith(LAYER_PREFIX):
LOG.debug("Skipping updating the handler path as it is pointing to a layer.")
return False
expected_artifact_path = Path(self._build_dir, name, handler_filename)
return not expected_artifact_path.is_file()

Expand Down
27 changes: 27 additions & 0 deletions tests/end_to_end/test_runtimes_e2e.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from distutils.dir_util import copy_tree
from unittest import skipIf

import json
from pathlib import Path

import os
from parameterized import parameterized_class

from tests.end_to_end.end_to_end_base import EndToEndBase
Expand Down Expand Up @@ -163,3 +165,28 @@ def test_go_hello_world_default_workflow(self):
DefaultDeleteStage(BaseValidator(e2e_context), e2e_context, delete_command_list, stack_name),
]
self._run_tests(stages)


class TestEsbuildDatadogLayerIntegration(EndToEndBase):
app_template = ""

def test_integration(self):
function_name = "HelloWorldFunction"
event = '{"hello": "world"}'
stack_name = self._method_to_stack_name(self.id())
with EndToEndTestContext(self.app_name) as e2e_context:
project_path = str(Path("testdata") / "esbuild-datadog-integration")
os.mkdir(e2e_context.project_directory)
copy_tree(project_path, e2e_context.project_directory)
self.template_path = e2e_context.template_path
build_command_list = self.get_command_list()
deploy_command_list = self._get_deploy_command(stack_name)
remote_invoke_command_list = self._get_remote_invoke_command(stack_name, function_name, event, "json")
delete_command_list = self._get_delete_command(stack_name)
stages = [
EndToEndBaseStage(BuildValidator(e2e_context), e2e_context, build_command_list),
EndToEndBaseStage(BaseValidator(e2e_context), e2e_context, deploy_command_list),
EndToEndBaseStage(RemoteInvokeValidator(e2e_context), e2e_context, remote_invoke_command_list),
DefaultDeleteStage(BaseValidator(e2e_context), e2e_context, delete_command_list, stack_name),
]
self._run_tests(stages)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.lambdaHandler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({
message: 'hello world!',
}),
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

# Latest extension version: https:/DataDog/datadog-lambda-extension/releases
# Latest Node.js layer version: https:/DataDog/datadog-lambda-js/releases

Parameters:
DataDogLayers:
Description: DataDog layers
Type: CommaDelimitedList
Default: "arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Node18-x:93, arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Extension:44"

Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: /opt/nodejs/node_modules/datadog-lambda-js/handler.handler
Runtime: nodejs18.x
Environment:
Variables:
DD_LAMBDA_HANDLER: main.lambdaHandler
Layers: !Ref DataDogLayers
Metadata:
BuildMethod: esbuild
BuildProperties:
EntryPoints:
- main.js
7 changes: 7 additions & 0 deletions tests/unit/lib/build_module/test_bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ def test_check_invalid_lambda_handler_none_build_dir(self):
return_val = bundler_manager._should_update_handler("", "")
self.assertFalse(return_val)

def test_should_not_update_layer_path(self):
bundler_manager = EsbuildBundlerManager(Mock(), build_dir="/build/dir")
bundler_manager._get_path_and_filename_from_handler = Mock()
bundler_manager._get_path_and_filename_from_handler.return_value = "/opt/nodejs/node_modules/d/handler.handler"
return_val = bundler_manager._should_update_handler("", "")
self.assertFalse(return_val)

def test_update_function_handler(self):
resources = {
"FunctionA": {
Expand Down