AWS Lambda Best Practices
Monday, September 19, 2022In the past several years, serverless computation has been a popular subject, and nearly every cloud service provider has provided serverless solutions for their customers. The majority of these technologies have made the life of programmers much simpler than it used to be. But serverless services may not be suitable for everybody and each use case, since they will have their own drawbacks. We will discuss all of these in this blog on AWS Lambda best practices.
One of the advantages of adopting AWS Lambda function is that servers and architecture maintenance are handled automatically. This implies that AWS lambda will perform your Lambda procedures without requiring you to do any hard lifting.
Let’s dive deeper to know guidelines and AWS lambda best practices for developing AWS Lambda functions.
Further Reading On:
AWS Lambda vs Azure Function : Serverless Computing
1. AWS Lambda Best Practices
1.1 Monitor Your Concurrency
Due to the rapid scalability of Lambda functions, you must have measures in place to inform you of an increase in concurrency. Implementing an associated CloudWatch metrics alarm that tells your crew when the operation metrics like ConcurrentExecutions or subsequent lambda invocation surpass your concurrency limit is a smart idea. You can set an AWS budgeting so that you can track expenditures regularly.
1.2 Throttling
As described in the Concurrency paragraph, any event influx that surpasses a function’s concurrency limit might result in throttling. It signifies that no additional queries will be processed. Now, one should manage it graciously; else, it might have a significant influence on the organization.
- If a lambda execution is concurrent, a 429 error code will be returned. In addition, it will get data on whether the throttling is at the Functional or Account level. Depending on that calling service (such as API Gateway), then retry must be handled.
- In case a lambda call is concurrent, lambda will attempt the occurrence twice without discarding it. If a lambda function code is unable to handle an occurrence, DLQ (Dead Letter Queue) should be formed utilizing SQS or SNS to diagnose and execute it afterwards. Whether you fail to specify DLQ, communications will be missed and deleted.
- If a lambda execution is poll-based, the result of the poll is returned.
- When it is stream-based (Kinesis), it will continue to attempt until the information does not expire till 7 days.
- If it is not stream-based (SQS), the communication will be returned to the backlog and retried only when the Specified timeout duration expires. This procedure will continue until the message gets processed or the preservation period ends.
1.3 Monitoring
CloudWatch integrates effectively with Lambda function and gives comprehensive execution information. Lambda regularly monitors the user requests, the runtime environment per session, and the total of responses that result in an error and delivers the corresponding CloudWatch data. You may also use these metrics to construct custom CloudWatch alerts.
You can also utilize X-Ray to investigate possible Lambda execution bottlenecks. The X-Ray can be beneficial when attempting to understand how AWS lambda function’s runtime is being spent. It also facilitates the identification of all the secondary processing it links to the total flow.
1.4 Watch Your Package Size and Dependencies
The larger your delivery package, the longer your service will cold-start. Eliminate any extraneous stuff, such as useless modules and manuals. If you are utilizing Java methods with the AWS SDK, you should just package the file(s) you have to utilize, and not the full SDK.
Good:
software.amazon.awssdk
dynamodb
2.17.247
Bad:
software.amazon.awssdk
aws-sdk-java
2.17.247
1.5 When to use VPC in Lambda Function
AWS services are always executed within a lambda-managed VPC. Your function can send network queries to any open domain name by nature; this provides access to all the available AWS APIs. Your code may interface with AWS DynamoDB APIs to PutItem or Browse for data, for instance. If you want to communicate with a personal AWS lambda service on a private network, you must only allow VPC connectivity for your services. An RDS instance is a nice illustration.
When your service is VPC-enabled, all internet traffic generated by your service is subjected to the VPC/Subnet browsing rules. If your function must communicate with a common service, routing across a NAT gateway in an open subnet is required.
1.6 Lambda Handler
A lambda function’s data input is its handler procedure. In addition, three values are provided to this handler method for each lambda-triggered event, with the top environment variable holding all event-related information. Having the application logic in a different program layer instead of implementing it in the handler function is a correct approach because it provides the road for clearer and more legible software and, most significantly, makes it much simpler to build component code for the business logic.
- Remove the Lambda handler from the rest of your code. This helps you to create a code that is highly unit-testable. In Node.js, this may appear as:
exports.myHandler = function(event, context, callback) {
var foo = event.foo;
var bar = event.bar;
var result = LambdaFunction (foo, bar);
callback(null, result);
}
function LambdaFunction (foo, bar) {
// LambdaFunction logic here
}
1.7 Optimizing Memory Size
You may alter the number of RAM allotted to your lambda function. RAM size also affects the number of CPU time and internet speed allocated to a function. Obtain the ideal price/performance ratio for your app by evaluating your Lambda function at any of the existing resource settings. It is an anti-pattern to select the minimum number of RAM that executes your function quickly enough. Since Lambda operations are invoiced in 100-ms intervals, this is the case. Choosing the minimum quantity might introduce delay to your program and make it possibly more costly if the extra latency exceeds the RAM cost savings.
1.8 Optimizing Language Runtime
AWS Lambda supports C#, Java, Go, Python, Ruby, Node.js, and PowerShell.
It is necessary to develop in the language with which you are more familiar, but if functionality is the primary issue for the software, there are a number of important factors to examine. Compliable languages, such as Java and C#, have the longest initial starting time. Initial set-up times for programming languages are considerably faster.
Use an interpretative language, such as Python or Node.js, if your software’s use case is extremely latency-sensitive and prone to spiking or irregular load.
1.9 Asynchronous Architecture
If you can build your architecture to be more reactive, you may discover that your program’s components use less processing time to perform the task than elements that connect closely and wait for replies to concurrent queries. Decoupling a function might lead to a more economical solution.
2. Conclusion
In this AWS Lambda best practices, we have seen that AWS’s Lambda is a serverless computation service that follows the Function as a Service concept. This enables a user to concentrate more on corporate logic and eliminates the majority of issues that a developer may have to address. Owing to the characteristics of the Lambda runtime setting, this may also cause developers to make errors while setting or developing code. Hence, it is essential to keep industry standards in mind to ensure that the business logic is implemented in a protected setting that is optimized for lambda performance.
Comments