تعلم واحترف Lambda Service خلال ١٨ دقيقة!
تطبيق عملي يغطي كل مزايا الخدمة
رفع ملفات إلى S3 التي بدورها تفعل Lambda function, الذي يقوم بقراءة وتعديل الملف, وكتابة الملف الجديد مرة اخرى إلى S3
رح نتعلم, كيف نكتب ونشغل ونراقب اداء Lambda function بالاضافة إلى تعلم كيفية انشاء Trigger واستخدام Lambda function test tools
Learning Outcomes
- Write lambda function
- Monitor lambda function
- Add lambda function trigger
- Test lambda using lambda test tools
المحتوى
What is Lambda Function 0:01
Lambda Function Use Cases 0:42
تطبيق عملي Example 1:16
خطوات التطبيق:
- Create S3 Bucket 2:31
- Create Service Role 3:28
- Create Lambda Function 4:06
- Add Lambda Trigger 9:25
- Write Lambda Function Code 11:17
- Test Lambda Function 13:25
Clean up resources 17:17
Next! 18:05
حلقات سلسلة تعلم AWS للمبتدئين
Lambda Tutorial | Beginners to Advanced | Lambda Functions Tutorial
Lambda Function Code:
"
import json
import urllib.parse
import boto3
s3 = boto3.client('s3')
def lambda_handler(event, context):
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
try:
response = s3.get_object(Bucket=bucket, Key=key)
# process data: replace every : with -
data = []
for line in response["Body"].read().splitlines():
each_line = line.decode('utf-8')
print(each_line)
modified_line = each_line.replace(':', '-')
data.append(modified_line)
# Write modified data to a new file
new_key = 'output.txt'
r = s3.put_object(Body='\n'.join(data), Bucket=bucket, Key=new_key)
return response['ContentType']
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e
"