Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda function write to S3

I have a Node 4.3 Lambda function in AWS. I want to be able to write a text file to S3 and have read many tutorials about how to integrate with S3. However, all of them are about how to call Lambda functions after writing to S3.

How can I create a text file in S3 from Lambda using node? Is this possible? Amazons documentation doesn't seem to cover it.

like image 239
KVISH Avatar asked Oct 22 '16 04:10

KVISH


People also ask

Can a Lambda function Write to S3?

Yes it is absolutely possible! Make sure that you give your Lambda function the required write permissions to the target s3 bucket / key path by selecting or updating the IAM Role your lambda executes under.

Can Lambda upload file to S3?

AWS LAMBDA- Serverless Computing Amazon S3 service is used for file storage, where you can upload or remove files. We can trigger AWS Lambda on S3 when there are any file uploads in S3 buckets. AWS Lambda has a handler function which acts as a start point for AWS Lambda function.


4 Answers

Yes it is absolutely possible!

var AWS = require('aws-sdk');
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
        });
}

Make sure that you give your Lambda function the required write permissions to the target s3 bucket / key path by selecting or updating the IAM Role your lambda executes under.

IAM Statement to add:

{
    "Sid": "Stmt1468366974000",
    "Effect": "Allow",
    "Action": "s3:*",
    "Resource": [
        "arn:aws:s3:::my-bucket-name-goes-here/optional-path-before-allow/*"
    ]
}

Further reading:

  • AWS JavaScript SDK
  • The specific "Put Object" details
like image 108
Xavier Hutchinson Avatar answered Oct 11 '22 17:10

Xavier Hutchinson


IAM Statement for serverless.com - Write to S3 to specific bucket

service: YOURSERVICENAME

provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region: eu-west-1
  timeout: 60
  iamRoleStatements:
    - Effect: "Allow"
      Action:
       - s3:PutObject
      Resource: "**BUCKETARN**/*"
    - Effect: "Deny"
      Action:
        - s3:DeleteObject
      Resource: "arn:aws:s3:::**BUCKETARN**/*"
like image 33
Steffen Avatar answered Oct 11 '22 17:10

Steffen


After long long time of silence-failing of 'Task timed out after X' without any good error message, i went back to the beginning, to Amazon default template example, and that worked!

> Lambda > Functions > Create function > Use a blueprints > filter: s3.

Here is my tweaked version of amazon example:

const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });

async function uploadFileOnS3(fileData, fileName){
    const params = {
        Bucket:  "The-bucket-name-you-want-to-save-the-file-to",
        Key: fileName,
        Body: JSON.stringify(fileData),
    };

    try {
        const response = await s3.upload(params).promise();
        console.log('Response: ', response);
        return response;

    } catch (err) {
        console.log(err);
    }
};
like image 31
Elad Avatar answered Oct 11 '22 17:10

Elad


You can upload file on s3 using

aws-sdk

If you are using IAM user then you have to provide access key and secret key and make sure you have provided necessary permission to IAM user.

var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: "ACCESS_KEY",secretAccessKey: 'SECRET_KEY'});
var s3bucket = new AWS.S3({params: {Bucket: 'BUCKET_NAME'}});
function uploadFileOnS3(fileName, fileData){
    var params = {
      Key: fileName,
      Body: fileData,
    };
    s3bucket.upload(params, function (err, res) {               
        if(err)
            console.log("Error in uploading file on s3 due to "+ err)
        else    
            console.log("File successfully uploaded.")
    });
}

Here I temporarily hard-coded AWS access and secret key for testing purposes. For best practices refer to the documentation.

like image 44
Anish Agarwal Avatar answered Oct 11 '22 19:10

Anish Agarwal