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
885 stars 658 forks source link

Allow nodejs build to run npm install with --omit=dev #568

Closed braun1928 closed 6 days ago

braun1928 commented 1 month ago

Is your request related to a problem? Please describe.

When building node lambda, it runs just a npm install. If the package.json file has things in devDependencies, the package file may get a bit bloated unnecessarily. Specific case: one lambda goes from 4MB to 28MB, and obviously, takes a little longer to install the dependencies.

Describe the solution you'd like.

A way to signal the build process to append --omit=dev with node packages.

Describe alternatives you've considered.

Using a command list in source_path works, BUT that makes it not possible to use build_in_docker, which renders another list of issues (lots of different runtimes needed, CI pipelines, etc).

sachasmart commented 1 month ago

What if you create a separate resource like:

resource "null_resource" "node_build" {
...
  provisioner "local-exec" {
    command = "<build command>"
  }

....
data "archive_file" "lambda_package" {
  depends_on  = [null_resource.function_binary]
  type        = "zip"
  source_file = "<path>"
  output_path = "<path>"
}

...

Then point your lambda resource block to that archive_file resources.

github-actions[bot] commented 1 week 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

braun1928 commented 6 days ago

Workaround/clean way: set docker environment variable NODE_ENVIRONMENT to production (using docker_additional_options), the desired behaviour is achieved and no need to customize commands or anything. If not building in docker (not my case) then needs to run terraform with that variable set.