Home  Devops   Serverless ...

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

  1. No Server Management: Developers don’t need to manage or provision servers.
  2. Auto-Scaling: Automatically scales with the number of incoming requests.
  3. Pay-as-You-Go: Only pay for the execution time and resources consumed by the application.
  4. 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

  1. Create an S3 Bucket

    • Go to the AWS Management Console.
    • Navigate to S3.
    • Create a new S3 bucket (e.g., my-serverless-bucket).
  2. 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.
  3. 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).
  4. 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!' }),
    };
};
  1. Deploy the Lambda Function

    • Save and deploy the function in the AWS Management Console.
  2. 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

Use Cases for Serverless Computing

Published on: Jul 10, 2024, 10:55 PM  
 

Comments

Add your comment