Aws Lambda that Reacts to S3 File Upload

Aparna Rathore
2 min readJul 24


To create an AWS Lambda function that reacts to S3 file uploads, you can use the AWS SDK for Node.js. The Lambda function will be triggered whenever a new file is uploaded to a specific S3 bucket. Here’s a step-by-step guide on how to achieve this:

Step 1: Set Up an S3 Bucket
If you don’t have an S3 bucket already, create one in the AWS Management Console. Note down the bucket name as you’ll need it later.

Step 2: Create a Lambda Function
1. Go to the AWS Lambda console and click on “Create function.”
2. Choose “Author from scratch,” and provide a name, runtime (Node.js 14.x or higher), and an appropriate execution role with the necessary permissions to access S3.

Step 3: Add Trigger
1. In the Lambda function designer, click on “Add trigger.”
2. Select “S3” from the trigger options.
3. Choose the S3 bucket you created earlier.
4. For “Event type,” select “All object create events” or choose specific events based on your requirement.
5. Enable trigger now and click “Add.”

Step 4: Write the Lambda Function Code
In the Function code section of the Lambda console, write the Node.js code to react to the S3 file upload. Here’s a basic example:

const AWS = require(‘aws-sdk’);

exports.handler = async (event) => {
// Log the event data to CloudWatch for testing/debugging
console.log(‘Received S3 event:’, JSON.stringify(event, null, 2));

const s3 = new AWS.S3();

// Process each record in the event (multiple if batch upload)
for (const record of event.Records) {
// Get the bucket name and file key (object key) from the event
const { bucket: { name }, object: { key } } = record.s3;

// Perform actions based on the file upload, e.g., read the file from S3
// and process the content or trigger other AWS services.
// For example:
const params = {
Bucket: name,
Key: key,

try {
const data = await s3.getObject(params).promise();
// Process the data here…
console.log(‘File content:’, data.Body.toString());
} catch (err) {
console.error(‘Error reading file from S3:’, err);

return {
statusCode: 200,
body: JSON.stringify(‘File upload processing complete’),

Step 5: Save and Test the Lambda Function
Click “Save” to save the Lambda function. You can now test the Lambda function by uploading a file to the S3 bucket specified in the trigger.

When a new file is uploaded to the S3 bucket, the Lambda function will be triggered, and it will process the uploaded file based on your code implementation.

Please note that this is a basic example, and in a real-world scenario, you may need to handle error cases, use appropriate authentication, and implement the desired file processing logic based on your use case. Additionally, ensure that your Lambda function has the necessary permissions to read from the S3 bucket and write any desired outputs.



Aparna Rathore

A Tiny Girl with not so many tiny dreams.