question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Functions are bloated

See original GitHub issue

Description:

Runing sam build produces .aws-sam/build which has pretty much everything in it, and when you do sam deploy it deploys the entire source code into the function itself, resulting in a bloated function and slow deployments.

I got around the bloated sam build by writing my own builder, I learned that sam build is actually not required by sam deploy as long as the template.yaml and samconfig.toml are on the same directory as the source codes.

My folder architecture looks like this:

/ root
| - ...lots of other unrelated files
| - src
|   | - dependencies <---- this is a layer AWS::Serverless::LayerVersion
|   | - functions <---- this is where all my functions are
|   |    | - ...sub-folders containing all functions

Before:

I run the command sam build && sam deploy to deploy, after sam build my tree would look like this:

/ root
| - ...lots of other unrelated files
| - .aws-sam
|    | - build
|    |    | - login <---- this is one of my functions defined in template.yaml
|    |    |    | - login.js
|    |    |    | - ... all other things that the function does not really need <--- this is the problem
|    |    |    | - dependencies <---- even this layer was somehow added here
|    | - build.toml
| - src
|    | - dependencies <---- this is a layer AWS::Serverless::LayerVersion
|    | - functions <---- this is where all my functions are
|    |    | - ...sub-folders containing all functions

Now I don’t run sam build anymore, I run my own build and the build now looks like this:

/ root
| - ...lots of other unrelated files
| - build
|    | - login.js
|    | - ...allOthers.js <---- all of them are  just JS files
|    | - dependencies <---- the layer
|    | - template.yaml
| - src
|    | - dependencies <---- this is a layer AWS::Serverless::LayerVersion
|    | - functions <---- this is where all my functions are
|    |    | - ...sub-folders containing all functions

Now I jus do cd build && sam deploy.

The catch is, It’s still bloated because sam will package the entire build/ folder and make that the base of the function, so when I download the login function, it would look like this:

/ root
| - login.js
| - ...allOthers.js <---- all of them are  just JS files
| - dependencies <---- the layer
| - template.yaml

When in reality the function is just the JS code itself, like login.js, and nothing else, so it SHOULD look like this:

/ root
| - login.js
| - template.yaml

FYI: The layer is a typical NodeJS layer.

/ dependencies
| - nodejs
|    | - package.json
|    | - node_modules
|    | - yarn.lock
|    | - ...all other utilities being used by the functions themselves

The convention is that the functions are self-contained, any dependencies they need MUST BE pull from the dependencies layer, hence the name.

The deployment time was also reduced by half, what was previously 10 minutes, is now just 5 minutes.

Steps to reproduce:

  • sam build
  • sam deploy
  • Go to the function and download it, you’ll see that it contains the entire project itself, or whatever is in the files of package.json.

Observed result:

Functions are bloated.

Expected result:

Functions SHOULD NOT BE BLOATED. It should only contain the actual function itself, i.e., login.js

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: N/A
  2. sam --version: SAM CLI, version 1.17.0
  3. AWS region: N/A

Add --debug flag to command you are running

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Envekcommented, Feb 19, 2021

You either can do it manually with Makefile-based builder:

# Makefile
build-LoginFunction:
	cp login.js "$(ARTIFACTS_DIR)/"
build-RegisterFunction:
	cp register.js "$(ARTIFACTS_DIR)/"

or you can use TypeScript feature to transpile every handler separately and TypeScript will build only this file and files on which it depends (imports into itself). See “Bonus: Put only relevant code into your Lambdas functions” section at the end of my article for details on how to do that.

1reaction
Envekcommented, Feb 22, 2021

Wow, great! I’m super glad about such an improvement (we have much smaller set of functions).

However, there is still plenty of space for improvement. For example, current Makefile isn’t optimal and have a lot of duuplications (e.g. npm install is repeated for every function to install development dependencies).

If you have any ideas on how to improve it further, please open issue or pull request in my example app or my cookiecutter template.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bloated Stomach: Causes, Tips to Reduce & When to be ...
A bloated stomach feels tight, full and often painful. You might feel bloated even if you don't have a distended abdomen.
Read more >
Bloating 101: Why You Feel Bloated - WebMD
Bloating can also be caused by impaired muscle function in the digestive tract. When muscles that normally move food along don't work ...
Read more >
Why Do I Feel Bloated? Common Causes of Bloating
These include: An imbalance of microorganisms in your bowel, which can be the result of taking antibiotics.
Read more >
Understanding Bloating and Distension - IFFGD
There is also something called functional bloating, which is fullness and or/distension of the abdomen, not associated with changes in bowel movements.
Read more >
What Is the Feature Bloat and How Can You Effectively Get ...
Feature bloat, also known as feature creep, feature fatigue, or software bloat, is having more than necessary features in your product. This ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found