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
892 stars 662 forks source link

npm_requirements does not run npm install #385

Closed bryant8317 closed 1 year ago

bryant8317 commented 1 year ago

Description

According to the documentation, npm_requirements controls whether npm install is run when building a package. However, it does not work correctly.

When setting source_path directly to the directory's path with package.json, the build plan it creates contains the npm step.

Terraform Snippet:

module "test-layer" {
  source = "terraform-aws-modules/lambda/aws"
  create_layer    = true
  build_in_docker = true
  layer_name  = "test-layer"
  description = "test-npm-install"
  runtime             = "nodejs14.x"
  compatible_runtimes = ["nodejs14.x"]
  source_path = "src/test_package"
}

Build Plan:

{
  "filename": "builds\\xxx.zip",
  "runtime": "nodejs14.x",
  "artifacts_dir": "builds",
  "build_plan": [
    ["npm", "nodejs14.x", "src/test_package\\package.json", null, null],
    ["zip", "src/test_package", null]
  ],
  "docker": {
    "docker_additional_options": [],
    "docker_build_root": "",
    "docker_entrypoint": null,
    "docker_file": "",
    "docker_image": "",
    "docker_pip_cache": null,
    "with_ssh_agent": false
  }
}

However, when I add npm_requiremetns and prefix_in_zip to the terraform template, the build plan no longer contains the npm step.

Terraform Snippet:

module "test-layer" {
  source = "terraform-aws-modules/lambda/aws"
  create_layer    = true
  build_in_docker = true
  layer_name  = "test-layer"
  description = "test-npm-install"
  runtime             = "nodejs14.x"
  compatible_runtimes = ["nodejs14.x"]
  source_path = [
    {
      path             = "src/test_package",
      npm_requirements = true,
      prefix_in_zip    = "nodejs/"
    }
  ]
}

Build Plan:

{
  "filename": "builds\\xxxx.zip",
  "runtime": "nodejs14.x",
  "artifacts_dir": "builds",
  "build_plan": [["zip", "src/test_package", "nodejs/"]],
  "docker": {
    "docker_additional_options": [],
    "docker_build_root": "",
    "docker_entrypoint": null,
    "docker_file": "",
    "docker_image": "",
    "docker_pip_cache": null,
    "with_ssh_agent": false
  }
}

Versions

Reproduction Code [Required]

Run terraform plan with the following template

module "test-layer" {
  source = "terraform-aws-modules/lambda/aws"
  create_layer    = true
  build_in_docker = true
  layer_name  = "test-layer"
  description = "test-npm-install"
  runtime             = "nodejs14.x"
  compatible_runtimes = ["nodejs14.x"]
  source_path = [
    {
      path             = "src/test_package",
      npm_requirements = true,
      prefix_in_zip    = "nodejs/"
    }
  ]
}

Expected behavior

I expect to see npm step in the build plan similar to below

{
  "filename": "builds\\xxxx.zip",
  "runtime": "nodejs14.x",
  "artifacts_dir": "builds",
  "build_plan": [
    ["npm", "nodejs14.x", "src/test_package\\package.json", null, null],
    ["zip", "src/test_package", "nodejs/"]
   ],
  "docker": {
    "docker_additional_options": [],
    "docker_build_root": "",
    "docker_entrypoint": null,
    "docker_file": "",
    "docker_image": "",
    "docker_pip_cache": null,
    "with_ssh_agent": false
  }
}

Actual behavior

{
  "filename": "builds\\xxxx.zip",
  "runtime": "nodejs14.x",
  "artifacts_dir": "builds",
  "build_plan": [["zip", "src/test_package", "nodejs/"]],
  "docker": {
    "docker_additional_options": [],
    "docker_build_root": "",
    "docker_entrypoint": null,
    "docker_file": "",
    "docker_image": "",
    "docker_pip_cache": null,
    "with_ssh_agent": false
  }
}

Additional context

I have checked package.py and found the following line https://github.com/terraform-aws-modules/terraform-aws-lambda/blob/2e9aaa2d37d61299bfdaa8b919a75cb37f4726b7/package.py#L794

It seems like package.py is not getting the correct parameter (npm_requirements). In fact, when I add npm_package_json to my terraform template. The build plan behaves correctly as I expect.

module "test-layer" {
  source = "terraform-aws-modules/lambda/aws"
  create_layer    = true
  build_in_docker = true
  layer_name  = "test-layer"
  description = "test-npm-install"
  runtime             = "nodejs14.x"
  compatible_runtimes = ["nodejs14.x"]
  source_path = [
    {
      path             = "src/test_package",
      npm_requirements = true,
      npm_package_json = true,
      prefix_in_zip    = "nodejs/"
    }
  ]
}

I think this line in package.py will need to be changed to

 npm_requirements = claim.get('npm_requirements')

I will use npm_package_json as a workaround in the meanwhile

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

This issue was automatically closed because of stale in 10 days

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.