How to Set AWS Lambda Memory Size Easily
You set the memory size for an AWS Lambda function by specifying the
MemorySize property in megabytes (MB). This can be done via the AWS Management Console, AWS CLI with the update-function-configuration command, or Infrastructure as Code tools like AWS CloudFormation or Terraform.Syntax
The memory size for a Lambda function is set using the MemorySize parameter, which accepts values between 128 MB and 10,240 MB (10 GB) in 1 MB increments. Increasing memory also increases CPU power proportionally.
Here are common ways to set it:
- AWS CLI: Use
aws lambda update-function-configuration --function-name <name> --memory-size <MB> - CloudFormation: Use the
MemorySizeproperty in theAWS::Lambda::Functionresource. - Terraform: Use the
memory_sizeargument in theaws_lambda_functionresource.
bash
aws lambda update-function-configuration --function-name MyFunction --memory-size 512
Example
This example shows how to set the memory size to 512 MB for a Lambda function using AWS CLI and Terraform.
bash and terraform
### AWS CLI example aws lambda update-function-configuration --function-name MyFunction --memory-size 512 ### Terraform example resource "aws_lambda_function" "example" { function_name = "MyFunction" handler = "index.handler" runtime = "nodejs18.x" role = aws_iam_role.lambda_exec.arn memory_size = 512 filename = "lambda_function_payload.zip" } resource "aws_iam_role" "lambda_exec" { name = "lambda_exec_role" assume_role_policy = jsonencode({ Version = "2012-10-17", Statement = [{ Action = "sts:AssumeRole", Effect = "Allow", Principal = { Service = "lambda.amazonaws.com" } }] }) }
Output
AWS CLI: Updates Lambda function memory to 512 MB.
Terraform: Creates Lambda function with 512 MB memory.
Common Pitfalls
Common mistakes when setting Lambda memory include:
- Setting memory below 128 MB or above 10,240 MB, which is invalid.
- Forgetting to redeploy or update the function after changing memory.
- Not considering that increasing memory also increases CPU power and cost.
- Using inconsistent memory settings across environments causing unexpected performance.
bash
### Wrong: Setting memory below allowed minimum aws lambda update-function-configuration --function-name MyFunction --memory-size 64 ### Right: Setting memory within allowed range aws lambda update-function-configuration --function-name MyFunction --memory-size 256
Output
Error: Memory size must be between 128 MB and 10240 MB.
Success: Memory size updated to 256 MB.
Quick Reference
| Setting Method | Parameter | Valid Range | Notes |
|---|---|---|---|
| AWS CLI | memory-size | 128 - 10240 MB | Use update-function-configuration command |
| CloudFormation | MemorySize | 128 - 10240 MB | Set in AWS::Lambda::Function resource |
| Terraform | memory_size | 128 - 10240 MB | Set in aws_lambda_function resource |
Key Takeaways
Set Lambda memory using the MemorySize parameter between 128 MB and 10,240 MB.
Use AWS CLI, CloudFormation, or Terraform to configure memory size.
Increasing memory also increases CPU power and cost proportionally.
Always redeploy or update your Lambda function after changing memory.
Avoid setting memory outside the allowed range to prevent errors.