What is Serverless Computing?
Serverless computing is a cloud computing execution model where you, as a developer, don't have to manage servers directly. You focus solely on writing and deploying your code, while the cloud provider takes care of all the underlying infrastructure.
Serverless does not mean there are no servers… it means you just don’t manage / provision / see them.
How Lambda Functions Fit into the Serverless World
At the heart of AWS Lambda are "Lambda functions." These are individual units of code that perform specific tasks. Think of them as small, single-purpose applications that run independently.
Here's how Lambda functions fit into the serverless world:
Event-Driven Execution: Lambda functions are triggered by events. An event could be anything, like a new file being uploaded to Amazon S3, a request hitting an API, or a specific time on the clock. When an event occurs, Lambda executes the corresponding function.
No Server Management: As a developer, you don't need to worry about managing servers. AWS handles everything behind the scenes. You just upload your code, configure the trigger, and Lambda takes care of the rest.
Automatic Scaling: Whether you have one user or one million users, Lambda scales automatically. Each function instance runs independently, ensuring that your application can handle any level of incoming traffic without manual intervention.
Pay-per-Use: One of the most attractive features of serverless computing is cost efficiency. With Lambda, you pay only for the compute time your code consumes. When your code isn't running, you're not charged.
Supported Languages: Lambda supports multiple programming languages like Node.js, Python, Java, Go, and more. You can choose the language you are comfortable with or that best fits your application's needs.
What is AWS Lambda?
● AWS Lambda is a serverless compute service through which you can run your code without provisioning any Servers.
● It only runs your code when needed and also scales automatically when the request count increases.
● AWS Lambda follows the Pay-per-use principle – it means there is no charge when your code is not running.
● Lambda allows you to run your code for any application or backend service with zero administration.
● Lambda can run code in response to the events. Example– update in DynamoDB Table or change in S3 bucket.
● You can even run your code in response to HTTP requests using Amazon API Gateway.
● Lambda is serverless! No need to worry about server maintenance, just focus on your code
Describe the key differences between Amazon EC2 and AWS Lambda. When would you choose one over the other for a specific task?
Amazon EC2 provides virtual servers that you manage, while AWS Lambda runs code in response to events and scales automatically. Choose EC2 for long-running tasks or when you need more control over the environment. example: hosting a website with specific software requirements or running a database server.
Choose Lambda for event-driven, short-lived tasks with automatic scaling. example: processing event-driven actions such as file uploads, database updates, or API requests
Use Cases of Lambda
Data processing and analysis
Real-time image and video processing
IoT device management and data ingestion
Serverless web and mobile backend
Chatbots and voice assistants
When do you use Lambda?
● When using AWS Lambda, you are only responsible for your code.
● AWS Lambda manages the memory, CPU, Network, and other resources.
● It means you cannot log in to the compute instances or customize the operating system.
● If you want to manage your own compute resources, you can use other compute services such as EC2, and Elastic Beanstalk.
● There will be a level of abstraction which means you cannot log in to the server or customize the runtime.
Benefits of AWS Lambda
• Easy Pricing:
• Pay per request and compute time
• Free tier of 1,000,000 AWS Lambda requests and 400,000 GBs of compute time
• Integrated with the whole AWS suite of services
• Integrated with many programming languages
• Easy monitoring through AWS CloudWatch
• Easy to get more resources per function (up to 10GB of RAM!)
• Increasing RAM will also improve CPU and network!
Lambda Functions
● A function is a block of code in Lambda.
● You upload your application/code in the form of single or multiple functions.
● You can upload a zip file, or you can upload a file from the S3 bucket as well.
● After deploying the Lambda function, Lambda automatically monitors functions on your behalf, reporting metrics through Amazon CloudWatch.
Lambda Layers
● A Lambda layer is a container/archive that contains additional code such as libraries, dependencies, or custom runtimes.
● AWS Lambda allows five layers in a function.
● Layers are immutable.
● A new version will be added if you publish a new layer.
● Layers are by default private but can be shared and made public explicitly.
Language Supported in AWS Lambda
● NodeJS
● Go
● Java
● Python
● Ruby
Lambda@Edge
● It is the feature of Amazon CloudFront that allows you to run your code closer to the location of Users of your application.
● It improves performance and reduces latency.
● Just like lambda, you don’t have to manage and provision the infrastructure around the world.
● Lambda@Edge runs your code in response to the event created by the CDN.
Lambda Event
● A Lambda Event is an entity that invokes the lambda function.
● Lambda supports synchronous invocation of Lambda Functions.
● Lambda supports the following sources as an event:
⭐ AWS DynamoDB
⭐ AWS SQS
⭐ AWS SNS
⭐ CloudWatch Event
⭐ API Gateway
⭐ AWS IoT
⭐ Kinesis
⭐ CloudWatch Logs
Real-World Use Cases
Automated Image Processing: Imagine you have a photo-sharing app, and users upload images every day. You can use Lambda to automatically resize or compress these images as soon as they are uploaded to S3.
Chatbots and Virtual Assistants: Build interactive chatbots or voice-controlled virtual assistants using Lambda. These assistants can perform tasks like answering questions, fetching data, or even controlling smart home devices.
Scheduled Data Backups: Use Lambda to create scheduled tasks for backing up data from one storage location to another, ensuring data resilience and disaster recovery.
Real-Time Analytics: Lambda can process streaming data from IoT devices, social media, or other sources, allowing you to perform real-time analytics and gain insights instantly.
API Backends: Develop scalable API backends for web and mobile applications using Lambda. It automatically handles the incoming API requests and executes the corresponding functions.
Some Important Questions!
How does AWS Lambda work?
You can upload your code to Lambda and define event sources that trigger the execution of your code. Lambda automatically manages the execution environment, scales it as needed, and provides monitoring and logging.
Explain the concept of an AWS Lambda function and its key components.
An AWS Lambda function is a piece of code that is executed in response to an event. Key components include the function code, runtime, handler, execution role, and triggers. The handler is the entry point for the function code, and triggers define what events invoke the function.
What is the most expensive- Lambda or EC2?
Ans: It depends
I have designed an app where Lambda was more expensive than EC2. We can check through the AWS Cost calculator to estimate the cost for both lambda and ec2 with estimated traffic for a better-informed decision.
Describe the key differences between Amazon EC2 and AWS Lambda. When would you choose one over the other for a specific task?
Amazon EC2 provides virtual servers that you manage, while AWS Lambda runs code in response to events and scales automatically. Choose EC2 for long-running tasks or when you need more control over the environment. example: hosting a website with specific software requirements or running a database server.
Choose Lambda for event-driven, short-lived tasks with automatic scaling. example: processing event-driven actions such as file uploads, database updates, or API requests
What are the programming languages and runtimes supported by AWS Lambda, and how do you choose the appropriate runtime for your function?
AWS Lambda supports various runtimes, including Node.js, Python, Java, Go, Ruby, NET Core, and custom runtimes. You choose the appropriate runtime based on the language you are comfortable with and the specific requirements of your function.
How do you deploy and update AWS Lambda functions, and what is the role of versioning and aliases in managing functions?
You can deploy and update functions using the AWS Management Console, AWS CLI, or SDKs. Versioning and aliases allow you to manage different versions of a function and control which version or alias is invoked by other services or applications.
What types of events can trigger AWS Lambda functions?
AWS Lambda functions can be triggered by various event sources, such as changes in Amazon S3 objects, updates to Amazon DynamoDB tables, HTTP requests through Amazon API Gateway, and more.
How is concurrency managed in AWS Lambda?
Lambda automatically handles concurrency by scaling out instances of your function in response to incoming requests. You can set a concurrency limit to control how many concurrent executions are allowed.
What is the maximum execution duration for a single AWS Lambda invocation?
The maximum execution duration for a single Lambda invocation is 15 minutes.
How do you pass data to and from AWS Lambda functions?
You can pass data to Lambda functions through event objects, which contain information about the triggering event. You can also return data by using the return statement or creating a response object.
Can the AWS Lambda function communicate with external resources?
Yes, Lambda functions can communicate with external resources such as databases, APIs, and other AWS services by using appropriate SDKs and APIs provided by AWS.
What are AWS Lambda layers?
AWS Lambda layers are a way to manage and share code that is common across multiple functions. Layers can include libraries, custom runtimes, and other function dependencies.
How can you handle errors in AWS Lambda functions?
You can handle errors by using try-catch blocks in your code. Lambda also provides CloudWatch Logs for monitoring, and you can set up error handling and retries for asynchronous invocations.
Can AWS Lambda function access the internet?
Yes, Lambda functions can access the internet through the Virtual Private Cloud (VPC) or public endpoints if your function is not configured within a VPC.
What are the execution environments available for AWS Lambda functions?
Lambda supports several runtimes, including Node.js, Python, Java, Go, Ruby, .NET Core, and custom runtimes using the Runtime API.
How can you configure environment variables for AWS Lambda functions?
You can set environment variables for Lambda functions when creating or updating the function. These variables can be accessed within your code.
What is the difference between synchronous and asynchronous invocation of Lambda functions?
Synchronous invocations wait for the function to complete and return a response, while asynchronous invocations return immediately, and the response is sent to a specified destination.
What is the AWS Lambda Event Source Mapping?
Event Source Mapping allows you to connect event sources like Amazon DynamoDB streams or Amazon Kinesis streams to Lambda functions. This enables the function to process events as they occur.
How can you manage the permissions and execution roles for AWS Lambda functions?
You can use AWS Identity and Access Management (IAM) roles to grant permissions to your Lambda functions. Execution roles define what AWS resources the function can access.
What are AWS Step Functions?
AWS Step Functions is a serverless orchestration service that lets you coordinate multiple AWS services into serverless workflows using visual workflows called state machines.
How can you automate the deployment of AWS Lambda functions?
You can use AWS Serverless Application Model (SAM) templates, AWS CloudFormation, or CI/CD tools like AWS Code Pipeline to automate the deployment of Lambda functions.
Can the AWS Lambda function connect to on-premises resources?
Yes, Lambda functions can connect to on-premises resources by placing the function inside a VPC and using a VPN or Direct Connect connection to establish connectivity.
What is the Cold Start issue in AWS Lambda?
The Cold Start issue occurs when a Lambda function is invoked for the first time or after it has been idle for a while. The function needs to be initialized, causing a slight delay in response time.