aws / aws/aws-sam-cli

Broken local symlinks in Lambda function crashes `sam deploy` and `sam package`

Open
#4,364 3 comments 2 reactions 0 assignees View on GitHub
area/build area/esbuild type/feature
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### Description:
Lambda now supports ES6 modules, but due to how module resolution works and how Lambda structures layers, you can't place your common modules in a layer - the imports in the function code will fail to find them.

As described here - [https://github.com/vibe/aws-esm-modules-layer-support](https://github.com/vibe/aws-esm-modules-layer-support)
the simple solution is to add a symlink in your function for the `node_modules` folder, pointing it to the `/opt/nodejs/node_modules` folder of your layer.

Whilst this does work - it is not supported in the SAM workflow, as `sam package` or `sam deploy` will both fail as the Zip process tries to follow symlinks - and at run time (on local machine) the destination of the symlink does not exist - as it points to the path on the Lambda environment.

I'm pretty sure its the `followlinks` flag in the zip command:
[https://github.com/aws/aws-sam-cli/blob/435f81fa035e08442472da14d4db1c4d2de6db29/samcli/lib/package/utils.py#L241](https://github.com/aws/aws-sam-cli/blob/435f81fa035e08442472da14d4db1c4d2de6db29/samcli/lib/package/utils.py#L241)

### Steps to reproduce:
Create a Layer with a `nodejs/node_modules` folder, and install a package.
Create a Lambda Function and import that package.
Add a symlink in the Lambda Function to the layer's `nodejs/node_modules` folder, BUT using the paths that will be in use once deployed into Lambda - `ln -s /opt/nodejs/node_modules my_funciton/node_modules`

### Observed result:
```
2022-11-01 10:21:38,605 | Unable to export
Traceback (most recent call last):
File "/opt/homebrew/Cellar/aws-sam-cli/1.58.0/libexec/lib/python3.8/site-packages/samcli/lib/package/packageable_resources.py", line 127, in export
self.do_export(resource_id, resource_dict, parent_dir)
File "/opt/homebrew/Cellar/aws-sam-cli/1.58.0/libexec/lib/python3.8/site-packages/samcli/lib/package/packageable_resources.py", line 149, in do_export
uploaded_url = upload_local_artifacts(
File "/opt/homebrew/Cellar/aws-sam-cli/1.58.0/libexec/lib/python3.8/site-packages/samcli/lib/package/utils.py", line 171, in upload_local_artifacts
return zip_and_upload(local_path, uploader, extension)
File "/opt/homebrew/Cellar/aws-sam-cli/1.58.0/libexec/lib/python3.8/site-packages/samcli/lib/package/utils.py", line 188, in zip_and_upload
with zip_folder(local_path) as (zip_file, md5_hash):
File "/opt/homebrew/Cellar/python@3.8/3.8.14/Frameworks/Python.framework/Versions/3.8/lib/python3.8/contextlib.py", line 113, in __enter__
return next(self.gen)
File "/opt/homebrew/Cellar/aws-sam-cli/1.58.0/libexec/lib/python3.8/site-packages/samcli/lib/package/utils.py", line 210, in zip_folder
md5hash = dir_checksum(folder_path, followlinks=True)
File "/opt/homebrew/Cellar/aws-sam-cli/1.58.0/libexec/lib/python3.8/site-packages/samcli/lib/utils/hash.py", line 86, in dir_checksum
filepath_checksum = file_checksum(file)
File "/opt/homebrew/Cellar/aws-sam-cli/1.58.0/libexec/lib/python3.8/site-packages/samcli/lib/utils/hash.py", line 29, in file_checksum
with open(file_name, "rb") as file_handle:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/matt/test-sam-app/.aws-sam/build/MyLambdaFunction/node_modules'
Error: Unable to upload artifact MyLambdaFunction referenced by CodeUri parameter of MyLambdaFunction resource.
[Errno 2] No such file or directory: '/Users/matt/test-sam-app/.aws-sam/build/MyLambdaFunction/node_modules'

```

### Expected result:
Perhaps an option to enable / disable follow symlinks so it doesn't try to resolve them?

Or catch the error and still successfully package but with warnings ?

### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

1. OS: macos 12.6
2. `sam --version`: AM CLI, version 1.58.0
3. AWS region: eu-west-1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.