How to Use AWS S3 with Lambda: Simple Guide
Use
AWS Lambda to run code triggered by S3 events like file uploads. Configure an S3 event trigger in Lambda and use the AWS SDK inside your function to read or write S3 objects.Syntax
To use S3 with Lambda, you set up an event trigger on an S3 bucket and write a Lambda function that handles the event. The function receives event data including bucket name and object key. Use the AWS SDK to interact with S3 inside the function.
- S3 Event Trigger: Connects S3 bucket events (like object created) to Lambda.
- Lambda Handler: The function code that runs when triggered.
- AWS SDK S3 Client: Used inside Lambda to get or put objects.
javascript
exports.handler = async (event) => { const bucket = event.Records[0].s3.bucket.name; const key = event.Records[0].s3.object.key; // Use AWS SDK to access S3 };
Example
This example shows a Lambda function triggered by an S3 upload event. It reads the uploaded file's content and logs it.
javascript
const AWS = require('aws-sdk'); const s3 = new AWS.S3(); exports.handler = async (event) => { const bucket = event.Records[0].s3.bucket.name; const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); try { const params = { Bucket: bucket, Key: key }; const data = await s3.getObject(params).promise(); const fileContent = data.Body.toString('utf-8'); console.log('File content:', fileContent); return { statusCode: 200, body: 'Success' }; } catch (err) { console.error('Error getting object:', err); throw err; } };
Output
File content: (contents of the uploaded file logged in CloudWatch Logs)
Common Pitfalls
- Not setting correct IAM permissions for Lambda to access S3 causes access errors.
- Forgetting to decode the S3 object key can cause errors with spaces or special characters.
- Not configuring the S3 event trigger properly means Lambda won't run on uploads.
- Using synchronous SDK calls instead of async/await can cause timeouts.
javascript
/* Wrong: Missing permissions and no decoding */ exports.handler = async (event) => { const bucket = event.Records[0].s3.bucket.name; const key = event.Records[0].s3.object.key; // Not decoded // This may fail if Lambda lacks S3 read permission }; /* Right: Decode key and ensure IAM role has S3 read access */ exports.handler = async (event) => { const bucket = event.Records[0].s3.bucket.name; const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); // Use AWS SDK with proper permissions };
Quick Reference
Remember these key points when using S3 with Lambda:
- Set S3 event triggers for Lambda on bucket events.
- Use AWS SDK inside Lambda to access S3 objects.
- Decode object keys from events to handle special characters.
- Grant Lambda IAM role permissions for S3 actions.
- Test with CloudWatch Logs to debug Lambda executions.
Key Takeaways
Configure S3 event triggers to invoke Lambda on file events.
Use AWS SDK inside Lambda to read or write S3 objects asynchronously.
Always decode S3 object keys from event data to handle special characters.
Ensure Lambda's IAM role has permissions to access the S3 bucket.
Check CloudWatch Logs to monitor Lambda execution and troubleshoot.