sam build of 12 lambdas generates 12 dirs with 12 lambdas in .aws-sam/build dir

0

I've written nodejs18.x SAM app that has 12 lambdas, at endpoints on a REST ApiGateway. I'm optimizing the code and I've moved common code and node-modules into layers. I was looking at the .aws-sam/build directory and I was puzzled to see that all my lambdas had their own dir and inside each dir were ALL the lambdas again. So I'm worried that means loading one lambda, loads all 12 for th execution of one lambda.

I can put each lambda in it's own dir (package.json), but that seems kinda clunky.

Am I missing some basic SAM knowledge?

asked 7 months ago244 views
2 Answers
1

I was puzzled to see that all my lambdas had their own dir and inside each dir were ALL the lambdas again.

sam build command will build each of the Lambda and store the output in .aws-sam/build. After that, you have a Lambda source, and a built Lambda code.

So I'm worried that means loading one lambda, loads all 12 for th execution of one lambda.

If you deploy those Lambda code to AWS Lambda (e.g., sam deploy), each Lambda code goes to separate runtime environment, and they cannot even locate each other. Therefore, loading one Lambda function will not make other Lambda being loaded.

Hope it helps.

profile picture
HS
answered 7 months ago
  • Thanks for responding. Regarding the first part of your response about 'build'; So the size of the built artifact is the size of just the single function and not the size of all twelve shown in that same dir. So if there are three function being built funcA, funcB, and funcC, and after building it looks like this >
    .aws-sam (dir) funcA (dir) funcA.js funcB.js funcC.js funcB (dir) funcA.js funcB.js funcC.js funcC (dir) funcA.js funcB.js funcC.js Each of the three function's size doesn't include the other two func in the dir?

  • Each of the three function's size doesn't include the other two func in the dir?

    No, it doesn't. This directory tree lies only on your local machine. After you deploy, there is no .aws-sam that contains all the artifacts in the Lambda runtime. Instead, it will create 12 Lambda functions and each has the similar directory tree as follows:

    /var/task/
    ├── funcA.js
    ├── funcB.js
    └── funcC.js
0

For my own peace-of-mind, I've created a new Cloud9 environment with 12 dirs, one for each function and the C9 env with all 12 in one dir. There is a corresponding deployed package size difference. So for now, I'm going to put dynamodb read (reads get 1M x more use a day, than puts) lambdas in their own dirs.

This is all new to me, so any insights are appreciated. And thanks again HS for your feedback.

answered 7 months ago
  • HS, as I just posted the above comment, I saw your latest post. So, in answer to you latest post. WRT the all 12 in one dir, when I post these 12 lambdas and then look at one of them in the Lambda console, there is the dir of the same name of the particular lambda, but there are twelve lambdas files in the 'Code' tab and when I open any one of them I see their code. Plus the package size is about 12 times larger.

    Since I'm new to all this, I believe you are right and I'm also right, BUT I'm missing some template.yaml directive that is causing my issue.

    One other factor, until I put the dynamodb node modules in a layer, this is what I'd see anytime I tried to look at my deployed code in the Lambda Console > "The deployment package of your Lambda function "sam-att-v1-5-postEventFunction-RJkSwRTCGoPP" is too large to enable inline code editing. However, you can still invoke your function." So only after I introduced layers did I see the lambdas showing up in each other's directories.

    So until I get more experience, I'm putting all lambdas that read dynamodb into their own dirs.

    Thanks for your support.

  • Did you specify the CodeUri properly? For example, if you store 12 Lambda in src, the following will deploy a Lambda function that contains all the 12 Lambda codes.

      FunctionA:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: src

    Instead, you need to use this.

      FunctionA:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: src/funcA
    
      FunctionB:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: src/funcB

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions