Home > Back-end >  How to launch an AWS Lambda Function locally on my computer?
How to launch an AWS Lambda Function locally on my computer?

Time:12-08

Our development team has created several AWS Lambdas Functions written in Java that are currently running on the cloud.

I have imported them locally on my computers.

They are all Maven projects.

I have already installed both the JDK and Maven.

I do not know what is the next step for launching them locally.

Is there any client that I should install before?

CodePudding user response:

Lambda function handlers have a very specific interface, typically with event and context parameters. The shape of the event parameter depends on the source of the event that triggered the Lambda function (here's an SQS example).

If you want to test a Lambda function locally then you would typically start from a local development environment that uses project tooling such as AWS SAM or Serverless Framework. Those tools typically support some way of configuring events and invoking a Lambda function locally with those events. This allows local development, debugging, and testing.

If you can't use one of those tools, then you can typically simply wrap your Lambda function code in some custom-written wrapper code so that you can run it locally, but you'd have to write code to invoke the Lambda function handler, providing the relevant event and context parameters. Typically the context parameter is not all that important for local development.

Unfortunately, there's no specific documentation for this path (taking a native Lambda function and moving it to a local development environment). This isn't a supported path (from a cloud deployment backwards to an on-prem dev environment), but it should be possible. If these Lambda functions are deployed as Docker container images then you may be in better shape.

You'll need to understand how your Lambda functions are structured, what their expected inputs are (context and event), and then provide a Java wrapper for them to run locally. You might also find that they have been written with dependencies on the Lambda runtime environment, e.g. the presence of /tmp directory or specific environment variables or an IAM role with specific IAM permissions, so account for that when attempting to run locally.

So, I think you can do this but it's not trivial. It's generally a better path to start from local using SAM or Serverless, however with some effort you might also be able to retrofit your Lambda functions into that local tooling.

CodePudding user response:

There is no chance to start a Lambda function just like normal Java method... You should use some of the AWS functionalities.

First of all, you should be careful when testing AWS Lambda function locally, because as you probably know AWS Lambda has read-only file system and of course you don't have those restriction on your local environment.

So if your workflow create files or write to a files, locally you will not have any problems, but when you deploy your function on AWS, the same operations will start failing with "Permission denied" errors.

For deployment you must use Docker image(Up to 10GB) or zip archive(Up to 250MB). The better approach is to using a Docker image.

Regarding that, here is described how to test you function locally:

https://docs.aws.amazon.com/lambda/latest/dg/images-test.html

The easiest thing for testing is something called - Lambda function URL. The above link is for Docker image/Lambda function URL.

Hope that helps.

  • Related