Data Engineering

AWS Lambda to save some data into S3

Please do not use code below for production, its a toy example.

AWS Lambda role needs to get correct policy to access S3 (create policy permission to access S3, and add it to AWS Lambda role).

For example you need to create policy that has access to S3:

In AWS Lambda page, check out execution role:

And then in the roles you need to assign S3 access policy to AWS Lambda role:

Here is the AWS lambda code content:

     var AWS = require('aws-sdk');
      exports.handler = async (event) => {
         function putObjectToS3(bucket, key, data){
             var s3 = new AWS.S3();
       var params = {
            Bucket : bucket,
            Key : key,
            Body : data

        s3.putObject(params, function(err, data) {
          if (err) console.log(err, err.stack); // an error occurred
          else     console.log(data);           // successful response

console.log("Test string!!!" + event.key1);
putObjectToS3("testbucket", event.key1, JSON.stringify(event));

// Response is purely optional
const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!'),
return response;

Event is defined as:

“key1”: “value1”,
“key2”: “value2”,
“key3”: “value3”

So generated file in “testbucket” will be:


With content:


Back To Top