Skip to content

Deploying Amplify Code to AWS using amplify push resulting in package size error

1

I am trying to push my development code to amplify using command amplify push. It worked until today when i made some minor changes to a nodejs function to add a small snippet of around 30 lines. No dependency was added to the code or any directory. Below attached is the error I see on terminal.

Resource is not in the state stackUpdateComplete Name: LambdaFunction (AWS::Lambda::Function), Event Type: update, Reason: Resource handler returned message: "Unzipped size must be smaller than 262144000 bytes (Service: Lambda, Status Code: 400, Request ID: 7ce89e3f-de9c-404a-a2c7-998e8a3f6b6a)" (RequestToken: 78c37b55-45c8-115f-6733-6d3a0cbc4e16, HandlerErrorCode: InvalidRequest), IsCustomResource: false

Does anyone know or anyone faced similar issue?

asked a year ago356 views
1 Answer
0

Greeting

Hi Mark,

Thanks for sharing your question! It looks like you’re experiencing a package size issue while using amplify push to deploy your Node.js function. I’d be happy to help walk you through some strategies to resolve this. Let’s dive into the details. 😊


Clarifying the Issue

From what you’ve described, you made a small update to your Node.js function (around 30 lines of code), and now the deployment using amplify push fails with a package size error. The error message suggests that the unzipped size of your Lambda function exceeds the limit of 262,144,000 bytes (250 MB). While no dependencies were added, it’s possible that either additional files in your directory are being included unintentionally, or your current dependencies are large enough to push you over the limit after the changes.

Even small updates to code can sometimes cause unintended increases in package size, especially if unrelated files are unintentionally included during deployment. Let’s figure out a solution that keeps your deployment efficient and within AWS’s size constraints.


Key Terms

  • Amplify Push: A CLI command used to deploy updates made in AWS Amplify projects, including functions, hosting, and APIs.
  • AWS Lambda Size Limit: AWS Lambda functions have an unzipped deployment package size limit of 250 MB. This includes all code and dependencies.
  • Lambda Layers: A mechanism to share and manage common code or libraries across multiple Lambda functions, allowing smaller deployment packages.

The Solution (Our Recipe)

Steps at a Glance:

  1. Check for large files in your project directory.
  2. Exclude unnecessary files using the amplify.ignore file.
  3. Optimize dependencies or externalize them using Lambda Layers.
  4. Test your changes locally to ensure functionality.
  5. Redeploy with amplify push.

Step-by-Step Guide:

  1. Check for large files in your project directory
    • Run the following command to identify any large files or directories:
      du -h --max-depth=1
      Look for directories like node_modules or other files that could be inflating the size.

  1. Exclude unnecessary files using the amplify.ignore file
    • The amplify.ignore file works like .gitignore and can exclude files from deployment. Update your amplify.ignore file to exclude logs, test files, and other unneeded items:
      *.log
      node_modules/
      .DS_Store
      tests/
      Save the file in the root of your project directory.

  1. Optimize dependencies or externalize them using Lambda Layers
    • If your node_modules directory is the issue, identify unused or redundant packages using a package analyzer:

      npm prune --production
      npm install --only=prod
    • If certain dependencies are used across multiple functions, move them to a Lambda Layer:

      • Create a layer in Amplify:
        amplify add function
        Select "Lambda Layer" and specify which dependencies to include.
    • Modify your function to reference the layer instead of bundling dependencies directly.


  1. Test your changes locally to ensure functionality
    • Before deploying, test your updated function to confirm it works correctly. Use the Amplify mock feature or a local Node.js environment to validate your changes:
      amplify mock function

  1. Redeploy with amplify push
    • Once you’ve reduced the package size, redeploy using the command:
      amplify push
      Monitor the output to ensure the deployment completes successfully.

Closing Thoughts

If you continue to encounter issues, double-check your project setup and consider breaking down your code into smaller, modular functions. For further guidance, you can refer to the following AWS documentation:

Let us know how it goes, and feel free to reach out if you have additional questions or run into any roadblocks!


Farewell

Best of luck with your deployment, Mark! I’m confident these steps will help you resolve the issue and get your project running smoothly again. Let me know if you have more questions or need additional help. 🚀


Cheers,

Aaron 😊

answered 10 months ago

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.