Serverless Computing explained with example
What is Serverless Computing?
Serverless computing is a cloud-computing execution model where the cloud provider dynamically manages the allocation and provisioning of servers. A serverless application runs in stateless compute containers that are event-triggered, ephemeral (may last only for one invocation), and fully managed by the cloud provider.
Key Characteristics
- No Server Management: Developers don’t need to manage or provision servers.
- Auto-Scaling: Automatically scales with the number of incoming requests.
- Pay-as-You-Go: Only pay for the execution time and resources consumed by the application.
- Event-Driven: Functions are invoked in response to events such as HTTP requests, database changes, file uploads, etc.
Example: AWS Lambda with S3 Trigger
Scenario
We’ll create a serverless function using AWS Lambda that gets triggered whenever a new file is uploaded to an S3 bucket. The function will log the details of the uploaded file.
Steps
-
Create an S3 Bucket
- Go to the AWS Management Console.
- Navigate to S3.
- Create a new S3 bucket (e.g.,
my-serverless-bucket
).
-
Create a Lambda Function
- Navigate to Lambda in the AWS Management Console.
- Click on "Create function."
- Choose "Author from scratch."
- Name your function (e.g.,
LogS3Upload
). - Choose a runtime (e.g., Node.js 14.x).
- Set up the execution role with permissions to access the S3 bucket.
-
Add S3 Trigger
- In the Lambda function configuration, add a trigger.
- Choose S3.
- Select the bucket created in step 1.
- Set the event type to
ObjectCreated
(this will trigger the function whenever a new object is uploaded).
-
Write the Lambda Function Code
- Use the following example code for the Lambda function:
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.handler = async (event) => {
// Log the event received from S3
console.log('Event:', JSON.stringify(event, null, 2));
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
// Get the object metadata from S3
const params = {
Bucket: bucket,
Key: key,
};
try {
const metadata = await s3.headObject(params).promise();
console.log('Metadata:', JSON.stringify(metadata, null, 2));
} catch (error) {
console.error('Error fetching metadata:', error);
}
return {
statusCode: 200,
body: JSON.stringify({ message: 'Function executed successfully!' }),
};
};
-
Deploy the Lambda Function
- Save and deploy the function in the AWS Management Console.
-
Test the Function
- Upload a file to the S3 bucket.
- Check the CloudWatch logs for the Lambda function to see the logs for the uploaded file.
Advantages of Serverless Computing
- Reduced Operational Complexity: No need to manage infrastructure.
- Scalability: Automatically scales with the load.
- Cost-Effective: Pay only for the compute time used.
- Quick Deployment: Easier and faster to deploy than traditional server-based applications.
Use Cases for Serverless Computing
- Microservices: Building small, single-purpose services.
- Real-Time File Processing: Processing files uploaded to a cloud storage.
- Data Transformation: Transforming data as it flows through your system.
- API Backends: Creating backends for web and mobile applications.
- Event-Driven Applications: Applications that react to events from various sources.
Published on: Jul 10, 2024, 10:55 PM