DevOps Sessions - Week 16 - Serverless

devops serverless 14-11-2024 ​​

DevOps Sessions - Week 16 - Serverless

Welcome to Week 16 of our “Becoming a DevOps Engineer” series! This week, we will focus on serverless computing, an innovative approach that allows developers to build and deploy applications without managing the underlying infrastructure. Serverless computing offers scalability, cost efficiency, and faster development cycles. We will explore key concepts, popular serverless platforms like AWS Lambda, Azure Functions, and Google Cloud Functions, and best practices for implementing serverless architectures. Let’s get started!

Session Overview

1. Introduction to Serverless Computing

2. Key Concepts in Serverless

3. AWS Lambda

4. Azure Functions

5. Google Cloud Functions

6. Practical Examples

7. Best Practices and Tools

1. Introduction to Serverless Computing

What is Serverless Computing?

Serverless computing is a cloud computing model where the cloud provider dynamically manages the allocation and provisioning of servers. In a serverless architecture, developers focus on writing code, and the cloud provider handles the infrastructure, scaling, and maintenance.

Benefits of Serverless Computing in DevOps

2. Key Concepts in Serverless

Function as a Service (FaaS)

FaaS is a serverless computing model that allows developers to run individual functions in the cloud. Each function is executed in response to events and is stateless, meaning it does not retain any data between invocations.

Backend as a Service (BaaS)

BaaS provides developers with pre-built backend services such as databases, authentication, and storage. These services are managed by the cloud provider, allowing developers to focus on building frontend applications.

3. AWS Lambda

Overview of AWS Lambda

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. You pay only for the compute time you consume, and it automatically scales your application by running code in response to triggers.

Setting Up AWS Lambda

  1. Create an AWS Account: Sign up for an AWS account at aws.amazon.com.
  2. Navigate to AWS Lambda: Open the AWS Management Console and navigate to the AWS Lambda service.

Creating and Deploying Lambda Functions

  1. Create a Lambda Function:

    • Click on “Create function”.
    • Choose “Author from scratch”.
    • Enter a function name, select a runtime (e.g., Python 3.8), and click “Create function”.
  2. Write the Function Code:

    def lambda_handler(event, context):
        return {
            'statusCode': 200,
            'body': 'Hello from AWS Lambda!'
        }
  3. Configure Triggers:

    • Add triggers such as API Gateway, S3, DynamoDB, or CloudWatch Events.
  4. Deploy the Function:

    • Click on “Deploy” to save and deploy the function.

4. Azure Functions

Overview of Azure Functions

Azure Functions is a serverless compute service that allows you to run event-driven code without having to manage infrastructure. It integrates with various Azure services and supports multiple programming languages.

Setting Up Azure Functions

  1. Create an Azure Account: Sign up for an Azure account at azure.microsoft.com.
  2. Navigate to Azure Functions: Open the Azure Portal and navigate to the Azure Functions service.

Creating and Deploying Azure Functions

  1. Create a Function App:

    • Click on “Create a resource”.
    • Select “Function App” and click “Create”.
    • Configure the basic settings (e.g., subscription, resource group, function app name, runtime stack) and click “Review + create”.
  2. Write the Function Code:

    import logging
    import azure.functions as func
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
        return func.HttpResponse("Hello from Azure Functions!")
  3. Configure Triggers:

    • Add triggers such as HTTP, Timer, Blob Storage, or Event Grid.
  4. Deploy the Function:

    • Click on “Deploy” to save and deploy the function.

5. Google Cloud Functions

Overview of Google Cloud Functions

Google Cloud Functions is a serverless compute service that executes your code in response to events. It is fully managed and automatically scales with demand.

Setting Up Google Cloud Functions

  1. Create a Google Cloud Account: Sign up for a Google Cloud account at cloud.google.com.
  2. Navigate to Google Cloud Functions: Open the Google Cloud Console and navigate to the Cloud Functions service.

Creating and Deploying Google Cloud Functions

  1. Create a Cloud Function:

    • Click on “Create function”.
    • Enter a function name, select a region, and choose a runtime (e.g., Node.js 14).
  2. Write the Function Code:

    exports.helloWorld = (req, res) => {
        res.send('Hello from Google Cloud Functions!');
    };
  3. Configure Triggers:

    • Add triggers such as HTTP, Pub/Sub, or Cloud Storage.
  4. Deploy the Function:

    • Click on “Deploy” to save and deploy the function.

6. Practical Examples

Event-Driven Processing with AWS Lambda

  1. Create an S3 Bucket: Create a new S3 bucket in the AWS Management Console.
  2. Configure S3 Trigger: Add an S3 trigger to your Lambda function to process objects uploaded to the bucket.
  3. Write the Lambda Function:
    import json
    
    def lambda_handler(event, context):
        s3_event = event['Records'][0]['s3']
        bucket = s3_event['bucket']['name']
        key = s3_event['object']['key']
        return {
            'statusCode': 200,
            'body': json.dumps(f'Processed file {key} from bucket {bucket}')
        }

Building a REST API with Azure Functions

  1. Create a Function App: Create a new Function App in the Azure Portal.

  2. Add an HTTP Trigger: Add an HTTP trigger to your Azure Function.

  3. Write the Function Code:

    import logging
    import azure.functions as func
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
        name = req.params.get('name')
        if not name:
            try:
                req_body = req.get_json()
            except ValueError:
                pass
            else:
                name = req_body.get('name')
        if name:
            return func.HttpResponse(f"Hello, {name}!")
        else:
            return func.HttpResponse(
                "Please pass a name on the query string or in the request body",
                status_code=400
            )
  4. Deploy the Function: Deploy the function and test it by sending HTTP requests.

7. Best Practices and Tools

Best Practices for Serverless Computing


By mastering serverless computing with platforms like AWS Lambda, Azure Functions, and Google Cloud Functions, you can build scalable, cost-efficient, and fast applications. Stay tuned for next week’s session, where we will explore compliance and governance. Happy coding!

Author's photo

Nihit Jain

Architecting DevOps 🏗️ with Data, AI, Security, & IoT on Cloud ☁️




See other articles:

Sessions