AWS Lambda function write to S3 AWS Lambda function write to S3 node.js node.js

AWS Lambda function write to S3


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:


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

service: YOURSERVICENAMEprovider:  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**/*"


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.