From your Local to the Cloud
Building software is one of the most rewarding creative activities you can do. One the reasons I love building tech solutions is that you can create awesome stuff with little resources. But building is not always straightforward. The process of creating a great piece of software involves testing, a lot.
In order to build something that solves a real problem and becomes robust, it needs to go through a lot of changes, adjustments, improvements and tuning. For this reason, it is crucial to have a quick and easy way to make changes and see the result as fast as possible. Developing locally reduces this feedback loop and you can iterate faster.
Now enter AWS Lambda. Suddenly, testing locally isn't so simple anymore. Your code is running in the cloud, inside an AWS-managed environment that you don't control. And if your Lambda interacts with other AWS services like S3, DynamoDB, or API Gateway, things get even trickier.
I ran into this problem recently while working on a project, and I had to dig deep to find a good local testing approach. If you're struggling with this too, let me save you some time and share what I've learned.
So, How Do You Test AWS Lambda Locally?
Option 1: AWS SAM – The Official Way
My first stop was AWS SAM (Serverless Application Model), which is AWS's official tool for local development. With SAM, you can invoke a Lambda function on your machine using sam local invoke, or even spin up a local API Gateway using sam local start-api. This means you can test API calls before deploying to AWS, which is super useful.
But there's a catch. Since SAM relies on Docker to simulate the AWS environment, it can feel a bit heavy at times. Plus, setting it up can take a while if you're new to it. Still, if you want an AWS-supported way to test locally, SAM is a solid option.
If you are interested in learning more about this approach, I would recommend to watch this awesome tutorial, where Jonathan goes through the whole development and deployment cycle.
Option 2: LocalStack – Running AWS on Your Machine
Then I came across LocalStack, which is basically a fully emulated AWS environment that runs entirely on your local machine. This approach is quite interesting. If your Lambda function interacts with S3, DynamoDB, or SQS, you can test everything without even touching AWS.
I spun up LocalStack, deployed my Lambda, and boom! my function was running locally as if it were in the cloud. But again, there's a tradeoff. The free version has some limitations, and running a full AWS-like environment locally can be resource-intensive.
Option 3: AWS Lambda Runtime Interface Emulator
Another option I explored was the AWS Lambda Runtime Interface Emulator. This is an official AWS tool that lets you run Lambda inside a Docker container, mimicking the AWS runtime. It's particularly useful if you're deploying container-based Lambdas.
While this was helpful, I found that for most of my use cases, I didn't need to go that deep. If you're working with Lambda containers, though, this is worth checking out.
Option 4: The Good Old Mocking Approach
Sometimes, the simplest solution is the best one. Instead of running a full AWS stack locally, I started writing unit tests with mocked AWS SDK responses. Using tools like moto (for Python) or aws-sdk-mock (for Node.js), I could simulate DynamoDB, S3, and other AWS services without spinning up anything locally.
This approach made my tests run way faster, and it worked great for logic validation. Of course, it didn't fully replace integration testing, but for quick feedback, it was a lifesaver.
So, What’s the Best Approach?
Honestly, there's no one-size-fits-all solution. For me, a combination of AWS SAM for local execution and unit tests with mocks gave me the best of both worlds. I could run real-world tests when needed but still keep my feedback loop fast.
I'm curious: how do you test your Lambda functions locally? Are you using one of these methods, or do you have a different approach? Let's chat in the comments! 🚀
Top comments (1)
I am using Localstack for quite some time. It does a good job at simulation, except for that free tier issues.