Skip to content

Lambda Default Memory Size

Level: Error

Initial version: 0.1.8

cfn-lint: ES1005

tflint: aws_lambda_function_default_memory

Lambda allocates CPU power in proportion to the amount of memory configured. By default, your functions have 128 MB of memory allocated. You can increase that value up to 10 GB. With more CPU resources, your Lambda function's duration might decrease.

You can use tools such as AWS Lambda Power Tuning to test your function at different memory settings to find the one that matches your cost and performance requirements the best.

Implementations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const myFunction = new Function(
      scope, 'MyFunction',
      {
        code: Code.fromAsset('src/hello/'),
        handler: 'main.handler',
        runtime: Runtime.PYTHON_3_8,
        // Change the function memory size
        memorySize: 2048,
      }
    );
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
  "Resources": {
    "MyFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        // Required properties
        "CodeUri": ".",
        "Runtime": "python3.8",
        "Handler": "main.handler",

        // Change the function memory size
        "MemorySize": 2048
      }
    }
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      # Required properties
      CodeUri: .
      Runtime: python3.8
      Handler: main.handler

      # Change the function memory size
      MemorySize: 2048
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
provider:
  name: aws
  # Change the memory size across all functions
  memorySize: 2048

functions:
  hello:
    handler: handler.hello
    # Change the memory size for one function
    memorySize: 512
1
2
3
4
5
6
7
8
9
resource "aws_lambda_function" "this" {
  function_name = "my-function"
  runtime       = "python3.8"
  handler       = "main.handler"
  filename      = "function.zip"

  # Change the default memory size value
  memory_size = 2048
}

See also