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









Sunday, June 22, 2025

Ec2 part2

Ec2 part2

Class 46th AWS EC2 June 16th

Load Balancer

Types of elastic load balancer

Target groups

Practical

Launch templates 

Auto scaling 7 types of scaling

Auto scaling groups

Automatic scaling policies

Practical

What is the ELB?

  • Elastic load balancing automatically distributes incoming application traffic across multiple targets, such as amazon Ec2 instances, containers, IP addresses , lambda functions, and virtual appliances 
  • It can handle the varying load of your application traffic in a single availability zone or across multiple

Users -->LoadBalancer-->Targetgroups-(round robin)-> Multiple Webappl Instances -->database

We have configured mapped domain to loadbalancer endpoint

Elastic Load balancing types:
Application load balancer :

Best for:websites,web apps, and APIs(Http/Https)
Understands URL paths and headers -can route based on /login, /image ,etc
Smart choice for modern web apps or micro service
Network load balancer 
Best for: High-spees and real-time apps(TCP,UDP)
Very fast handles millions for request per second 
Use when speed and high performance are critical,like game servers or financial apps
Gateway load balancer:
Best for :sending traffic to security appliances like firewalls,intrusion detection system(IDS), and traffic inspection tools.
Forwards traffic without modifying it,so security tools can inspect the full,original packets.
Use when you need to inspect or filter traffic before it reaches you applications like in secure VPC architectures or centralized firewall setups .
Classic load balancer: not used

Practical:

All our instances private subnet ,our load balance is public subnet 

Step1: Create three instance using Public subnet ,while creating instance add below code  Advance details User data

#!/bin/bash

# STEP1  -updates 

sudo yum update -y

# STEP2 -Begin Configuration 

sudo yum install -y httpd 

sudo systemctl start httpd

sudo systemctl enable httpd

# STEP3 Custom web page 

echo "<h1>Welcome to CCIT Server 1 </h1>" >>/var/www/html/index.html 

change last line  remaining two servers For Server2 Second instance and Server3 for third instance

echo "<h1>Welcome to CCIT Server 2 </h1>" >>/var/www/html/index.html 

echo "<h1>Welcome to CCIT Server 3 </h1>" >>/var/www/html/index.html 

Step2:Three instances created ,Ec2-Instance1,Ec2-Instance2,Ec2-Instance3

Step3:Instance public ip able to access webpage server1,server2,server3 three tabs 
Step4:Need to create target group attach the instances to target group
Ec2>Target groups >create target groups ,Select first option instance given name target group
Target group name: CCIT-TG
Health checks :Here below User access the webpage, it will hit the load balancer distributed the traffice equally all webservers.

Advanced health check settings: default setup load balancer the webserver http port 80, every 5 seconds
and it will check 2 time, every 5 seconds interval consequently check server health check ,Success code is 200,Click Next
                                                            

Click Including as pending below and then Click create target group

It is take some time come health status unused to used in target group , it was dependency with load balancer, so need create load balancer

Step5: Creating load balancer 
Ec2>Load balancer >Choose Load balancer types  >Application Load Balancer
Given any name ccit-loadbalance, All your instance should be in public choose your subnet public

Choose select target group: here http request 80 will go target group, target group based on health check it will go the request. then click Create load balancer, it will take some time to active


Load balancer is now active 

And target groups are now healthy, if any unhealthy instance we can create auto scale, it will create automatically when one instance has issues.

Step6: take the dns name from  the load balancer end point http://ccit-loadbalance-1595733045.eu-west-2.elb.amazonaws.com/
After refresh multiple time same dns name ,the load is divided equally three server automatically



Step7:If you want, you can set these into route 53, How to buy the domain go daddy.. plat form and register the domain, for you domain add the record endpoint loadbalancer,it will give unique http: name.If you want https:// we have once concept ASM certificate in aws  it will cover there.

Buying Domain in Go daddy



https://vakatisubbu.xyz/ Successfully brought the domain

Step8: Please the endpoint url from the loadbalancer step5 active loadbalancer, these dns is http://
DNSName:
ccit-loadbalancer-1607206506.eu-west-2.elb.amazonaws.com

                                                            Route53
Route 53 >Hosted zones Create host zone ,give you domain name which was purchased in godaddy click create hosted zone


Created successful Hosted zone for the domain


Here click create record 
Click create record

Created record successfully.

We have create just domain today it will take 48 hour to complete, as of now, only came domain default page 


ASM certificate
Step1:
Step2: give you domain name ,click request

Step3:Click  Create record route 53

click create records
Step4:
one record is adding in the hosted zone


Step5: After Pending validation success ,you got one SSL certificate





Loader Balance Completed.
Step8: Delete the load balancer deleted, plan to create autoscaling
                                                              Auto Scaling 
Aws auto scaling monitors your applications and automatically adjusts capacity to maintain steady, predictable performance at the lowest possible cost.
Using AWS auto scaling, it’s easy to setup application scaling for multiple resources across multiple services in minutes.
Vertical: Able to increase the capacity of the instance called vertical, no down time required.
Horizontal scaling :Able increase the instances not capacity of the instance is called Horizonal scaling,
down time required.

Existing Target group Health checks success code 200,2 consecutive check unhealth means the instance is unhealthy ,which our autoscale trigger create one instance automatically.

Delete the existing Target group, Terminate three instance, We will create instance you launch template.

Step1:Create Launch Template
EC2>Launch template >create launch template given any name ccit-launchtemplate,
here im not setting network, plan to setup in autoscaling group
User data add you http script code click create launch template

Created templated, below showing default version 1,latest version 1, if you do any change on launch template again new version will create not replaced existing version.
Step2: Create Autoscaling group
Ec2>Auto scaling group >Create Auto scaling group