AWS: The maximum compressed size of your Lambda function cannot exceed 1 MB for viewer events

1.1k views Asked by At

I've created a lambda function with SAM. Extremely small. It doesn't actually do anything really, I've deployed it just for testing.

This is the handler (Nodejs):

exports.lambdaHandler = async (event, context, callback) => {
    try {

        const request = event.Records[0].cf.request;

        console.log(`Request log: ${JSON.stringify(request)}`)

        callback(null, request);
    } catch (err) {
        console.log(err);
        return err;
    }

    return response
};

Then, I have this template.yaml:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  simple lamdga test
  
Globals:
  Function:
    Timeout: 3

Resources:

  LambdaEdgeFunctionRole:
    Type: "AWS::IAM::Role"
    Properties:
      Path: '/'
      ManagedPolicyArns:
        - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          -
            Sid: "AllowLambdaServiceToAssumeRole"
            Effect: "Allow"
            Action: 
              - "sts:AssumeRole"
            Principal:
              Service: 
                - "lambda.amazonaws.com"
                - "edgelambda.amazonaws.com"

  TestLambda:
      Type: AWS::Serverless::Function
      Properties:
        CodeUri: lambdaTest/
        Description: 'test func'
        Handler: app.lambdaHandler
        Runtime: nodejs12.x
        MemorySize: 128
        Timeout: 1
        Role: !GetAtt LambdaEdgeFunctionRole.Arn
        AutoPublishAlias: live

testing the function with SAM local invoke... works great.

I deploy it and I can see the size in the S3 bucket is 1.2MB. When I go to the lambda console in AWS I then try to add the CloudFront trigger I need, but, I get this error:

enter image description here

How can I get over this error? I would think a function so minimal should be easily allowed to the deployed as a lambda edge function.

How can I get over this error?

This is what I've learn as of now:

I went in S3 and added a .zip extension to the package SAM uploaded, to then be able to unzip it and it shows this:

enter image description here

So, it looks like the node_modules alone are 2.8 Mb. I'm trying to figure out how to tell SAM not to include node_modules and any unnecessary content.

So, if I go in and delete the dependencies from the package.json file and leave them like this:

...
  "devDependencies": {
    
  },
  "optionalDependencies": {

  }
...

it works. I am able to create the package and deploy to lambda@edge. The package is about 1K, super small. The function then works once connected to a cloudfront trigger.

What is the proper way to make sure those dependencies do not get included in the package without deleting them manually?

0

There are 0 answers