What is AWS Lambda. What are some important points to keep in mind while working with Lambda functions?
AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS) that allows you to run code without provisioning or managing servers. It lets you execute code in response to events such as changes in data, HTTP requests, or scheduled events, without the need to manage infrastructure.
So, we can invoke lambda directly from Amazon Event Bridge rule or from SQS.
Here are some important points to keep in mind while working with AWS Lambda:
1. Serverless Architecture: With AWS Lambda, you don't need to provision or manage servers. You upload your code and AWS takes care of scaling and managing the infrastructure needed to run it.
But keep one thing in mind, it is possible to upload the code directly to Lambda in AWS in zip format, but use this approach only when you want to develop the Lambda locally.
For production ready applications, it is always advisable to deploy Lambda using CICD pipelines. It means build the Lambda code and publish to ECR repository and then deploy it to AWS using the image.
2. Event-driven Execution: Lambda functions are triggered by events such as changes to data in Amazon S3 buckets, updates to DynamoDB tables, HTTP requests via Amazon API Gateway, or scheduled events from Amazon CloudWatch.
Recently I had worked on one use case where we were required to copy the objects uploaded by users to S3. So here we have invoked the Lambda from the S3 Batch Operations Job, which were responsible to actually copy the objects from one S3 location to another.
3. Pay-per-Use Pricing: You only pay for the compute time you consume. AWS Lambda automatically scales to handle the incoming requests, and you're charged based on the number of requests and the duration of the execution.
But always keep in mind, if your execution time of Lambda function is huge, then it will cost you too much. So always try to reduce the long running processes in Lambda like I/O operations etc.
4. Support for Multiple Languages: Lambda supports various programming languages including Node.js, Python, Java, Go, and .NET Core, allowing developers to choose the language they're most comfortable with.
But before choosing a particular language to write Lambda, you need to understand that some languages takes too much time for Lambda execution. e.g. a Lambda written in Node JS or Python will be much faster than a Lambda written in .NET Core.
So the suggestion is to use the interpreted languages like Node and Python for the Lambda.
5. Integration with Other AWS Services: Lambda can easily integrate with other AWS services, allowing you to build complex applications and workflows. For example, you can trigger Lambda functions in response to events from services like S3, DynamoDB, SQS, SNS, and more.
I had recently worked on a use case where we were sending a message to SQS, after completing a long running process and once that message was invoking a Lambda function.
6. Scalability and Fault Tolerance: AWS Lambda automatically scales to handle a high volume of requests simultaneously, and it ensures fault tolerance by running your code across multiple availability zones within a region.
7. Limited Execution Environment: While Lambda supports multiple programming languages, it does have certain limitations such as execution time limits, memory limits, and restrictions on the file system. It's important to be aware of these constraints while developing Lambda functions.
Always keep in mind, AWS Lambda has execution limit of 15 min and that is the maximum limit we are talking about. So if your lambda exceeds this limit it will fail automatically. So you may have to re-run you lambda function in that case.
Similarly, Lambda allows to allocate memory to your functions in increments of 1 MB starting from 128 Mb up to 10 GB.
8. Stateless Functions: Lambda functions are designed to be stateless, meaning they don't retain state between invocations. If you need to maintain state, you can use external data stores like DynamoDB, S3, or ElastiCache.
9. Monitoring and Logging: AWS provides monitoring and logging capabilities through services like Amazon CloudWatch, which allows you to monitor the performance of your Lambda functions, track execution logs, and set up alarms for specific metrics.
Always keep an eye on the Monitoring tab of your Lambda function, it will help you to save some extra bucks.
AWS Lambda is useful because it simplifies the process of deploying and managing code, allowing developers to focus on writing business logic rather than managing infrastructure. It also offers scalability, cost-effectiveness, and seamless integration with other AWS services, making it a powerful tool for building serverless applications and microservices.
That's it for today.
Happy Learning :)
Comments
Post a Comment