terraform-aws-modules / terraform-aws-lambda

Terraform module, which takes care of a lot of AWS Lambda/serverless tasks (build dependencies, packages, updates, deployments) in countless combinations 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/lambda/aws
Apache License 2.0
901 stars 677 forks source link

Symlinks are not preserved during zip archive creation #596

Closed adamscybot closed 1 week ago

adamscybot commented 1 month ago

Description

The issue is that when specifying source_files such that some symlinks are encountered, the resulting zip archive does not preserve the symlinks (counter to some misinformation online, AWS Lambda itself does support this). Instead, the destination of any links is copied as a file/dir (it doesn't work on either).

This is a problem in my case since I use pnpm who's (much superior) install strategy is such that the node_modules structure relies on symlinks. As a result, the resulting zip created by this module is huge because of the duplication.

pnpm uses hard links but I've also verified the same issue occurs on soft links.

This module does not use the archive provider, which gave me hope that it would work since that has its own issue in the same area. But it's not to be confused with it.

The only mention of symlinks I could find on this repo was on this issue , where a contributor mentioned that one of the reasons this module has its own impl is symlink support. So, I think it's not working as expected.

According to this StackOverflow post, you have to do a bit extra to support this with Pythons zipfile.

Versions

+ provider registry.terraform.io/hashicorp/aws v5.59.0
+ provider registry.terraform.io/hashicorp/external v2.3.3
+ provider registry.terraform.io/hashicorp/local v2.5.1
+ provider registry.terraform.io/hashicorp/null v3.2.2

Also my Python is 3.10.12.

Reproduction Code [Required]

Manually create a dummy src dir

mkdir /tmp/aws-lambda-symlink-issue
echo "hard link destination" > /tmp/aws-lambda-symlink-issue/destination-hard
echo "soft-link destination" > /tmp/aws-lambda-symlink-issue/destination-soft
ln  /tmp/aws-lambda-symlink-issue/destination-hard  /tmp/aws-lambda-symlink-issue/hard-link
ln -s /tmp/aws-lambda-symlink-issue/destination-soft /tmp/aws-lambda-symlink-issue/soft-link

Create resources

module "lambda_function" {
  source = "terraform-aws-modules/lambda/aws"

  function_name = "broken_symlinks"
  description   = "My awesome lambda function"
  handler       = "index.lambda_handler"
  runtime       = "nodejs20.x"
  source_path = ["/tmp/aws-lambda-symlink-issue" ]
}

Get the zip file and check it

terraform apply

Then note down the zip filename it created in the builds folder. Then unzip it.

unzip <zip-archive>
ls -la .

Observe that this output shows no links and the links are copies of their original destination.

I have also ensured its not something weird with my unzip by proving a point by using it on something I manually used zip -y with. That worked fine and the zip was 4x smaller.

Expected behavior

I would expect the unzipped archive to maintain the symlinks.

Actual behavior

Any symlinks are copied as files/dirs, causing duplication

github-actions[bot] commented 3 weeks ago

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

github-actions[bot] commented 1 week ago

This issue was automatically closed because of stale in 10 days