AWS Certified Developer - Associate
Storage
S3 Events
In this lesson, you'll learn how S3 events automatically trigger actions when specific events occur within an Amazon S3 bucket. For example, when a user uploads or deletes an object, S3 can generate an event that integrates with other AWS services such as Lambda, SNS, SQS, or EventBridge.
When a user uploads an object, S3 can trigger an event that either publishes a message to an SNS topic, starts a Lambda function, sends a message to an SQS queue, or integrates with EventBridge. For instance, if a user uploads a video, you can have S3 automatically invoke a Lambda function that processes and converts the video—removing the need for continuous polling.
Key Capabilities
S3 events offer significant flexibility. You can configure automated triggers for a variety of actions, including:
- Object creation (covering POST, COPY, and multi-part upload events)
- Object deletion
- Object restoration
- Object transitions (such as those triggered by lifecycle policies or changes in storage classes)
Below is an image showcasing the ten different types of Amazon S3 events that can generate notifications, including events for new object creation, removal, and lifecycle expiration:
Configuring S3 Events with a Lambda Function
In this demo, you'll set up S3 event notifications to trigger a simple Lambda function when an object is uploaded to an S3 bucket.
Step-by-Step Setup:
Create an S3 Bucket:
- Create your S3 bucket and navigate to its Properties section.
- Scroll down to the Event Notifications area.
- You have two notification options: use built-in event notifications or Amazon EventBridge. In this demo, choose to create an event notification.
Create Event Notification:
- Click on Create event notifications.
- Provide a name for the event (e.g., "new object uploaded").
- By default, the event applies to the entire bucket. Optionally, specify a prefix (to target specific directories) or suffix (to filter by file type, such as
.JPEG
or.PNG
).
Select Event Type:
- Choose the type of operation to trigger the event (e.g., object creation events). For this demo, select an event that applies to any object creation.
Choose the Destination:
- Select the destination for the event. Options include:
- Triggering a Lambda function
- Sending a message to an SNS topic
- Pushing a message to an SQS queue
For this example, select Lambda function.
- Select the destination for the event. Options include:
Lambda Function Setup
Even if you're new to Lambda, don’t worry. Lambda functions are simply pieces of code that execute in response to events. In this demo, you'll use a Lambda function with the simple code snippet below to log details of the S3 event:
export const handler = async (event) => {
console.log(event);
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda'),
};
return response;
};
After configuring the Lambda function:
- Select this function in the Lambda configuration.
- Set the event type to "new objects created" (or an appropriate equivalent).
- Click Save changes. S3 will update the Lambda function’s permissions to allow it to be triggered by S3 events.
Testing Your Configuration
Verify that your configuration is working by following these steps:
Upload an Object:
- In the S3 console, navigate to the Objects section.
- Upload a file (for example, an image of dogs) to your bucket.
Monitor the Event:
- After the upload, switch to the Monitoring tab and access CloudWatch logs.
- Open the latest log stream to review the output.
- Look for logged event details that include the event name, the S3 operation (such as a PUT event), and metadata related to the uploaded object.
The CloudWatch logs confirm that the Lambda function was successfully triggered by the S3 event, validating your configuration.
Summary
This demonstration illustrates how S3 events can seamlessly integrate with AWS Lambda to automate workflows based on bucket activities. With proper configuration, you can streamline processes such as file processing, data validation, and more, leveraging the power of AWS cloud services.
That concludes this lesson on S3 events. For further reading on S3 and related AWS services, refer to the AWS Documentation.
Watch Video
Watch video content