0
0
AwsHow-ToBeginner · 3 min read

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 MemorySize property in the AWS::Lambda::Function resource.
  • Terraform: Use the memory_size argument in the aws_lambda_function resource.
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 MethodParameterValid RangeNotes
AWS CLImemory-size128 - 10240 MBUse update-function-configuration command
CloudFormationMemorySize128 - 10240 MBSet in AWS::Lambda::Function resource
Terraformmemory_size128 - 10240 MBSet 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.