Skip to content

Commit 0210d0b

Browse files
authored
Merge branch 'main' into Dictionary-app
2 parents 7bb47ef + 893bfb6 commit 0210d0b

File tree

211 files changed

+17078
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

211 files changed

+17078
-18
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ At CodeHarborHub, our mission is clear: to provide accessible and comprehensive
2424
- **Inspiring Showcases**: Explore inspiring showcases of real-world projects and innovative web solutions.
2525
- **Engaging Community**: Connect with a vibrant community of developers, share knowledge, and collaborate on projects.
2626

27+
## Announcement:
28+
29+
**New Project:** Loan Calculator! Check it out on [LinkedIn](https://www.linkedin.com/posts/ajay-dhangar_loan-calculator-activity-7221402171957768195-1WYs?utm_source=share&utm_medium=member_desktop) 🚀
30+
2731
## Discord Channel for GSSoC 2024 Contributors
2832

2933
Now, resolve your all doubts and communicate with our all contributors.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
---
2+
title: 'Getting Started with Serverless Architecture Using AWS Lambda'
3+
sidebar_label: Serverless Architecture and AWS Lambda
4+
authors: [nayanika-mukherjee]
5+
tags: [serverless, AWS Lambda, cloud computing, Python, technology]
6+
date: 2024-07-22
7+
hide_table_of_contents: true
8+
---
9+
10+
## Introduction
11+
12+
Serverless architecture is a cloud computing execution model where the cloud provider dynamically manages the allocation and provisioning of servers. AWS Lambda, a key component of serverless architecture, allows you to run code without provisioning or managing servers. This guide will introduce you to AWS Lambda and provide a step-by-step approach to getting started with serverless architecture.
13+
14+
## Key Concepts
15+
16+
### What is AWS Lambda?
17+
18+
AWS Lambda is a compute service that lets you run code in response to events and automatically manages the compute resources required by that code. You pay only for the compute time you consume.
19+
20+
### Serverless Benefits
21+
22+
- **No Server Management:** No need to provision or manage servers.
23+
- **Scalability:** Automatically scales your application by running code in response to each trigger.
24+
- **Cost Efficiency:** Pay only for the compute time you consume.
25+
26+
### Event Sources
27+
28+
Lambda can be triggered by various AWS services such as S3, DynamoDB, Kinesis, SNS, and more.
29+
30+
## Setting Up AWS Lambda
31+
32+
### Prerequisites
33+
34+
- An AWS account.
35+
- AWS CLI installed and configured.
36+
- Basic knowledge of Python (or the language you choose for your Lambda functions).
37+
38+
### Creating an IAM Role
39+
40+
Before creating a Lambda function, you need an IAM role that Lambda assumes when it executes your function.
41+
42+
```bash
43+
aws iam create-role --role-name lambda-execution-role --assume-role-policy-document file://trust-policy.json
44+
```
45+
`trust-policy.json`:
46+
```json
47+
{
48+
"Version": "2012-10-17",
49+
"Statement": [
50+
{
51+
"Effect": "Allow",
52+
"Principal": {
53+
"Service": "lambda.amazonaws.com"
54+
},
55+
"Action": "sts:AssumeRole"
56+
}
57+
]
58+
}
59+
```
60+
Attach the necessary policies to the role:
61+
```bash
62+
aws iam attach-role-policy --role-name lambda-execution-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
63+
```
64+
65+
## Writing and Deploying Lambda Functions
66+
67+
### Basic Lambda Function
68+
Here is a simple Python function that returns a greeting.
69+
70+
`lambda_function.py`:
71+
```python
72+
def lambda_handler(event, context):
73+
return {
74+
'statusCode': 200,
75+
'body': 'Hello, World!'
76+
}
77+
```
78+
79+
### Creating and Deploying the Function
80+
- Create a ZIP file containing your code:
81+
```bash
82+
zip function.zip lambda_function.py
83+
```
84+
85+
- Deploy the Lambda Function:
86+
```bash
87+
aws lambda create-function --function-name HelloWorldFunction \
88+
--zip-file fileb://function.zip --handler lambda_function.lambda_handler \
89+
--runtime python3.8 --role arn:aws:iam::123456789012:role/lambda-execution-role
90+
```
91+
92+
## Lambda Execution Model
93+
94+
Lambda functions have an execution model that includes:
95+
96+
- Invocation: Functions can be invoked synchronously or asynchronously.
97+
- Concurrency: Lambda automatically scales to handle the incoming requests.
98+
- Execution Duration: You can configure the timeout for your function (default is 3 seconds, maximum is 15 minutes).
99+
100+
## Managing Lambda Functions
101+
- Updating a Function
102+
To update the function code:
103+
```bash
104+
zip function.zip lambda_function.py
105+
aws lambda update-function-code --function-name HelloWorldFunction --zip-file fileb://function.zip
106+
```
107+
108+
- Monitoring and Logging
109+
AWS Lambda integrates with Amazon CloudWatch to provide monitoring and logging. You can view logs by navigating to the CloudWatch Logs in the AWS Management Console.
110+
111+
## Advanced Topics
112+
113+
### Environment Variables
114+
You can use environment variables to pass configuration settings to your Lambda function.
115+
```bash
116+
aws lambda update-function-configuration --function-name HelloWorldFunction \
117+
--environment "Variables={ENV_VAR1=value1,ENV_VAR2=value2}"
118+
```
119+
### Layers
120+
Lambda layers allow you to package libraries and other dependencies separately from your function code.
121+
122+
- Create a layer:
123+
```bash
124+
zip -r myLayer.zip python/
125+
aws lambda publish-layer-version --layer-name myLayer --zip-file fileb://myLayer.zip
126+
```
127+
### VPC Integration
128+
You can configure your Lambda function to access resources in a VPC.
129+
```bash
130+
aws lambda update-function-configuration --function-name HelloWorldFunction \
131+
--vpc-config SubnetIds=subnet-abc123,SecurityGroupIds=sg-abc123
132+
```
133+
134+
## Performance and Scaling
135+
136+
### Cold Starts
137+
A cold start occurs when a new instance of the function is invoked after being idle. To mitigate cold starts:
138+
139+
- Optimize initialization code.
140+
- Use Provisioned Concurrency for predictable performance.
141+
142+
### Concurrency Limits
143+
You can configure reserved concurrency to limit the number of concurrent executions:
144+
```bash
145+
aws lambda put-function-concurrency --function-name HelloWorldFunction --reserved-concurrent-executions 10
146+
```
147+
## Testing and Debugging
148+
149+
### Local Testing
150+
Use the AWS SAM CLI to test Lambda functions locally:
151+
```bash
152+
sam local invoke HelloWorldFunction -e event.json
153+
```
154+
155+
### Debugging
156+
Utilize CloudWatch Logs to debug issues by adding log statements in your code:
157+
```python
158+
import logging
159+
logger = logging.getLogger()
160+
logger.setLevel(logging.INFO)
161+
162+
def lambda_handler(event, context):
163+
logger.info("Event: %s", event)
164+
return {
165+
'statusCode': 200,
166+
'body': 'Hello, World!'
167+
}
168+
```
169+
## Real-World Examples
170+
171+
### S3 Event Trigger
172+
Trigger a Lambda function when an object is uploaded to an S3 bucket:
173+
```python
174+
import json
175+
176+
def lambda_handler(event, context):
177+
for record in event['Records']:
178+
s3 = record['s3']
179+
bucket = s3['bucket']['name']
180+
key = s3['object']['key']
181+
print(f'Received event. Bucket: {bucket}, Key: {key}')
182+
return {
183+
'statusCode': 200,
184+
'body': json.dumps('Processed S3 event')
185+
}
186+
```
187+
188+
### DynamoDB Stream
189+
Process DynamoDB stream events:
190+
```python
191+
import json
192+
193+
def lambda_handler(event, context):
194+
for record in event['Records']:
195+
if record['eventName'] == 'INSERT':
196+
new_image = record['dynamodb']['NewImage']
197+
print(f'New item added: {new_image}')
198+
return {
199+
'statusCode': 200,
200+
'body': json.dumps('Processed DynamoDB stream event')
201+
}
202+
```
203+
204+
## Conclusion
205+
206+
AWS Lambda provides a powerful and flexible way to build serverless applications. By understanding its key concepts, setting up your environment, and leveraging advanced features, you can build scalable and cost-efficient solutions. This guide serves as a starting point for your journey into serverless architecture using AWS Lambda.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
id: lesson-1
3+
title: "Advanced File Handling in C++"
4+
sidebar_label: Advanced File Handling
5+
sidebar_position: 1
6+
description: "Learn Advanced File Handling in C++"
7+
tags: [courses,Advance-level,Introduction]
8+
---
9+
10+
11+
Advanced file handling techniques allow for better control, performance optimization, and handling of large files. Let's explore these techniques:
12+
13+
### Flowchart
14+
15+
```mermaid
16+
graph TD;
17+
A[Start] --> B[File Locking Mechanisms];
18+
B --> C[File I/O Performance Optimization];
19+
C --> D[Handling Large Files];
20+
D --> E[Working with Memory-Mapped Files];
21+
E --> F[End];
22+
```
23+
24+
#### 1. File Locking Mechanisms
25+
26+
File locking prevents simultaneous access to a file, which can cause data corruption. It ensures that only one process can write to a file at a time.
27+
28+
##### Example: File Locking with `fcntl`
29+
30+
```cpp
31+
#include <iostream>
32+
#include <fcntl.h>
33+
#include <unistd.h>
34+
35+
int main() {
36+
int fd = open("example.txt", O_RDWR | O_CREAT, 0666);
37+
if (fd == -1) {
38+
std::cerr << "Failed to open file" << std::endl;
39+
return 1;
40+
}
41+
42+
struct flock lock;
43+
lock.l_type = F_WRLCK;
44+
lock.l_whence = SEEK_SET;
45+
lock.l_start = 0;
46+
lock.l_len = 0; // Lock the whole file
47+
48+
if (fcntl(fd, F_SETLK, &lock) == -1) {
49+
std::cerr << "Failed to lock file" << std::endl;
50+
close(fd);
51+
return 1;
52+
}
53+
54+
std::cout << "File locked. Press Enter to unlock..." << std::endl;
55+
std::cin.get();
56+
57+
lock.l_type = F_UNLCK;
58+
if (fcntl(fd, F_SETLK, &lock) == -1) {
59+
std::cerr << "Failed to unlock file" << std::endl;
60+
}
61+
62+
close(fd);
63+
return 0;
64+
}
65+
```
66+
67+
#### 2. File I/O Performance Optimization
68+
69+
Optimizing file I/O can significantly improve performance, especially when dealing with large files or high-frequency I/O operations.
70+
71+
:::tip
72+
- **Buffering**: Use buffered I/O for efficient data handling.
73+
- **Asynchronous I/O**: Perform non-blocking I/O operations to avoid waiting for I/O completion.
74+
- **Memory Mapping**: Use memory-mapped files for direct memory access to file contents.
75+
:::
76+
77+
#### 3. Handling Large Files
78+
79+
Handling large files efficiently is crucial for applications that deal with massive datasets.
80+
81+
##### Example: Reading Large Files in Chunks
82+
83+
```cpp
84+
#include <iostream>
85+
#include <fstream>
86+
87+
int main() {
88+
std::ifstream file("largefile.txt", std::ios::in | std::ios::binary);
89+
if (!file) {
90+
std::cerr << "Failed to open file" << std::endl;
91+
return 1;
92+
}
93+
94+
const size_t bufferSize = 1024 * 1024; // 1 MB buffer
95+
char buffer[bufferSize];
96+
97+
while (file.read(buffer, bufferSize) || file.gcount() > 0) {
98+
// Process buffer contents
99+
std::cout.write(buffer, file.gcount());
100+
}
101+
102+
file.close();
103+
return 0;
104+
}
105+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Advance File Handling",
3+
"position": 5,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Learn Advance File Handling."
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
id: lesson-2
3+
title: "Working with Memory-Mapped Files"
4+
sidebar_label: Working with Memory-Mapped Files
5+
sidebar_position: 2
6+
description: "Learn Working with Memory-Mapped Files"
7+
tags: [courses,Advance-level,Introduction]
8+
---
9+
10+
11+
Memory-mapped files allow for efficient file access by mapping a file's contents to memory. This technique is useful for large files and random access patterns.
12+
13+
##### Example: Memory-Mapped File Access
14+
15+
```cpp
16+
#include <iostream>
17+
#include <fcntl.h>
18+
#include <sys/mman.h>
19+
#include <sys/stat.h>
20+
#include <unistd.h>
21+
22+
int main() {
23+
int fd = open("largefile.txt", O_RDONLY);
24+
if (fd == -1) {
25+
std::cerr << "Failed to open file" << std::endl;
26+
return 1;
27+
}
28+
29+
struct stat sb;
30+
if (fstat(fd, &sb) == -1) {
31+
std::cerr << "Failed to get file size" << std::endl;
32+
close(fd);
33+
return 1;
34+
}
35+
36+
size_t fileSize = sb.st_size;
37+
char *mapped = static_cast<char *>(mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0));
38+
if (mapped == MAP_FAILED) {
39+
std::cerr << "Failed to map file" << std::endl;
40+
close(fd);
41+
return 1;
42+
}
43+
44+
// Access the file content through the mapped memory
45+
std::cout.write(mapped, fileSize);
46+
47+
if (munmap(mapped, fileSize) == -1) {
48+
std::cerr << "Failed to unmap file" << std::endl;
49+
}
50+
51+
close(fd);
52+
return 0;
53+
}
54+
```
55+
:::tip
56+
- **File Locking**: Use file locking mechanisms to prevent concurrent write operations.
57+
- **Buffered I/O**: Implement buffering to reduce the number of I/O operations.
58+
- **Asynchronous I/O**: Utilize asynchronous I/O for non-blocking operations.
59+
- **Memory Mapping**: Use memory-mapped files for efficient access to large files.
60+
:::
61+
62+

0 commit comments

Comments
 (0)