Setting up Trigger Lambda on S3 Put Event Using Node.js: A Step-by-Step Guide with Code
Amazon Simple Storage Service (S3) is a popular cloud storage service offered by Amazon Web Services (AWS). S3 allows users to store and retrieve data objects from anywhere in the world, using a simple web services interface. One of the powerful features of S3 is its ability to trigger AWS Lambda functions when objects are uploaded to or removed from a bucket. This capability enables users to perform various automated tasks based on the uploaded files. In this blog post, we will explore how to set up a trigger Lambda function using Node.js and AWS SDK for JavaScript.
Step 1: Create an S3 Bucket
The first step is to create an S3 bucket. If you already have a bucket, you can skip this step. To create an S3 bucket, follow these steps:
Login to your AWS account and go to the S3 console.
Click on the “Create bucket” button.
Enter a unique name for your bucket.
Select the region where you want your bucket to be created.
Click on the “Create” button.
Step 2: Create a Lambda Function
Next, we need to create a Lambda function that will be triggered when an object is uploaded to the S3 bucket. Follow these steps:
Go to the AWS Lambda console.
Click on the “Create function” button.
Choose “Author from scratch”.
Enter a name for your function.
Select “Node.js 18. x” as the runtime.
Choose “Create a new role with basic Lambda permissions” for the role.
Click on the “Create function” button.
Step 3: Add an S3 Trigger
Now, we need to add an S3 trigger to our Lambda function. Follow these steps:
Click on the “Add trigger” button in the “Designer” section of your Lambda function.
Choose “S3” from the trigger options.
Select the S3 bucket you created in step 1.
Choose “All objects create events” for the event type.
Click on the “Add” button.
Step 4: Write the Lambda Function Code
Now, we need to write the code for our Lambda function. We will use the AWS SDK for JavaScript to interact with S3. Follow these steps:
Scroll down to the “Function code” section of your Lambda function.
Replace the existing code with the following code:
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.handler = async (event) => {
const bucket = event.Records[0].s3.bucket.name;
const key = event.Records[0].s3.object.key;
console.log(`Bucket: ${bucket}, Key: ${key}`);
try {
const object = await s3.getObject({ Bucket: bucket, Key: key }).promise();
console.log(`Object content: ${object.Body.toString()}`);
} catch (err) {
console.log(`Error getting object: ${err}`);
}
return {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
};
This code retrieves the object from the S3 bucket and logs its content to the console. You can modify this code to perform any action you want based on the uploaded file.
Step 5: Test the Lambda Function
Finally, we need to test our Lambda function to ensure it is working correctly. Follow these steps:
Click on the “Test” button in the top right corner of your Lambda function.
Choose “S3 Put” as the event template.
Replace the values of “bucketName” and “objectKey” with the name of your S3 bucket and a sample object key, respectively.
Click on the “Create” button.
Click on the “Test” button again.
You should see the output of your Lambda function in the “Execution result” section. If everything is working correctly, you should see the content of the uploaded object in the logs.
In this blog post, we have learned how to set up a trigger Lambda function on an S3 bucket using Node.js and AWS SDK for JavaScript. We have created an S3 bucket, and a Lambda function added an S3 trigger, written the function code, and tested it using the AWS Lambda console. This is just a basic example, but you can use this as a starting point to create more advanced S3-triggered Lambda functions to automate your workflows.