just installed aws-iot-device-sdk-v2
into my lambda cluster. I am able to install the package just fine into the project. Unfortunately, I got the error An error occurred: exampleLambda - Resource handler returned message: "Unzipped size must be smaller than 262144000 bytes (Service: Lambda, Status Code: 400, Request ID: ...
Looking at serverless, I see that the inclusion increased my package size from 36MB too 92MB. This doesn't seem right at all. Our team has installed over one hundred packages but none of them have increased our deployed memory size this much. Is there some work-around for this problem?

- 592
- 4
- 9
- 19
4 Answers
Yes, there is a limitation of 250 MB of code and dependencies when it is unzipped. We do provide a zipped file of code along with dependencies, but behind the scene it gets unzipped.
Best practice: Keep the lambda size small as possible and orchestrate as per flow rather than doing everything in one Lambda. Large size lambda do affect the initialization (cold start)
Workaround:
- Use minified dependencies while packaging. Use packaging tool/utilities while packaging the project.
- Try removing unwanted dependencies if not being used.
- If assets are used and are huge, put them in S3 and use them instead of putting them in package.
If none of this work, keep the large dependencies in S3 and load it to /tmp directory and the use it. This is not a good solution, but will work. I have used it for setting up a selenium dependency in Java.
Also, I am open for other solutions from the community. Thanks!

- 555
- 5
- 11

- 591
- 2
- 5
- 13
I had such issues and I realized that I was using a different NodeJS version to do deployment/build other than the one used in the application/during development/installation
NB:
This is own solution I received from the same problem faced. Only applies to applications using NodeJS.

- 897
- 1
- 12
- 19
-
1Thanks for pointing this out. I was having the same issue, where I could only deploy by excluding my `node_modules` but obviously my app was not pulling them. Using `nvm` to switch my local node version to the one in my `serverless.yml` fixed it. – David Azar Jun 16 '22 at 23:03
Couple of things I would suggest trying:
- The
aws-iot-device-sdk-v2
direct dependencyaws-crt
written in C to be cross-platform, you can use script to remove binary for unused platform fromnode_modules/aws-crt/dist/bin
. - If you are using serverless.com to package the lambda there is a plugin that could also help to reduce the package size, use it combine with 1 could further reduce the package size.
aws-iot-device-sdk-v2
dependency can move to Lambda Layer that will reduce the lambda package size.

- 951
- 2
- 12
- 33

- 151
- 5
Looking at your issue from another angle, I would question the need to use a device SDK from inside a AWS Lambda function. A device sdk is normally used to establish a bidirectional connection between a client and AWS IoT Core to publish and receive MQTT messages. It involves a TCP/TLS or Websocket connection which need to be re-established at every cold start. This is an anti-pattern for a Lambda function, especially with an high message volume.
When using Lambda functions you should publish directly to AWS IoT Core using the AWS SDK: for Javascript it would be IoT Data Plane Client.
In order to process MQTT messages via Lambda functions, you would instead rely on Rules for AWS IoT to relay the messages directly to the processing Lambda function, or, for added reliability and scalability, you can put the message into an Amazon SQS queue or an Amazon Kinesis data stream using the corresponding rule actions and configure them as event sources for your Lambda function.

- 11
- 1