Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda - downloading a file, and using it in the same function - nodejs

I have some certificate files over s3 (public) and I am to download and use these files in my code, If I write an equivalent code in nodejs at my local, it just runs fine, but in AWS lambda it just crashes.

var apn = require('apn'); var https = require('https'); var fs = require('fs');  exports.handler = function(event, context) {   console.log("Running aws apn push message function");   console.log("==================================");   console.log("event", event);    var certPath = event.certPath;   var keyPath = event.keyPath;   var certFileName = event.certFileName;   var keyFileName = event.keyFileName;   var passphrase = event.passphrase;   var apnId = event.apnId;   var content = event.content;   var certfile = fs.createWriteStream(certFileName); var certrequest = https.get(certPath, function(certresponse) {   certresponse.pipe(certfile);   console.log("downloaded the certificate");    var keyfile = fs.createWriteStream(keyFileName);   var keyrequest = https.get(keyPath, function(keyresponse) {     keyresponse.pipe(keyfile);     console.log("downloaded the key file");     var options = {                        "cert":certFileName,                       "key":keyFileName,                       "passphrase":passphrase,                       "batchFeedback": true,                       "interval": 10                       };    var apnConnection = new apn.Connection(options);    var myDevice = new apn.Device(apnId);   var note = new apn.Notification();   note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.           note.payload = {'COMMAND': content};          apnConnection.pushNotification(note, myDevice);   console.log('message sent to ' + apnId);           context.done();    }); }); } 

Error I get is related to accessing files i suppose -

events.js:72   throw er; // Unhandled 'error' event   ^  Error: EACCES, open 'PushChatCert.pem' 

So while on AWS Lambda is there some specific concerns when one is downloading a file and using it, related to its path or something, where do the files stay when they get downloaded, in fact I don't even see the log of file getting downloaded.

like image 539
Brij Raj Singh - MSFT Avatar asked Aug 11 '15 10:08

Brij Raj Singh - MSFT


People also ask

How do I download a file from Lambda?

To download the code for a Lambda function:Open the AWS Lambda console and click on your function's name. Click on the Actions button and click Export function.

Can AWS Lambda have multiple files?

At the end you want to send multiple files to a lambda function as a zip and use these files inside the lambda_handler() function . You need to create all your files inside a directory in such a way that they can be imported in other files easily and zip your lambda function and use it.

Can AWS Lambda share code between functions?

Before lambda layers, developers used to either duplicate common code in every lambda function or create local npm packages and refer them in lambdas. Now with lambda layers, you can securely share code among your lambda functions in the same AWS account, cross-accounts or in public.

Can AWS Lambda have multiple threads?

Using multithreading in AWS Lambda can speed up your Lambda execution and reduce cost as Lambda charges in 100 ms unit.


2 Answers

The only available local file system you can write to in Lambda is /tmp so make sure the path for the local file you are trying to write to is in the /tmp directory and you should be all set.

like image 127
garnaat Avatar answered Oct 19 '22 04:10

garnaat


Just note as of last year (2020) Lambdas support EFS as a mount as well so you can write to EFS mount point. Excessive for your case..but it might help for someone doing large file processing https://aws.amazon.com/blogs/compute/using-amazon-efs-for-aws-lambda-in-your-serverless-applications/

like image 38
iddqd Avatar answered Oct 19 '22 04:10

iddqd