DEV Community

Guille Ojeda for AWS Community Builders

Posted on • Originally published at newsletter.simpleaws.dev

Simple AWS: 20 Advanced Tips for Lambda

Note: This content was originally published at the Simple AWS newsletter. Understand the Why behind AWS Solutions. Subscribe for free! 3000 engineers and tech experts already have.

AWS Service: Lambda

tl;dr: Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. It's the most basic serverless building block, especially for event-driven architectures.

Here's how it works:

  • You create a function and write the code that goes in it.

  • You set up a trigger for that function, such as an HTTP request

  • You configure CPU and memory, and give that function an execution role with IAM permissions

  • When the trigger event occurs, an isolated execution starts, receives the event, runs the code, and returns

  • You only pay for the time the code was actually running

Obviously, that code runs somewhere. The point is that you don't manage or care where ('cause it's serverless, you see). Every time a request comes in, Lambda will either use an available execution environment or start a new one. That means Lambda scales up and down automatically and nearly instantly.

Here's the fine details:

  • Supported languages are: Node.js, TypeScript, Python, Ruby, Java, Go, C# and PowerShell. Use a custom runtime for other languages.

  • Lambda functions can be invoked from HTTP requests, in response to events from other services, or at defined time intervals (cron jobs).

  • Billing is actually calculated as execution time * assigned memory (GB-seconds), plus a fixed charge per invocation. CPU is tied to memory.

  • Lambdas aren't actually instantaneous, there's a cold start (time to start the execution environment). Check the tips below for how to mitigate it.

  • Logs are automatically generated and sent to CloudWatch Logs.

Actionable tips

The most important tip is that you don't need to do everything in this list, and you don't need to do everything right now. But take your time to read it, I bet there's at least one thing in there that you should be doing but aren't.

  1. Lambdas don't run in a VPC, unless you configure them for that. You need to do that if you want to access VPC resources, such as an RDS or Aurora database.

  2. Use environment variables.

  3. If you need secrets, put them in Secrets Manager and put the secret name in an environment variable.

  4. As always, grant minimum permissions only.

  5. Use versioning and aliases, so you can do canary deployments, blue-green deployments and rollbacks.

  6. Use Lambda layers to reuse code and libraries.

  7. For constant traffic, Lambda is more expensive than anything serverful (e.g. ECS). The benefit of Lambda is in scaling out extremely fast, and scaling in to 0 (i.e. if there's no traffic you don't pay).

  8. Not everything needs to be serverless. Choose the best runtime for each service.

  9. Use Lambda Power Tuning to optimize memory and concurrency settings for better performance and cost efficiency.

  10. Set provisioned concurrency to guarantee a min number of hot execution environments. It's not free.

  11. There's an account limit for concurrent Lambda executions. To ensure a particular function always has available limit, use reserved concurrency. It also serves as a maximum concurrency for that function.

  12. Use compute savings plans to save money.

  13. Already using containers? Run your containerized apps in Lambda. You could also consider ECS Fargate.

  14. Don't use function URLs, if you want to trigger functions from HTTP(s) requests use API Gateway instead. Here's a tutorial, and we'll talk more about it next week.

  15. If you have Lambdas that call other Lambdas, monitoring and tracing is a pain, unless you use AWS X-Ray.

  16. Use SnapStart to improve cold starts by 10x (only in Java, for now...)

  17. Code outside the handler only runs on environment initialization, not on every invocation. Put there everything you can, such as initializing the SDK.

  18. Reduce request latency with global accelerators.

  19. If you're processing data streams from Kinesis or DynamoDB, configure parallelization factor to process a shard with more than one simultaneous Lambda invocation.

Recommended tool(s)

Lambda is supported by every IaC tool out there. But if you're working with serverless, you'll want to check out these options (and pick one, don't mix them):

  • AWS SAM: It's like CloudFormation, but built for serverless. In fact, to deploy an app your SAM template is translated to CFN (or you can use Accelerate). And it lets you run Lambda functions locally.

  • Serverless Framework: Cloud-agnostic IaC tool specifically built for serverless. Works great with Lambda and many other serverless AWS services such as SQS, SNS, API Gateway, DynamoDB and more. The bad news is that, if a service is not supported, you can't work around that. Also lets you run your apps locally (though I've encountered a few problems with SNS and SQS).

  • AWS CDK: An IaC tool built for programmers. Most tools are declarative, you write a config file. CDK is imperative, you use a programming language and declare variables, control structures and loops. It's not specific for serverless, but it's a lot more dev-friendly than most. Also supports locally running your apps.

All of the above are great, but a bit limited for running things locally. LocalStack is better (though some features are paid). Try to use your IaC tool's capabilities, but if you hit a wall, definitely give LocalStack a shot.

Serverless Land is a place with a ton of serverless resources.

And check out the Serverlespressso AWS Workshop. They built a serverless app to serve coffee, and set up a coffee shop at the expo in AWS Re:Invent 2021 (I was there!).

Haiku(s)

Code without servers,
Scaling at the speed of light,
That's Lambda for you.


Understand the Why behind AWS Solutions.
Join over 3000 devs, tech leads, and experts learning how to architect cloud solutions, not pass exams, with the Simple AWS newsletter.

  • Real-world scenarios
  • The Why behind solutions
  • How to apply best practices

Subscribe for free!

If you'd like to know more about me, you can find me on LinkedIn or at www.guilleojeda.com

Top comments (4)

Collapse
 
mcat_ee profile image
no context tom

RE: the serverless framework

The bad news is that, if a service is not supported, you can't work around that

That's not strictly true since you can include cloudformation definitions as Resources?

Collapse
 
guilleojeda profile image
Guille Ojeda

I tried that a couple of times and didn't work for unsupported services. Maybe I did something wrong? I'll update the post!

Collapse
 
rcoundon profile image
Ross Coundon

I strongly suggest you take a look at sst.dev
Provides fantastic Typescript support, local debugging, higher level constructs like Static Site and NextJS and a tonne of other capabilities with first class DX
It also has a great, supportive community

Collapse
 
codesinthedark profile image
Srdjan Mitrovic

Why shouldn't we use function URLs? Thanks.