aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.52k stars 1.17k forks source link

When the package at `CodeUri` does not exist, report a warning #1754

Open ashleyfrieze opened 4 years ago

ashleyfrieze commented 4 years ago

Describe your idea/feature/enhancement

When using sam local start-lambda, invoke or start-api, the output describes the activities between the local machine and the lambci container while processing the invocation. When the package is present and a zip/jar, then we even see it being de-compressed before mounting.

However, I've been caught out a few times by sam quietly just mounting the CodeUri path in the target container when that path does not exist on the local machine. This results in an error which is secondary to the problem.

Proposal

While detecting if the CodeUri represents something to unzip/unpack, output a warning if the CodeUri appears to refer to a non-existent path. This will speed up discovery of this common error.

Additional Details

awood45 commented 4 years ago

Can you give an example of a situation that would trigger this condition?

CoshUS commented 3 years ago

Closing as there is no response. Feel free to reopen if there is further information or issue.

ashleyfrieze commented 3 years ago

@CoshUS and @awood45 - sorry for not replying sooner.

Our use case:

The output of sam on start-api is normal.

The output of sam while it starts the child container, unzipping the non existent zip into its temp directory before mounting it into the running container, all looks normal.

The error we get is from the lambda runtime saying that the handler cannot be found.

However, the real error is that there's nothing to mount from the CodeUri.

While it's possible to work out what's gone wrong, an error or warning that there's nothing present behind the location specified in the CodeUri would be remarkably helpful!

ashleyfrieze commented 3 years ago

@CoshUS _ don't have the user rights to reopen this

sriram-mv commented 3 years ago

This looks to be a sane ask, marking this as open for contributors.

DewaldDeJager commented 1 year ago

I would really like to start contributing to this project so I've been looking at a few issues with the contributors/welcome and contributors/good-first-issue tags and found this one. It seems pretty straightforward. I've made sure this issue is still reproducible with the latest code as documented below.

I'm using the Hello World Python function which is defined in the template as (Note that CodeUri references a directory):

  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: HelloWorldFunction
      Handler: app.lambda_handler
      Runtime: python3.8
      Architectures:
        - x86_64

When I run sam build and then sam local invoke HelloWorldFunction --debug, the function executes as expected and I can see this in the logs:

2023-05-26 15:31:24,190 | Invoking app.lambda_handler (python3.8)
2023-05-26 15:31:26,255 | Resolving code path. Cwd=/Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build,
CodeUri=/Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip
2023-05-26 15:31:26,256 | Resolved absolute path to code is /Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip
2023-05-26 15:31:26,259 | Decompressing /Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip
2023-05-26 15:31:49,579 | Mounting /private/var/folders/nf/93d01hl9725g48vb7pbt4dr8ns4dm0/T/tmphzxneft1 as /var/task:ro,delegated, inside runtime container

If I change the CodeUri to point to an existing ZIP file like so:

  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: HelloWorldFunction/Archive.zip
      Handler: app.lambda_handler
      Runtime: python3.8
      Architectures:
      - x86_64

The function executes as expected and I see these logs:

2023-05-26 15:31:24,190 | Invoking app.lambda_handler (python3.8)
2023-05-26 15:31:26,255 | Resolving code path. Cwd=/Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build,
CodeUri=/Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip
2023-05-26 15:31:26,256 | Resolved absolute path to code is /Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip
2023-05-26 15:31:26,259 | Decompressing /Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip
2023-05-26 15:31:49,579 | Mounting /private/var/folders/nf/93d01hl9725g48vb7pbt4dr8ns4dm0/T/tmphzxneft1 as /var/task:ro,delegated, inside runtime container

Now if I leave the function pointing to a zip archive but I delete the archive, the function fails with this error message:

Error: 500 Server Error: Internal Server Error ("error while creating mount source path '/Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip': chown /Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip: permission denied")

And I can see this in the logs (Similar to if I were pointing to a directory but the URI referenced doesn't exist as a file or as a directory):

2023-05-26 13:20:43,606 | Invoking app.lambda_handler (python3.8)
2023-05-26 13:20:45,651 | Resolving code path. Cwd=/Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build,
CodeUri=/Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip
2023-05-26 13:20:45,654 | Resolved absolute path to code is /Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip
2023-05-26 13:20:45,655 | Code /Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip is not a zip/jar file
2023-05-26 13:21:09,514 | Mounting /Users/dewald.dejager/Documents/sam-cli-reproduce-issue-1754/.aws-sam/build/HelloWorldFunction/Archive.zip as /var/task:ro,delegated, inside runtime container
DewaldDeJager commented 1 year ago

The comments in the code document the scenarios that are should be catered for: https://github.com/aws/aws-sam-cli/blob/de9da32eeb4300074d9357649bdf8fb1bcbe491f/samcli/local/lambdafn/runtime.py#L271-L275 And this is the implementation of the logic: https://github.com/aws/aws-sam-cli/blob/de9da32eeb4300074d9357649bdf8fb1bcbe491f/samcli/local/lambdafn/runtime.py#L289-L295 I found this test that's a little misleading because the name implies that the file should exist: https://github.com/aws/aws-sam-cli/blob/de9da32eeb4300074d9357649bdf8fb1bcbe491f/tests/unit/local/lambdafn/test_runtime.py#L562-L572 The only other test is for when it is a valid ZIP/JAR file and needs to be decompressed.

This issue suggests that a warning should be logged but I'd like to understand if either of these two are valid use cases?

  1. The CodeUri points to a file that is not a ZIP/JAR file such as foo.exe in the example
  2. The CodeUri points to something that does not exist (file/directory)

I think the execution should fail and output an error in (1) and potentially in (2) but I'm not sure if there is maybe a valid scenario I'm not considering. Thoughts?