Best Practices & Common Mistakes

Small habits that make Lambda functions faster, cheaper, and easier to debug.

1 min read

Do this

  • Keep functions small and focused. One job per function is easier to test, secure, and scale.
  • Use environment variables for config such as table names and model IDs, not hard-coded strings.
  • Set a sensible timeout. Long enough for normal runs, short enough that a hung call fails fast.
  • Give each function its own role with least-privilege permissions.
  • Log useful context (IDs, sizes, durations) so CloudWatch Logs tell a story.
  • Make handlers idempotent. Async triggers can deliver the same event more than once, so processing it twice shouldn't cause harm.

Avoid this

MistakeBetter approach
Creating clients inside the handlerCreate them once at module level
Bundling huge libraries you don't useTrim dependencies, or use a Lambda layer or container image
Secrets in environment variables as plain textStore them in AWS Secrets Manager or SSM Parameter Store
One giant "do everything" functionSplit it, or use Step Functions to orchestrate
Tip

Cold starts bothering a user-facing API? Try trimming imports first. If you still need more, look at provisioned concurrency (keeps environments warm) or SnapStart (restores from a snapshot).

Key takeaways

  • Small, single-purpose functions with their own least-privilege role.
  • Config in environment variables, secrets in Secrets Manager or Parameter Store.
  • Design handlers to be idempotent, because events can arrive more than once.

Check your understanding

Pick an answer to see the explanation.

  1. 1. Why should a Lambda handler be idempotent?

Next in “Build a serverless AI app”
You finished Lambda. Continue with the API Gateway tutorial