Wednesday, June 25, 2025

Dynamodb lambda Part1

Dynamodb Part1

Class 52nd AWS Dynamo db June 25th

  • Dynamodb
  • Lambda

Practical: 

Login page create using python code, database MySQL and dynamodb

https://github.com/Vakatisubbu/DigitalLibrary/

Step1: Create one Ec2-instance Windows 

Public:35.177.182.236 Private:172.31.35.150

Step2: Create RDS database free tier Mysql

Python download and install in Remote desktop windows

https://www.python.org/downloads/


Step3: Copy the Git digitalLibrary

Mysql RDS create succefully


db_config = { 'host': 'msql-sql.csn64oem2jvs.us-east-1.rds.amazonaws.com', 'user': 'admin', 'password': 'admin12345', 'database': 'digital_library' }

-- 1. Create the database

CREATE DATABASE IF NOT EXISTS digital_library;
-- 2. Use the database
USE digital_library;
-- 3. Create the 'books' table
CREATE TABLE books (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255),
    author VARCHAR(255),
    available BOOLEAN DEFAULT TRUE
);
-- 4. Insert 10 sample book records
INSERT INTO books (title, author, available) VALUES
('To Kill a Mockingbird', 'Harper Lee', TRUE),
('1984', 'George Orwell', TRUE),
('The Great Gatsby', 'F. Scott Fitzgerald', FALSE),
('Pride and Prejudice', 'Jane Austen', TRUE),
('The Catcher in the Rye', 'J.D. Salinger', TRUE),
('The Hobbit', 'J.R.R. Tolkien', FALSE),
('The Alchemist', 'Paulo Coelho', TRUE),
('The Da Vinci Code', 'Dan Brown', TRUE),
('The Kite Runner', 'Khaled Hosseini', TRUE),
('Harry Potter and the Sorcerer\'s Stone', 'J.K. Rowling', FALSE);
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255),
    mobile VARCHAR(15),
    email VARCHAR(255),
    password VARCHAR(255),
    gender VARCHAR(10),
    location VARCHAR(255),
    image VARCHAR(255)
);

CREATE TABLE history ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT, book_id INT, borrow_date DATETIME, return_date DATETIME, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE );

Step4: you have to run the app.py file in remote desktop

C:\DigitalLibrary>python app.py Traceback (most recent call last): File "C:\DigitalLibrary\app.py", line 1, in <module> from flask import Flask, request, render_template, redirect, session, url_for, flash ModuleNotFoundError: No module named 'flask'

getting error you need install the below package prior to that

pip install flask
pip install mysql-connector-python
pip install requests
pip install boto3

Step5:run the app.py file ,signup with detail , and login with out details.

C:\DigitalLibrary>python app.py
 * Serving Flask app 'app'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://172.31.35.150:5000


Step6: once you singup the details are stored in the local mysql


Step7: sign in with your credential and user

Click go to page ,try to borrow some book and retrun some books and test 


Step8:mysql history table data was captured.



Practical with Dynamo db
Previous example we have used mysql,need to mention all the tables creation prior insert,
dynamodb we have more flexible ,Horizontal scaling 
Step1:Create Dynamo db users table 
here we have given two columns only id,email


Step2:Create table books id,sortkey title create table 

Step3:Create table history,sortkey book_id create table 

Step4: Tables are create here the problem is id auto increase not exists ware as mysql ,we have to give id manually.


Step5:Need to Create iam user, i have already admin user access and screate key exists 
DynamoDB put_item, get_item (item is record or row).See here we have create two column only for the users table in dynamo db 
Inserted two records
Lambda

Topic :it is complete AWS developer related service free service and storage also ,no of hits based on will charge 1 year 10million hits will free.(we can run the lambda function using other service,cloud watch,ap gateway ..etc)
  • What is the serverless computing?
Server is exist but completely maintained by  aws (serverless service Dynamdb). In AWS serverless refers to a cloud computing model where you don’t need to manage the underlying server infrastructure.
Instead you focus on writing code and defining how your application should run. AWS handles the provisioning, scaling, and management of servers automatically.
     Popular Serverless service in AWS
     AWS Lambda: For running code in response to events.
     Amazon API gateway: For create and managing APIs.
     Amazon DynamoDB: A serverless Nosql database
     AWS step Functions: For coordinating and managing workflows.
    Amazon s3:Oject storage that can trigger events for serverless processing.

Here’s How the serverless model works in AWS:

No server management: You don’t need to worry about provisioning, configuring, or managing servers. AWS takes care of the infrastructure needed to run your application

Automatic Scaling: Serverless service automatically scale up or down based on demand. This means you application can handle varying loads without manual intervention.

Event Driven: Serverless architecture often work with event. For instance, AWS lambda functions are triggered by events such as changes in data, HTTP requests or message in a queues.

Pay as you go: You only pay for actually use, based on the number of request and the duration of code execution,rather than paying for a fixed amount of server capacity.

Built in high Availability:Serverless services are designed to be highly available, with redundancy and fault tolerance built in,so you don’t need to manage this yourself.

Focus on code: Developers can focus on writing and deploying  code rather than managing servers  or scaling or scaling infrastructure. This accelerated development and reduces operation complexity

  • Aws Lambda introduction. 
AWS Lambds is a serverless computing service provided  by Amazon web service(AWS), it lets you run code without provisioning or managing servers.
You simply upload you code and lambda takes care of everything required to run and scale it with higl availability
  • Cold start in AWS Lambda
  • Environment variables configuration
  • Role importance in the lambda function
  • Create a simple lambda function and trigger with test event
  • Event driven architecture.

You can able create function :.Net,java,Python,Node.js,Ruby, custom runtime if any other application
lambda function default  cloud watch monitoring attached the policy

Practical:

Step1:Create lambda function, need role for the execute the function , if not there it will cerate new role.

Click IM console create rule for lambda, after click  Next add permission  AWSLambda_FullAccess
then click next 
given any name Lambda-role, click create role ,in your create function choose exist role select create function

Step2: if you are python developer you writ the code in function it self ,if other language .dot net you upload the code in zip max size 10MB, if more than that you need upload to S3 bucket.

see above in the default code, if you change any single space line, you should need to deploy the code 
then only the change will reflect, if not the change will loss.
Just given one space ,click deploy, you can able test you code ,above screen tab test 
Click test ,succeeded output came

Step3: you can call the lambda  function using other service, for using trigger we can use the function

Step4:select the API Gateway
Choose below option click add

Step5: Select the tab Configuration select triggers ,take the endpoint 

Step6:So using other service we called lambda function,(API Gateway is one of the service)
Step7:API Gate way one of the best example server less computing ,using these server we have lambda function
Step8:You can able change configuration of lambda Click edit 


Step9:Storage service  memory maximum 10Gb capacity ,Ephemeral storage /tmp  temporary only 10 GB,Timeout period for endpoint  maximum 900 second , after that container will destroy and recreate after call again endpoint

Step10: lambda function one role was created ,only communicate lambda function using that role permission only, if you want any RDS related service you can use in the lambda functon
you need to attach the permission for that rule, else lambda function will fail, like this way you add the permission.


Step11:Cloud watch automatically captured,whenevery using endpoint 

Step12: We can able debug put print statement in the between python code the text will display in cloud watch,i have added subbu, added and deployed and call the endpoint automatically captured in the log event in  cloud watch

import json

def lambda_handler(event, context):
    # TODO implement
    print('subbu')
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from  Lambda!')
    }

Step13: we can able wait our code 5 second using below command ,if not lambda function timed out.
time.sleep(5)


--Thanks









No comments:

Post a Comment