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

fix: Checks for `npm` instead of `runtime` when building nodejs packages #364

Closed lorengordon closed 1 year ago

lorengordon commented 1 year ago

Description

Building nodejs packages requires npm, but the runtime check was looking for the runtime value, e.g. nodejs14.x. This patch changes the check to look for npm.

Motivation and Context

Fixes #363

Breaking Changes

How Has This Been Tested?

I'm not usualy nodejs locally, so would appreciate if someone else can confirm this one...

antonbabenko commented 1 year ago

Something still is not right. Try running terraform apply in examples/build-package. Here is an error:

β•·
β”‚ Error: local-exec provisioner error
β”‚ 
β”‚   with module.package_with_docker.null_resource.archive[0],
β”‚   on ../../package.tf line 65, in resource "null_resource" "archive":
β”‚   65:   provisioner "local-exec" {
β”‚ 
β”‚ Error running command 'builds/a1dc105bc2db47ec9f7aadf7908a002844a2a53bda0b2b7b12ce2de9643810d1.plan.json': exit status 1. Output: zip:
β”‚ creating 'builds/a1dc105bc2db47ec9f7aadf7908a002844a2a53bda0b2b7b12ce2de9643810d1.zip' archive
β”‚ zip: adding: index.py
β”‚ zip: adding content of directory: ./../fixtures/python3.8-app1/dir1/dir2
β”‚ zip: adding: ignore2.txt
β”‚ > docker images '--format={{.ID}}' public.ecr.aws/sam/build-python3.8
β”‚ Installing python requirements: ./../fixtures/python3.8-app1/requirements.txt
β”‚ > mktemp -d terraform-aws-lambda-XXXXXXXX # /var/folders/vy/wm1qhfns0rl564mb6w5w1b3c0000gn/T/terraform-aws-lambda-fkx2r4h3
β”‚ > cd /var/folders/vy/wm1qhfns0rl564mb6w5w1b3c0000gn/T/terraform-aws-lambda-fkx2r4h3
β”‚ > docker run --rm -w /var/task -v /private/var/folders/vy/wm1qhfns0rl564mb6w5w1b3c0000gn/T/terraform-aws-lambda-fkx2r4h3:/var/task:z -v
β”‚ /Users/Bob/.ssh/known_hosts:/root/.ssh/known_hosts:z --mount
β”‚ type=bind,src=/run/host-services/ssh-auth.sock,target=/run/host-services/ssh-auth.sock -e SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock -v
β”‚ /Users/Bob/Sites/terraform-aws-modules/terraform-aws-lambda/examples/build-package/builds/cache/pip:/root/.cache/pip:z --entrypoint ''
β”‚ '9453fa734ff9
β”‚ eb960e2fbbf5' /bin/sh -c 'python3.8 -m pip install --no-compile --prefix= --target=. --requirement=requirements.txt && chown -R 501:20 .'
β”‚ docker: invalid reference format.
β”‚ See 'docker run --help'.
β”‚ zip: Error during zip archive creation
β”‚ Traceback (most recent call last):
β”‚   File "/Users/Bob/Sites/terraform-aws-modules/terraform-aws-lambda/examples/build-package/../../package.py", line 1292, in build_command
β”‚     bpm.execute(build_plan, zs, query)
β”‚   File "/Users/Bob/Sites/terraform-aws-modules/terraform-aws-lambda/examples/build-package/../../package.py", line 824, in execute
β”‚     with install_pip_requirements(query, pip_requirements, tmp_dir) as rd:
β”‚   File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/contextlib.py", line 135, in __enter__
β”‚     return next(self.gen)
β”‚   File "/Users/Bob/Sites/terraform-aws-modules/terraform-aws-lambda/examples/build-package/../../package.py", line 961, in install_pip_requirements
β”‚     check_call(docker_run_command(
β”‚   File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 369, in check_call
β”‚     raise CalledProcessError(retcode, cmd)
β”‚ subprocess.CalledProcessError: Command '['docker', 'run', '--rm', '-w', '/var/task', '-v',
β”‚ '/private/var/folders/vy/wm1qhfns0rl564mb6w5w1b3c0000gn/T/terraform-aws-lambda-fkx2r4h3:/var/task:z', '-v',
β”‚ '/Users/Bob/.ssh/known_hosts:/root/.ssh/known_hosts:z', '--mount',
β”‚ 'type=bind,src=/run/host-services/ssh-auth.sock,target=/run/host-services/ssh-auth.sock', '-e',
β”‚ 'SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock', '-v',
β”‚ '/Users/Bob/Sites/terraform-aws-modules/terraform-aws-lambda/examples/build-package/builds/cache/pip:/root/.cache/pip:z', '--entrypoint',
β”‚ '', '9453fa734ff9\neb960e2fbbf5', '/bin/sh', '-c', 'python3.8 -m pip install --no-compile --prefix= --target=.
β”‚ --requirement=requirements.txt && chown -R 501:20 .']' returned non-zero exit status 125.
β”‚ 
β•΅
β•·
β”‚ Error: local-exec provisioner error
β”‚ 
β”‚   with module.lambda_layer.null_resource.archive[0],
β”‚   on ../../package.tf line 65, in resource "null_resource" "archive":
β”‚   65:   provisioner "local-exec" {
β”‚ 
β”‚ Error running command 'builds/8e6338d3e2d57de107a95f1f99973201ab2964bb45c98a63db3de3796bf67890.plan.json': exit status 1. Output: zip:
β”‚ creating 'builds/8e6338d3e2d57de107a95f1f99973201ab2964bb45c98a63db3de3796bf67890.zip' archive
β”‚ > docker images '--format={{.ID}}' public.ecr.aws/sam/build-python3.8
β”‚ Installing python requirements: ./../fixtures/python3.8-app1/requirements.txt
β”‚ > mktemp -d terraform-aws-lambda-XXXXXXXX # /var/folders/vy/wm1qhfns0rl564mb6w5w1b3c0000gn/T/terraform-aws-lambda-lr1s0phv
β”‚ > cd /var/folders/vy/wm1qhfns0rl564mb6w5w1b3c0000gn/T/terraform-aws-lambda-lr1s0phv
β”‚ > docker run --rm -w /var/task -v /private/var/folders/vy/wm1qhfns0rl564mb6w5w1b3c0000gn/T/terraform-aws-lambda-lr1s0phv:/var/task:z -v
β”‚ /Users/Bob/.ssh/known_hosts:/root/.ssh/known_hosts:z --entrypoint '' '9453fa734ff9
β”‚ eb960e2fbbf5' /bin/sh -c 'python3.8 -m pip install --no-compile --prefix= --target=. --requirement=requirements.txt && chown -R 501:20 .'
β”‚ docker: invalid reference format.
β”‚ See 'docker run --help'.
β”‚ zip: Error during zip archive creation
β”‚ Traceback (most recent call last):
β”‚   File "/Users/Bob/Sites/terraform-aws-modules/terraform-aws-lambda/examples/build-package/../../package.py", line 1292, in build_command
β”‚     bpm.execute(build_plan, zs, query)
β”‚   File "/Users/Bob/Sites/terraform-aws-modules/terraform-aws-lambda/examples/build-package/../../package.py", line 824, in execute
β”‚     with install_pip_requirements(query, pip_requirements, tmp_dir) as rd:
β”‚   File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/contextlib.py", line 135, in __enter__
β”‚     return next(self.gen)
β”‚   File "/Users/Bob/Sites/terraform-aws-modules/terraform-aws-lambda/examples/build-package/../../package.py", line 961, in install_pip_requirements
β”‚     check_call(docker_run_command(
β”‚   File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 369, in check_call
β”‚     raise CalledProcessError(retcode, cmd)
β”‚ subprocess.CalledProcessError: Command '['docker', 'run', '--rm', '-w', '/var/task', '-v',
β”‚ '/private/var/folders/vy/wm1qhfns0rl564mb6w5w1b3c0000gn/T/terraform-aws-lambda-lr1s0phv:/var/task:z', '-v',
β”‚ '/Users/Bob/.ssh/known_hosts:/root/.ssh/known_hosts:z', '--entrypoint', '', '9453fa734ff9\neb960e2fbbf5', '/bin/sh', '-c', 'python3.8 -m pip
β”‚ install --no-compile --prefix= --target=. --requirement=requirements.txt && chown -R 501:20 .']' returned non-zero exit status 125.
β”‚ 
β•΅
lorengordon commented 1 year ago

Hmm, pretty sure nothing I have modified impacts the apply phase, only the plan phase... When is the last time that worked?

lorengordon commented 1 year ago

I see this error in the output on apply...

module.package_with_docker.null_resource.archive[0]: Provisioning with 'local-exec'...
module.package_with_docker.null_resource.archive[0] (local-exec): Executing: ["python3" "../../package.py" "build" "--timestamp" "1666619759869896000" "builds/a1dc105bc2db47ec9f7aadf7908a002844a2a53bda0b2b7b12ce2de9643810d1.plan.json"]
module.package_with_docker.null_resource.archive[0] (local-exec): zip: creating 'builds/a1dc105bc2db47ec9f7aadf7908a002844a2a53bda0b2b7b12ce2de9643810d1.zip' archive
module.package_with_docker.null_resource.archive[0] (local-exec): zip: adding: index.py
module.package_with_docker.null_resource.archive[0] (local-exec): zip: adding content of directory: ./../fixtures/python3.8-app1/dir1/dir2
module.package_with_docker.null_resource.archive[0] (local-exec): zip: adding: ignore2.txt
module.package_with_docker.null_resource.archive[0] (local-exec): > docker images '--format={{.ID}}' public.ecr.aws/sam/build-python3.8
module.package_with_docker.null_resource.archive[0] (local-exec): > docker build --tag public.ecr.aws/sam/build-python3.8 ./../../docker
module.package_with_docker.null_resource.archive[0] (local-exec): unable to prepare context: path "./../../docker" not found
module.package_with_docker.null_resource.archive[0] (local-exec): zip: Error during zip archive creation
module.package_with_docker.null_resource.archive[0] (local-exec): Traceback (most recent call last):
module.package_with_docker.null_resource.archive[0] (local-exec):   File "../../package.py", line 1292, in build_command
module.package_with_docker.null_resource.archive[0] (local-exec):     bpm.execute(build_plan, zs, query)
module.package_with_docker.null_resource.archive[0] (local-exec):   File "../../package.py", line 824, in execute
module.package_with_docker.null_resource.archive[0] (local-exec):     with install_pip_requirements(query, pip_requirements, tmp_dir) as rd:
module.package_with_docker.null_resource.archive[0] (local-exec):   File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
module.package_with_docker.null_resource.archive[0] (local-exec):     return next(self.gen)
module.package_with_docker.null_resource.archive[0] (local-exec):   File "../../package.py", line 909, in install_pip_requirements
module.package_with_docker.null_resource.archive[0] (local-exec):     check_call(docker_cmd)
module.package_with_docker.null_resource.archive[0] (local-exec):   File "/usr/lib/python3.8/subprocess.py", line 364, in check_call
module.package_with_docker.null_resource.archive[0] (local-exec):     raise CalledProcessError(retcode, cmd)
module.package_with_docker.null_resource.archive[0] (local-exec): subprocess.CalledProcessError: Command '['docker', 'build', '--tag', 'public.ecr.aws/sam/build-python3.8', './../../docker']' returned non-zero exit status 1.

This line appears to be the problem:

module.package_with_docker.null_resource.archive[0] (local-exec): unable to prepare context: path "./../../docker" not found

But I am still unsure how anything I have changed might have resulted in an apply-time failure, instead of a plan-time failure...

lorengordon commented 1 year ago

If I checkout v4.1.4 and cherry-pick this commit, the plan succeeds, showing this commit does fix what it aims to fix. The apply still fails but in a completely different way. So I think something else is going on with that example?

β”‚ Error: local-exec provisioner error
β”‚
β”‚   with module.lambda_layer.null_resource.archive[0],
β”‚   on ../../package.tf line 65, in resource "null_resource" "archive":
β”‚   65:   provisioner "local-exec" {
β”‚
β”‚ Error running command 'builds/4f8e10b871c695ccfeaab8a46e922b072f124b29a49572b366258620e88efc31.plan.json': exit status 1. Output: zip: creating 'builds/4f8e10b871c695ccfeaab8a46e922b072f124b29a49572b366258620e88efc31.zip' archive
β”‚ > docker images '--format={{.ID}}' public.ecr.aws/sam/build-python3.8
β”‚ Installing python requirements: ./../fixtures/python3.8-app1/requirements.txt
β”‚ > mktemp -d terraform-aws-lambda-XXXXXXXX # /home/loren/git/repos/terraform-aws-lambda/examples/build-package/terraform-aws-lambda-40lzwm94
β”‚ > cd /home/loren/git/repos/terraform-aws-lambda/examples/build-package/terraform-aws-lambda-40lzwm94
β”‚ > docker run --rm -w /var/task -v /home/loren/git/repos/terraform-aws-lambda/examples/build-package/terraform-aws-lambda-40lzwm94:/var/task:z -v /home/loren/.ssh/known_hosts:/root/.ssh/known_hosts:z --entrypoint '' 'ceeef822b3e6
β”‚ 5d0f1eb64cdc' /bin/sh -c 'python3.8 -m pip install --no-compile --prefix= --target=. --requirement=requirements.txt && chown -R 1000:1000 .'
β”‚ docker: invalid reference format.
β”‚ See 'docker run --help'.
β”‚ zip: Error during zip archive creation
β”‚ Traceback (most recent call last):
β”‚   File "../../package.py", line 1292, in build_command
β”‚     bpm.execute(build_plan, zs, query)
β”‚   File "../../package.py", line 824, in execute
β”‚     with install_pip_requirements(query, pip_requirements, tmp_dir) as rd:
β”‚   File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
β”‚     return next(self.gen)
β”‚   File "../../package.py", line 961, in install_pip_requirements
β”‚     check_call(docker_run_command(
β”‚   File "/usr/lib/python3.8/subprocess.py", line 364, in check_call
β”‚     raise CalledProcessError(retcode, cmd)
β”‚ subprocess.CalledProcessError: Command '['docker', 'run', '--rm', '-w', '/var/task', '-v', '/home/loren/git/repos/terraform-aws-lambda/examples/build-package/terraform-aws-lambda-40lzwm94:/var/task:z', '-v', '/home/loren/.ssh/known_hosts:/root/.ssh/known_hosts:z', '--entrypoint', '',
β”‚ 'ceeef822b3e6\n5d0f1eb64cdc', '/bin/sh', '-c', 'python3.8 -m pip install --no-compile --prefix= --target=. --requirement=requirements.txt && chown -R 1000:1000 .']' returned non-zero exit status 125.
β”‚
β•΅
β•·
β”‚ Error: local-exec provisioner error
β”‚
β”‚   with module.package_with_docker.null_resource.archive[0],
β”‚   on ../../package.tf line 65, in resource "null_resource" "archive":
β”‚   65:   provisioner "local-exec" {
β”‚
β”‚ Error running command 'builds/a1dc105bc2db47ec9f7aadf7908a002844a2a53bda0b2b7b12ce2de9643810d1.plan.json': exit status 1. Output: zip: creating 'builds/a1dc105bc2db47ec9f7aadf7908a002844a2a53bda0b2b7b12ce2de9643810d1.zip' archive
β”‚ zip: adding: index.py
β”‚ zip: adding content of directory: ./../fixtures/python3.8-app1/dir1/dir2
β”‚ zip: adding: ignore2.txt
β”‚ > docker images '--format={{.ID}}' public.ecr.aws/sam/build-python3.8
β”‚ Installing python requirements: ./../fixtures/python3.8-app1/requirements.txt
β”‚ > mktemp -d terraform-aws-lambda-XXXXXXXX # /home/loren/git/repos/terraform-aws-lambda/examples/build-package/terraform-aws-lambda-a1nh4ez2
β”‚ > cd /home/loren/git/repos/terraform-aws-lambda/examples/build-package/terraform-aws-lambda-a1nh4ez2
β”‚ zip: Error during zip archive creation
β”‚ Traceback (most recent call last):
β”‚   File "../../package.py", line 1292, in build_command
β”‚     bpm.execute(build_plan, zs, query)
β”‚   File "../../package.py", line 824, in execute
β”‚     with install_pip_requirements(query, pip_requirements, tmp_dir) as rd:
β”‚   File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
β”‚     return next(self.gen)
β”‚   File "../../package.py", line 961, in install_pip_requirements
β”‚     check_call(docker_run_command(
β”‚   File "../../package.py", line 1129, in docker_run_command
β”‚     sock = os.environ['SSH_AUTH_SOCK']  # TODO: Handle missing env var
β”‚   File "/usr/lib/python3.8/os.py", line 675, in __getitem__
β”‚     raise KeyError(key) from None
β”‚ KeyError: 'SSH_AUTH_SOCK'
antonbabenko commented 1 year ago

Too bad :( I was pretty sure I ran those examples not long time ago, but I can't be sure about this now. I don't have time to investigate the reason and fix it now. Maybe later this week.

lorengordon commented 1 year ago

There might be something about my local shell that is not setup correctly to work with the build-package example... I checked out the last change to the example, v3.0.0, and that apply failed in the same way as v4.1.4 + my cherry-pick.

antonbabenko commented 1 year ago

Thank you, @lorengordon !

antonbabenko commented 1 year ago

This PR is included in version 4.2.2 :tada:

github-actions[bot] commented 1 year ago

I'm going to lock this pull request 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 related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.