Python Classroom notes 29/Dec/2024

Python contd…

Serverless

  • Serverless is a technology where
    • on the server side we implement a Function
    • This server component will be created on request, runs as long as the function executes.
    • You will be charged for the exact duration
  • Serverless providers handle scaling.
  • Serverless is also referred as FaaS (Functions as a service)
  • Serverless is recommeded for unpredicatable demands.
  • Serverless is offered by cloud providers,
    • AWS Lambda
    • Azure functions
    • Gcp functions
  • Serverless usecases:
    • To automate certain cloud scenarios
      • starting/shutting down the ec2 instances on schedule
      • integrational usecases.
    • Event based workflows
      • Image Processing
      • Defect processing
  • Generally serverless functions are executed on event triggers, Cloud providers offer inbuilt events that can trigger lambda
    • item added to s3 bucket/azure blob storage
    • ..
    • ..
  • Serverless functions are also used for workflow building.

AWS Lambda

  • Refer Here for official home page of aws lambda
  • AWS Lambda has some restrictions:
    • a function execution has a hard time limit which is 15 minutes.
Limit Type Description Maximum Value
Execution Time Maximum duration a function can run 15 minutes (900 seconds)
Concurrent Executions Default limit for concurrent executions per region 1,000
Invocation Frequency Maximum invocations per second (10x concurrent limit) 10,000
Memory Allocation Range of memory that can be allocated to a function 128 MB to 3 GB
Payload Size (Synchronous) Maximum event payload size for synchronous invocations 6 MB
Payload Size (Asynchronous) Maximum event payload size for asynchronous invocations 256 KB
Throttling Limits Rate at which AWS throttles requests Based on concurrent limits
API Rate Limits Maximum requests per second for specific APIs 100 requests/second (e.g., GetFunction)
  • Lambda functions can be written in various languages
    • python
    • javascript
    • java
    • C#
    • Ruby

Lambda functions in python.

  • Creating lambda function (Watch classroom video)
    Preview
  • Lambda function
from awslambdaric.lambda_context import LambdaContext
def lamda_handler(event:dict,context:LambdaContext) :
    # your functionality
    pass

  • The event argument contains the input which is of type dictionary
  • Lambda function components
    • event
    • trigger
    • handler
    • runtime
    • role (permissions to lambda functions)

Lets write a lambda function to calculate fd returns

  • solution
import json

def lambda_handler(event, context):
    principal = event['principal']
    rate = event['rate']/100
    time = event['time']
    n = 1
    maturity_amount = principal * (1 + rate / (100 * n)) ** (n * time)
    interest_earned = maturity_amount - principal
    return {
        'statusCode': 200,
        'body': {
            'maturity_amount': maturity_amount,
            'interest_earned': interest_earned

        }
    }

Azure Functions

  • Azure Functions offer serverless functionality in azure
  • Watch classroom recording

Goal

Preview

  • To do this we need to understand
    • how to use python sdk for aws (boto3) or azure
    • how to make http requests from python over REST API

Boto3

  • Boto3 is an SDK (Software Developement Kit) for python to interact with AWS.
  • Boto3 can be installed by using pip
pip install boto3
  • Boto3 expects credentials and if not provided tries to get cli credentials.
  • If the credentials are valid and if the user has required permissions, then aws boto3 works.
  • Watch recording for creating access key and secret key and selecting permissions

Core Components

  • Boto3 gives us two components to get the information about resources
    • client:
      • is used to connect to any service
      • here we have low level calls
    • Resource
      • is used to connect to any service
      • here we have high level object oriented calls
  • Boto3 API Reference

List all s3 buckets

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube