Pemicu Peristiwa S3 dan SQS
Pelajari cara memicu fungsi Lambda sebagai respons terhadap peristiwa objek Amazon S3 (misalnya unggahan) dan pesan dari antrean Amazon SQS untuk mendukung alur kerja pemrosesan data.
Pemicu Peristiwa S3 dan SQS adalah pelajaran Serverless AWS Lambda Development gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Serverless AWS Lambda Development, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Serverless AWS Lambda Development mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
Events Drive Serverless
Serverless functions, like AWS Lambda, don't just run on their own. They wait for something to happen! This "something" is called an event.
An event is a signal from another service that tells your Lambda function to execute. Think of it like a doorbell ringing for your function to answer.
S3 Bucket Events
Amazon S3 (Simple Storage Service) is a popular cloud storage service. It can generate events when objects are created, deleted, or modified in your buckets.
- Object Created (PUT): An image is uploaded.
- Object Deleted: A file is removed.
- Object Restored: An archived item is brought back.
These events can trigger a Lambda function to process the changes.
Connecting S3 to Lambda
To link an S3 bucket to a Lambda function, you define a trigger. This tells S3 which events to listen for and which Lambda function to invoke.
- You specify the S3 bucket.
- You choose the event types (e.g.,
s3:ObjectCreated:*). - You can filter by object prefix or suffix (e.g., only trigger for
images/folder or.jpgfiles).
What S3 Sends to Lambda
When an S3 event triggers your Lambda function, the function receives a JSON object containing details about the event. This object is the "event" parameter in your function handler.
Key information includes:
- Bucket Name: Where the event occurred.
- Object Key: The full path and name of the file affected.
- Event Name: The specific action (e.g.,
ObjectCreated:Put).
Processing an S3 Upload
Here's a Python Lambda function that processes an S3 object creation event. It extracts and prints the bucket name and the key of the uploaded object.
Try uploading a file to an S3 bucket configured to trigger this function!
import json
def lambda_handler(event, context):
for record in event['Records']:
bucket_name = record['s3']['bucket']['name']
object_key = record['s3']['object']['key']
event_name = record['eventName']
print(f"Event: {event_name}")
print(f"Bucket: {bucket_name}")
print(f"Object Key: {object_key}")
return {
'statusCode': 200,
'body': json.dumps('S3 event processed!')
}SQS Queue Messages
Amazon SQS (Simple Queue Service) is a message queuing service. It allows different parts of your application to communicate asynchronously without direct interaction.
When messages are sent to an SQS queue, they can trigger a Lambda function. This is great for tasks that can be processed later or in batches, like sending emails or processing orders.
Connecting SQS to Lambda
Setting up an SQS trigger for Lambda is straightforward. Lambda continuously polls the SQS queue for new messages.
- You specify the SQS queue.
- You define a batch size (1 to 10,000 messages). This determines how many messages Lambda tries to process in a single invocation.
- Lambda deletes messages from the queue only after successful processing.
What SQS Sends to Lambda
Similar to S3, an SQS event passed to your Lambda function is a JSON object. It contains an array of Records, each representing a message from the queue.
Each record includes:
- Message Body: The actual content of the message.
- Message Attributes: Optional metadata about the message.
- Receipt Handle: Used internally by SQS for message deletion.
Processing SQS Messages
This Python Lambda function processes messages from an SQS queue. It iterates through the batch of messages received and prints their content.
Imagine each message contains a task to perform!
import json
def lambda_handler(event, context):
for record in event['Records']:
message_body = record['body']
print(f"Received message: {message_body}")
# Add your processing logic here
return {
'statusCode': 200,
'body': json.dumps('SQS messages processed!')
}S3 vs. SQS Triggers
You've learned about two powerful event sources for AWS Lambda: S3 and SQS. They serve different purposes but both enable event-driven architectures.
Which of the following statements are TRUE regarding S3 and SQS Lambda triggers?
Recap: S3 & SQS Triggers
Great job! You've explored how AWS Lambda functions can be triggered by events from Amazon S3 and SQS.
- S3 triggers are perfect for reacting to file changes, like image uploads or document processing.
- SQS triggers enable asynchronous processing of messages, allowing for decoupled and scalable workflows.
These event sources are fundamental to building powerful, reactive serverless applications!
Belajar Serverless AWS Lambda Development dengan tutor AI — gratis
Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.
- Kursus
- 12
- Pelajaran
- 48
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Pemicu Peristiwa S3 dan SQS” gratis?
Ya — teks lengkap “Pemicu Peristiwa S3 dan SQS” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Serverless AWS Lambda Development, upgrade ke CoddyKit PRO. Kursus Serverless AWS Lambda Development mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Pemicu Peristiwa S3 dan SQS”?
Pelajari cara memicu fungsi Lambda sebagai respons terhadap peristiwa objek Amazon S3 (misalnya unggahan) dan pesan dari antrean Amazon SQS untuk mendukung alur kerja pemrosesan data. Kamu berlatih Serverless AWS Lambda Development dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Serverless AWS Lambda Development?
Tidak diperlukan pengalaman sebelumnya. Serverless AWS Lambda Development di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.
Berapa lama pelajaran “Pemicu Peristiwa S3 dan SQS” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Serverless AWS Lambda Development ini?
Ya. Setiap pelajaran Serverless AWS Lambda Development menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Dasar-Dasar Arsitektur Berbasis Peristiwa
- Memanggil Lambda dengan API Gateway
- Pemicu Peristiwa S3 dan SQS
- Pemanggilan Terjadwal dengan EventBridge