aws-extensions-for-dotnet-cli icon indicating copy to clipboard operation
aws-extensions-for-dotnet-cli copied to clipboard

Global CodeUri ignored - treated as empty string and zips current working directory

Open drch- opened this issue 4 years ago • 5 comments

Setting CodeUri in the Globals section of the SAM template zips up the current working directory.

Workaround: Don't put CodeUri in Globals, and leave it in the individual function specifications.

Use Case: My code has the implementation for several lambda functions with different entrypoints for each which are mapped to API Gateway Verbs.

GET /Product => handler.GetProducts()
POST /Product => handler.CreateProduct()
DELETE /Product => handler.DeleteProduct()

The deployment works correctly if each of these have their own CodeUri, and the tool is smart enough to only build the project once.

I wanted to move the common properties (runtime, memory, timeout, codeuri) to the Globals section for re-use. Unfortunately, lambda tooling seems to treat the CodeUri as blank, and packs up the working directory.

A very simple repro can be found here: https://github.com/drch-/dotnet-sam-repro

Function Code:

namespace func
{
    public class Function
    {
        public string UpperHandler(string input, ILambdaContext context)
        {
            return input?.ToUpper();
        }

        public string LowerHandler(string input, ILambdaContext context)
        {
            return input?.ToLower();
        }
    }
}

Working Template: The following template works correctly.

# serverless.template.yml
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"

Resources:
  UpperFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: func::func.Function::UpperHandler
      Runtime: dotnetcore3.1
      MemorySize: 256
      Timeout: 30
      CodeUri: ./func

  LowerFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: func::func.Function::LowerHandler
      Runtime: dotnetcore3.1
      MemorySize: 256
      Timeout: 30
      CodeUri: ./func

Outputs:
  upperFuncName:
    Value: !Ref UpperFunction
  lowerFuncName:
    Value: !Ref LowerFunction
dotnet lambda deploy-serverless --template ./serverless.template.yml --stack-name func1 --s3-bucket MY-DEPLOYMENT-BUCKET
Amazon Lambda Tools for .NET Core applications (4.0.0)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Processing CloudFormation resource UpperFunction
Initiate packaging of ./func for resource UpperFunction
Executing publish command
Deleted previous publish folder
... invoking 'dotnet publish', working folder '/Users/XXX/work/dotnet/bug/func/src/././func/bin/Release/netcoreapp3.1/publish'
... publish: Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core
... publish: Copyright (C) Microsoft Corporation. All rights reserved.
... publish:   Restore completed in 56.99 ms for /Users/XXX/work/dotnet/bug/func/src/func/func.csproj.
... publish:   func -> /Users/XXX/work/dotnet/bug/func/src/func/bin/Release/netcoreapp3.1/linux-x64/func.dll
... publish:   func -> /Users/XXX/work/dotnet/bug/func/src/func/bin/Release/netcoreapp3.1/publish/
Changed permissions on published file (chmod +rx func.dll).
Changed permissions on published file (chmod +rx func.pdb).
Changed permissions on published file (chmod +rx Amazon.Lambda.Serialization.SystemTextJson.dll).
Changed permissions on published file (chmod +rx func.runtimeconfig.json).
Changed permissions on published file (chmod +rx Amazon.Lambda.Core.dll).
Changed permissions on published file (chmod +rx func.deps.json).
Zipping publish folder /Users/XXX/work/dotnet/bug/func/src/././func/bin/Release/netcoreapp3.1/publish to /var/folders/ks/s5_58mcd11g38n170k32yc8cb74n8n/T/UpperFunction-CodeUri-637236313627143020.zip
... zipping:   adding: func.dll (deflated 56%)
... zipping:   adding: func.pdb (deflated 21%)
... zipping:   adding: Amazon.Lambda.Serialization.SystemTextJson.dll (deflated 45%)
... zipping:   adding: func.runtimeconfig.json (deflated 23%)
... zipping:   adding: Amazon.Lambda.Core.dll (deflated 46%)
... zipping:   adding: func.deps.json (deflated 68%)
Created publish archive (/var/folders/ks/s5_58mcd11g38n170k32yc8cb74n8n/T/UpperFunction-CodeUri-637236313627143020.zip).
Lambda project successfully packaged: /var/folders/ks/s5_58mcd11g38n170k32yc8cb74n8n/T/UpperFunction-CodeUri-637236313627143020.zip
Uploading to S3. (Bucket: MY-DEPLOYMENT-BUCKET Key: UpperFunction-CodeUri-637236313627143020-637236313660603570.zip)
... Progress: 100%
Processing CloudFormation resource LowerFunction
Using previous upload artifact s3://MY-DEPLOYMENT-BUCKET/UpperFunction-CodeUri-637236313627143020-637236313660603570.zip for resource LowerFunction
Uploading to S3. (Bucket: MY-DEPLOYMENT-BUCKET Key: func1-serverless.template-637236313667702050.yml)
... Progress: 100%
Found existing stack: True
CloudFormation change set created
... Waiting for change set to be reviewed
Initiated CloudFormation stack update on func1

Timestamp            Logical Resource Id                      Status
-------------------- ---------------------------------------- ----------------------------------------
2020-04-28 12:42 AM  func1                                    UPDATE_IN_PROGRESS
2020-04-28 12:42 AM  UpperFunction                            UPDATE_IN_PROGRESS
2020-04-28 12:42 AM  LowerFunction                            UPDATE_IN_PROGRESS
2020-04-28 12:42 AM  UpperFunction                            UPDATE_COMPLETE
2020-04-28 12:42 AM  LowerFunction                            UPDATE_COMPLETE
2020-04-28 12:43 AM  func1                                    UPDATE_COMPLETE_CLEANUP_IN_PROGRESS
2020-04-28 12:43 AM  func1                                    UPDATE_COMPLETE
Stack finished updating with status: UPDATE_COMPLETE

Output Name                    Value
------------------------------ --------------------------------------------------
upperFuncName                  func1-UpperFunction-5YOIQONAMDLP
lowerFuncName                  func1-LowerFunction-12EQL3CHN0COE

Failing Template: When I move common properties, including CodeUri to the Globals section, it packs the working directory, and deploys this zip, which is unable to be run due to the unexpected locations of the assemblies. As I understand it, this is the behaviour when the CodeUri parameter is empty.

# serverless-global.template.yml

AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"

Globals:
  Function:
    Runtime: dotnetcore3.1
    MemorySize: 256
    Timeout: 30
    CodeUri: ./func

Resources:
  UpperFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: func::func.Function::UpperHandler

  LowerFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: func::func.Function::LowerHandler

Outputs:
  upperFuncName:
    Value: !Ref UpperFunction
  lowerFuncName:
    Value: !Ref LowerFunction

dotnet lambda deploy-serverless --template ./serverless-global.template.yml --stack-name func1-global --s3-bucket MY-DEPLOYMENT-BUCKET
Amazon Lambda Tools for .NET Core applications (4.0.0)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Processing CloudFormation resource UpperFunction
Initiate packaging of . for resource UpperFunction
... zipping:   adding: serverless-global.template.yml (deflated 52%)
... zipping:   adding: aws-lambda-tools-defaults.json (deflated 44%)
... zipping:   adding: .gitignore (deflated 56%)
... zipping:   adding: serverless.template.yml (deflated 60%)
... zipping:   adding: .git/config (deflated 37%)
... zipping:   adding: .git/HEAD (stored 0%)
... zipping:   adding: .git/description (deflated 14%)
... zipping:   adding: .git/index (deflated 28%)
... zipping:   adding: .git/COMMIT_EDITMSG (stored 0%)
... zipping:   adding: func/func.csproj (deflated 48%)
... zipping:   adding: func/Readme.md (deflated 58%)
... zipping:   adding: func/Function.cs (deflated 54%)
... zipping:   adding: .git/info/exclude (deflated 28%)
... zipping:   adding: .git/logs/HEAD (deflated 65%)
... zipping:   adding: .git/hooks/commit-msg.sample (deflated 44%)
... zipping:   adding: .git/hooks/pre-rebase.sample (deflated 59%)
... zipping:   adding: .git/hooks/pre-commit.sample (deflated 45%)
... zipping:   adding: .git/hooks/applypatch-msg.sample (deflated 42%)
... zipping:   adding: .git/hooks/fsmonitor-watchman.sample (deflated 53%)
... zipping:   adding: .git/hooks/pre-receive.sample (deflated 40%)
... zipping:   adding: .git/hooks/prepare-commit-msg.sample (deflated 50%)
... zipping:   adding: .git/hooks/post-update.sample (deflated 27%)
... zipping:   adding: .git/hooks/pre-applypatch.sample (deflated 38%)
... zipping:   adding: .git/hooks/pre-push.sample (deflated 50%)
... zipping:   adding: .git/hooks/update.sample (deflated 68%)
... zipping:   adding: func/obj/func.csproj.nuget.cache (deflated 3%)
... zipping:   adding: func/obj/func.csproj.nuget.g.targets (deflated 32%)
... zipping:   adding: func/obj/func.csproj.nuget.dgspec.json (deflated 70%)
... zipping:   adding: func/obj/func.csproj.nuget.g.props (deflated 60%)
... zipping:   adding: func/obj/project.assets.json (deflated 77%)
... zipping:   adding: .git/objects/61/13bdabd74f5bf184260eb9bd7f5a489911226e (stored 0%)
... zipping:   adding: .git/objects/95/f414ff5cce7309ff9cb27dd94dab0830bab270 (stored 0%)
... zipping:   adding: .git/objects/92/8a4cd6d19e6800f36f07e956ba4f1c9691976f (stored 0%)
... zipping:   adding: .git/objects/3b/bbdb71bc0e74c2cc90404aede62029651a4642 (stored 0%)
... zipping:   adding: .git/objects/32/88b05fd8424c5ec528358638f2723824468ea7 (stored 0%)
... zipping:   adding: .git/objects/69/ddb1640e11c4485f4b4c80eb6e62544f8570e6 (stored 0%)
... zipping:   adding: .git/objects/93/5f953139c3cfaf7c1439594fafa0257ec8b05f (deflated 0%)
... zipping:   adding: .git/objects/ad/d10f08c82566aecba1d1dfbe9de08dd05698ce (stored 0%)
... zipping:   adding: .git/objects/be/f9b534ee19ccd32b000349748c46a01ebc17d7 (stored 0%)
... zipping:   adding: .git/objects/d6/52675dcefbcc751d9f779a1ff988038380af74 (stored 0%)
... zipping:   adding: .git/objects/bc/456d7868bb54ec1809da30e339cd43f0a8a09c (stored 0%)
... zipping:   adding: .git/objects/ab/ac02291b39c4d349cf3de2bca1523e12f095b8 (stored 0%)
... zipping:   adding: .git/objects/ee/b16361d7bf8c8534ffa44093e2a90ed0eeeab7 (stored 0%)
... zipping:   adding: .git/objects/fc/1e01fb731dc457308f60741a368610f5445dab (stored 0%)
... zipping:   adding: .git/objects/e3/aa6c06196214db50eba1b59ede59271a2e6293 (stored 0%)
... zipping:   adding: .git/objects/e4/6512d5c5bc88407902f34d7aa7fcb1630f3715 (stored 0%)
... zipping:   adding: .git/objects/27/fceecefcb773f8d199e2408ae9e3e6bd26c001 (stored 0%)
... zipping:   adding: .git/objects/7d/64e7bf322e4d5093302e3049fd3ecd04846d21 (stored 0%)
... zipping:   adding: .git/objects/1f/45746343b9cda350c34dcf039630ddd7a4d343 (stored 0%)
... zipping:   adding: .git/objects/17/0a2ca0141b19423e0dcae304a68510ebcf75c5 (stored 0%)
... zipping:   adding: .git/objects/10/9b9f53541457d23042b8a68387da20cd3befe9 (stored 0%)
... zipping:   adding: .git/objects/4d/af0eafa8bf34d6fe105d35f5c56ce3c7d26d84 (stored 0%)
... zipping:   adding: .git/objects/2a/90caa47da4eaf610bf871fcc1b1b45dc1f5aa1 (stored 0%)
... zipping:   adding: .git/objects/2a/1cf5c923bb9afe404c7670069dfcc3ff00bc37 (stored 0%)
... zipping:   adding: .git/objects/43/e53a6df789780f473b08ad129a1da98b375341 (stored 0%)
... zipping:   adding: .git/objects/07/6240a9279ce7bc5060d71ddab658d57e5c2e37 (stored 0%)
... zipping:   adding: .git/objects/9a/50debbaeba2ead95d6a05938d36d6332be65df (stored 0%)
... zipping:   adding: .git/objects/09/9c7ceee6dbdfb8e7b33957ee7ac620513d840f (stored 0%)
... zipping:   adding: .git/objects/53/cfaa19b16f3769b2bfc33db3b5c0053c16fdba (stored 0%)
... zipping:   adding: .git/objects/5b/de1ff16059ec8f308c6f5b583e0831a64e90e6 (stored 0%)
... zipping:   adding: .git/objects/6d/c515f2d35337a1eb28416e846dbb9da3c51c0c (stored 0%)
... zipping:   adding: .git/objects/01/082378cac256b7d82807a99600010e7b0874b5 (stored 0%)
... zipping:   adding: .git/objects/b8/299d8fb6835a680d46bc23fb9af22444f1cdac (stored 0%)
... zipping:   adding: .git/objects/ea/bfea1a512a6695f7dc2ed9dc83a908382c7909 (stored 0%)
... zipping:   adding: .git/objects/ea/78b49b39442a624bf94f0b847d8321c694e0c8 (stored 0%)
... zipping:   adding: .git/objects/e9/421767ad25cd3b672f9a08235bcba01cc7fc33 (stored 0%)
... zipping:   adding: .git/objects/f8/db7d9fe6da57288ad0626c8fe220f726de93f8 (stored 0%)
... zipping:   adding: .git/objects/1d/1fa0ce3693c97854d7cf971fc033eeb2154ebf (stored 0%)
... zipping:   adding: .git/objects/1d/50bceb97d306e4423ef523d069a470b24286e5 (stored 0%)
... zipping:   adding: .git/objects/2b/b15e645c3103ef0a7580a99adc2f298689938d (stored 0%)
... zipping:   adding: .git/objects/13/fb40aa6bf562586bcf4c94869818b179b65872 (stored 0%)
... zipping:   adding: .git/refs/heads/master (stored 0%)
... zipping:   adding: .git/logs/refs/heads/master (deflated 28%)
... zipping:   adding: .git/refs/remotes/origin/master (stored 0%)
... zipping:   adding: .git/logs/refs/remotes/origin/master (deflated 27%)
... zipping:   adding: func/obj/Release/netcoreapp3.1/linux-x64/func.dll (deflated 56%)
... zipping:   adding: func/obj/Release/netcoreapp3.1/linux-x64/func.AssemblyInfoInputs.cache (stored 0%)
... zipping:   adding: func/obj/Release/netcoreapp3.1/linux-x64/func.assets.cache (deflated 68%)
... zipping:   adding: func/obj/Release/netcoreapp3.1/linux-x64/func.pdb (deflated 21%)
... zipping:   adding: func/obj/Release/netcoreapp3.1/linux-x64/func.csproj.FileListAbsolute.txt (deflated 83%)
... zipping:   adding: func/obj/Release/netcoreapp3.1/linux-x64/func.AssemblyInfo.cs (deflated 68%)
... zipping:   adding: func/obj/Release/netcoreapp3.1/linux-x64/func.csprojAssemblyReference.cache (deflated 86%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/publish/func.dll (deflated 56%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/publish/func.pdb (deflated 21%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/publish/Amazon.Lambda.Serialization.SystemTextJson.dll (deflated 45%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/publish/func.runtimeconfig.json (deflated 23%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/publish/Amazon.Lambda.Core.dll (deflated 46%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/publish/func.deps.json (deflated 68%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/linux-x64/func.dll (deflated 56%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/linux-x64/func.pdb (deflated 21%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/linux-x64/func.runtimeconfig.dev.json (deflated 31%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/linux-x64/func.runtimeconfig.json (deflated 23%)
... zipping:   adding: func/bin/Release/netcoreapp3.1/linux-x64/func.deps.json (deflated 68%)
Created publish archive (/var/folders/ks/s5_58mcd11g38n170k32yc8cb74n8n/T/UpperFunction-CodeUri-637236316256573380.zip).
Uploading to S3. (Bucket: MY-DEPLOYMENT-BUCKET Key: UpperFunction-CodeUri-637236316256573380-637236316257725000.zip)
... Progress: 86%
... Progress: 100%
Processing CloudFormation resource LowerFunction
Using previous upload artifact s3://MY-DEPLOYMENT-BUCKET/UpperFunction-CodeUri-637236316256573380-637236316257725000.zip for resource LowerFunction
Uploading to S3. (Bucket: xxx-deploy Key: func1-global-serverless-global.template-637236316275336080.yml)
... Progress: 100%
Found existing stack: True
CloudFormation change set created
... Waiting for change set to be reviewed
Initiated CloudFormation stack update on func1-global

Timestamp            Logical Resource Id                      Status
-------------------- ---------------------------------------- ----------------------------------------
2020-04-28 12:47 AM  func1-global                             UPDATE_IN_PROGRESS
2020-04-28 12:47 AM  LowerFunction                            UPDATE_IN_PROGRESS
2020-04-28 12:47 AM  UpperFunction                            UPDATE_IN_PROGRESS
2020-04-28 12:47 AM  LowerFunction                            UPDATE_COMPLETE
2020-04-28 12:47 AM  UpperFunction                            UPDATE_COMPLETE
2020-04-28 12:47 AM  func1-global                             UPDATE_COMPLETE_CLEANUP_IN_PROGRESS
2020-04-28 12:47 AM  func1-global                             UPDATE_COMPLETE
Stack finished updating with status: UPDATE_COMPLETE

Output Name                    Value
------------------------------ --------------------------------------------------
upperFuncName                  func1-global-UpperFunction-BPGGYZQZ7CWH
lowerFuncName                  func1-global-LowerFunction-Y7WTKQ4KZ0EU

Invoking the function gives an error due to not finding files in the expected location

Payload:
{
  "errorType": "LambdaException",
  "errorMessage": "Could not find the required 'func.deps.json'.  This file should be present at the root of the deployment package."
}

drch- avatar Apr 28 '20 12:04 drch-