Lambda Count Words: How It Works & Limits
- Approx. invocations (1 MB code)
- Result
- Code size
- 1 MB
- You entered
- Payload size
- 0.1 MB
- You entered
This page explains how AWS Lambda counts words in your function code and event payloads, and what that means for your serverless applications. You enter your code size and payload size, and the calculator returns the total billable characters and estimated invocation cost. The answer is simple: Lambda counts words as characters, not as whitespace-separated tokens, and the main limit is 6 MB for a synchronous invocation payload and 50 MB for asynchronous.
Formula
The total number of characters Lambda processes is the sum of your code size and your event payload size, both converted to bytes and then to characters. Since Lambda stores code and payloads as UTF-8, one character is usually one byte, but multi-byte characters (like emoji) count as more.
Total characters = (Code size in MB × 1,048,576) + (Payload size in MB × 1,048,576)
Variables:
- Code size – the size of your deployment package (ZIP file) in MB.
- Payload size – the size of the event JSON sent to your function in MB.
- 1,048,576 – bytes in a megabyte (1024 × 1024).
Worked Example
Using the defaults: code size = 1 MB, payload size = 0.1 MB.
- Convert code size to bytes: 1 × 1,048,576 = 1,048,576 bytes.
- Convert payload size to bytes: 0.1 × 1,048,576 = 104,857.6 bytes.
- Add them: 1,048,576 + 104,857.6 = 1,153,433.6 bytes.
- Assuming 1 byte per character, total characters = 1,153,434 (rounded).
The calculator shows 1,153,434 characters, matching the formula.
Why “Words” Matter in Lambda
Lambda doesn’t have a “word count” feature. When people ask “Lambda count words?”, they usually mean one of three things:
- How many characters are in my function code or event payload?
- How does Lambda’s pricing count requests and duration?
- Can Lambda process text and count words in a document?
The first is a limit question, the second is a cost question, and the third is a programming task. This article addresses the first, but the other two are covered below.
Lambda Limits: Code and Payload Sizes
AWS Lambda has hard limits that affect how much text you can send in one invocation:
- Deployment package size: 50 MB (ZIP compressed) for direct upload, 250 MB for container images.
- Request payload (event) size: 6 MB for synchronous invocations (API Gateway, SDK), 256 KB for asynchronous invocations (S3, SNS).
- Response payload size: 6 MB for synchronous, 256 KB for asynchronous.
These limits are in bytes, not words. A typical English word is about 5 characters plus a space, so a 6 MB payload holds roughly 1.2 million words, but that’s a rough estimate. Non-English text or emoji can be larger.
How Lambda Actually Counts Text
When you invoke a Lambda function, the event is a JSON string. Lambda parses it as UTF-8. Each character in the JSON (including keys, quotes, braces, and spaces) counts toward the payload size. For example, {"text":"hello"} is 16 characters, not 1 word.
If you need to count words inside your function, you write code that splits the string on whitespace. For instance, in Python:
def lambda_handler(event, context):
text = event.get('text', '')
words = text.split()
return {'word_count': len(words)}
That code counts whitespace-separated tokens, but it doesn’t affect Lambda’s own limits. The limit is always the byte size of the payload, not the word count.
Common Mistakes and Misconceptions
- Mistake: Assuming 1 word = 1 byte. A word like “hello” is 5 bytes, and “héllo” is 6 bytes (é is 2 bytes).
- Mistake: Forgetting that JSON includes structural characters. A large array of numbers can be much bigger than the numbers alone.
- Mistake: Mixing up synchronous and asynchronous limits. A 10 MB event will fail asynchronously but succeed synchronously (if under 6 MB? No, 10 MB is over 6 MB, so it fails both).
- Misconception: Lambda counts words for billing. Billing is based on requests and duration (GB-seconds), not on text size.
How to Choose the Right Payload Size
If you’re sending large text to Lambda, consider these options:
- Compress the payload (e.g., gzip) and decompress inside the function.
- Store the text in S3 and pass the S3 key as the event.
- Split the text into multiple invocations.
- Use a streaming response if you’re returning large text.
For code size, keep your deployment package small. Use layers for dependencies, and avoid bundling unnecessary libraries. A 1 MB code package is typical for a simple function; a 50 MB package is near the limit.
Frequently Asked Questions
Does Lambda charge per word?
No. Lambda charges per request (every 1 million requests) and per duration (GB-seconds, based on memory allocated). The size of your code or payload doesn’t affect the price directly, but larger payloads may increase execution time, which can increase duration cost.
What happens if my payload exceeds 6 MB?
For synchronous invocations, Lambda returns a 413 Request Entity Too Large error. For asynchronous invocations, the limit is 256 KB, and exceeding it causes the invocation to fail. You must reduce the payload size or use an alternative like S3.
How many words can I put in a Lambda event?
Roughly, a 6 MB payload can hold about 1.2 million English words, assuming 5 characters per word plus a space. But that’s before JSON overhead. A safer estimate is 800,000–1,000,000 words for a simple JSON structure.
Can Lambda count words in a document?
Yes, but you must write the code yourself. Lambda doesn’t have a built-in word counter. You can use standard string functions in any runtime (Python, Node.js, etc.) to split text and count tokens.
Does the code size include dependencies?
Yes, the deployment package size includes all your code, any layers, and dependencies. The limit is 50 MB for ZIP uploads (uncompressed size is also limited to 250 MB for layers).
Is there a way to see the exact byte count of my payload?
Yes, you can check the Content-Length header when sending a request, or in your function, you can use len(event) in Python or JSON.stringify(event).length in Node.js to get the character count (which approximates bytes for ASCII).
Related
Formula used
in code_size | Code size | MB | 1 | 0 in payload_size | Payload size | MB | 0.1 | 0 out total_chars | Total characters | chars | (code_size * 1048576) + (payload_size * 1048576) | 0 out invocations | Approx. invocations (1 MB code) | - | 1 / code_size | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.