Skip to content
Codesavy
FacebookTwitter

How to fix error FUNCTION_ERROR_INIT_FAILURE

Code, Serverless, Amazon Web Services, Lambda2 min read

When does this happen?

The error mentioned above happens when you enable the AWS Lambda feature provisioned concurrency which in turn makes your lambda applications faster and more responsive since it can prevent cold starts to a certain degree.

What does this error mean?

This plainly means that there's something wrong with the lambda function deployed, so the feature "provisioned concurrency" isn't able to initialize or run the function.

How do we solve this problem?

We need to identify what caused the function to fail. There are a couple of ways to achieve this:

  1. Fix issues introduced at build-time

    • Make sure that your code is being compiled properly and you don't deploy a broken build into AWS Lambda
  2. Debug AWS Lambda run-time issues

    • You can easily do this by deploying your lambda with DISABLED "provisioned concurrency" feature
    • This will allow your app to run in normal circumstances and make the actual error appear.
    • The most common run-time issues are:
      • Missing IAM permission to access RDS, S3, DynamoDB, Secrets, SNS & other AWS Services
      • Type error(bad code)
      • Build issue - required module missing in bundle
      • Missing ENV variables

How can we prevent FUNCTION_ERROR_INIT_FAILURE from happening?

  1. Set-up a local development environment where you can test your Lambda function

    • It's best practice to have a local development set-up for your AWS Lambda code to have a faster development cycle and debug issues faster like the one we are facing here.
    • You can use tools like serverless offline or localstack to have your AWS Lambda application running locally.
  2. Write Unit Tests

    • FUNCTION_ERROR_INIT_FAILURE may be prevented by writing unit tests to make sure that the application is running correctly.
  3. Use AWS Services locally & Simulate your Lambda's permissions

    • If you have a local development environment, you can catch permission issues early by applying the same IAM role to your local app
  4. Set-up a staging/development environment in AWS

    • This environment should have a DISABLED "provisioned concurrency" so you can catch errors early on.

How do we use the provisioned concurrency feature?

One way to use this feature is via the Serverless Framework with your function looking something like this:

serverless.yml
functions:
graphqlPrivate:
handler: src/graphql/private/index.handler
memorySize: 1024
timeout: 60
name: ${self:service}-gqlPrivate-${self:provider.stage}
vpc: ${file(./serverless/vpc.yml)}
events:
- http:
path: /graphql/private
method: ANY
cors: true
authorizer:
arn: "arn:aws:cognito-idp:#{AWS::Region}:#{AWS::AccountId}:userpool/${self:custom.cognitoArns.private.${self:provider.stage}}"
provisionedConcurrency: 10 # This line will enable the feature

After using this nice Lambda feature, We may encounter the FUNCTION_ERROR_INIT_FAILURE error which doesn't provide much information and makes it tough to debug.

© 2023 by Codesavy. All rights reserved.
Made with 💚