AWS Builder Center

How I test a Lambda locally before deploying it

SAM local invoke with a test event JSON per handler, env vars from a local file that is never committed, and what local testing catches and what only a deploy catches.

Full-stack Developer & Security Researcher
I test every handler locally before I deploy it. Local testing catches config and logic errors early. It does not catch everything.

Setup

I use SAM local invoke with a test event JSON per handler. One file per event, named after the handler and the case. Env vars live in a local env file that is never committed. Secrets stay out of the repo.

What I run

sam local invoke MyFunction --event events/valid.json --env-vars env.json
I also run the handler directly with Python for fast loops, with the same event JSON and the same env file.

What local testing catches

  • Wrong env var names or missing values
  • Bad JSON parsing and wrong response shape
  • Timeout or memory set too low for the work
  • Broken imports and missing dependencies in the package

What it does not catch

  • IAM role mismatch. Local runs use my credentials, not the Lambda execution role. A call that works locally can fail in AWS with AccessDenied.
  • VPC, subnet, and security group problems. Local runs do not use the Lambda network config.
  • Trigger config. A test event is not the same as a real SQS, API Gateway, or EventBridge invocation.

What only a deploy catches

I deploy to a test function first, invoke it with a real event, and check CloudWatch logs. If the role, trigger, or network config is wrong, it shows up there, not locally.
Rule: local testing proves the code works. A deploy proves the AWS config works.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article