Search code examples
amazon-web-servicesaws-lambdaaws-cloudformationserverless-framework

Serverless - Cloudformation - Lambda event creation error


I was trying to create an s3:put event on one of the existing lambda functions and S3 buckets using serverless.

But was getting this error from AWS CloudFormation even though the lambda function doesn't use nodejs12.x. One note - the lambda is old, so it is using python3.7, which might be EOL soon, but we can't upgrade the version just yet due to dependencies on python3.7

Resource handler returned message: "The runtime parameter of nodejs12.x is no longer supported for creating or updating AWS Lambda functions. We recommend you use the new runtime (nodejs18.x) while creating or updating functions. (Service: Lambda, Status Code: 400, Request ID:)

On CloudFormation, the failure is on this resource creation CustomDashresourceDashexistingDashs3LambdaFunction with the same error as above.

This is the serverless code I was using -

provider:
  name: aws
  runtime: python3.7
 .....
functions:
  antivirus-scan:
    handler: src/scan.lambda_handler
    awsKmsKeyArn: ${opt:kms_arn, ""}
    role: antivirusFunctionRole
    memorySize: 2048
    timeout: 300  # 5mins
    events:
      - s3:
          bucket: ${self:custom.antivirus_files_bucket}
          event: s3:ObjectCreated:*
          existing: true

I tried removing the event and recreating it, which doesn't work. The S3 bucket is created outside of the CloudFormation template.


Solution

  • I found the cause, serverless v2 is deprecated https://github.com/serverless/serverless/issues/11400.

    Need to upgrade to v3.

    When the parameter existing: true is set on bucket events, serverless creates a lambda function (Custom S3 Resources) which creates the event on the original lambda. See using existing buckets. This lambda which is created runs on a deprecated runtime (Node 12) in Serverless 2.