How to reference other files within a lambda function? - go

In a handler of mine, I open a .mmdb file for doing geo lookups. In my package, I use ./ notation to reference the file since the handler and file are in the same directory. Now that I want to deploy the function using serverless, I've included the file within the include block in my serverless.yml file. Based on the package size that I'm seeing, the file is being uploaded, however I'm getting the error
open ./GeoLite2-City.mmdb: no such file or directory
when running the lambda. What is the proper way to get the location of the file from within my lambda?

I solved my problem by listing the contents of the directory that I was using in my lambda, using the code found in this answer: List directory in Go. After doing so I realized that I was in the root directory of the entire folder that I uploaded, not the directory of the specific package I was running the code from (containing main.go)

Related

Index handler is undefined or not exported

I have a lambda function which was working fine but I wanted to import a package so I created a directory with index.js and installed my npm package.
Then created a zip of this folder and uploaded it using
aws lambda update-function-code --function-name smrtfac-test
--zip-file fileb://lambda.zip
But now I am getting this error
index.handler is undefined or not exported
What could be the reason for it?
my index.js and node_modules are in the same directory.
This usually occurs when you zip up the directory, instead of zipping up the contents of the directory. When you open your zip file to browse the content, the index.js file should be in the root of the zip file, not in a folder.
You could also change the Handler section as below if your index.js is not directly under root folder as below
Consider using Lambda Layers for node modules: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path
This is because you are probably submitting the project inside a directory. You just zip all the files directly instead of zipping them into a directory. The index file needs to be at the root to be able to be read and accessed by lambda.
If you're using typescript with CDK, make sure you are not exporting another function within your main function file.

Laravel application deployed to AWS Elastic Beanstalk shows file not found error

I'm using AWS EB to host my Laravel 5.8 application. I have a view where I upload files. These files are 2 CSV files and a ZIP archive. Long story short, these files are stored locally to process their content, once they are stored, their stored location, which is returned as temp/... is concatenated with the helper method storage_path() to determine the full path for the file to open for processing:
$file = file(storage_path() . '\app/' . $filePath);
Once this line is reached, I'm getting this error:
fopen(/var/app/current/storage\app/temp/routes/20190906143753/ztySdkFwY5bAvQhIK4Mlsftz8fzVJfPK3S3d5CSV.txt): failed to open stream: No such file or directory
*************** UPDATE ***************
So it turns out that the storage folder in my Laravel project had no write permission. I've managed to fix that and now the files are actually being stored. However, another problem showed up. Which is files aren't stored fully until the execution of the page ends, which is not what I want since those files are also used during the same process for other purposes.

AWS Codedeploy access a file within BeforeInstall script

I'm bundling a file to be accessed by BeforeInstall shell script file. But shell script file cannot locate the file. I tried bundling in the same folder as script file. Still not working. It gives No such file or directory
bundle
scripts
beforeInstall.sh
file.properties
May I know if this is possible...
The files are not placed in the locations you have specified yet during the BeforeInstall step. At that point the zip has only been downloaded to a default location, so the file path in your script probably doesn't exist yet.
What do you want your script to do? Would running it during the AfterInstall step work?

How can i import and properly run pip imported libraries on lambda?

I imported boto3 for my lambda function in python. When i test lambda it gives this error : No module named boto3 which is rather expected.
Then i referred to the docs,to this link to be exact https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html and realized i need to create a deployment package the issue is i didn't understand the docs well enough to keep up and would like them to be explained again in a simpler way by someone that has already done the required steps.
You need to make a folder in your local system, install the required libraries into that folder, zip the contents of the folder and upload the zipped file to the AWS Lambda.
Make a folder in local system
I don't think you need help in this. Lets suppose you made a folder in D drive named yellow-bot
Install the required libraries into the folder
You can install the required packages in the folder using below command
pip install {package-name} -t "{path-to-project-dir}"
In your case it would be:
pip install apiai -t "D:\yellow-bot"
Zip the contents of the folder
Now after installing the required libraries there will be multiple files and folders in your yellow-bot folder. You need to select all and zip the content. Please note that do not zip the folder, instead you go inside the folder and zip the contents.
It would be something like below screenshot.
Create lambda function and upload zip
Now go to AWS Lambda, create a lambda function, give correct run-time and all that. Then select upload zip file in code entry type. Select your zip and click on upload.
Make sure to give correct Handler.
It follows naming convention as:
The filename.handler-method value in your function. For example,
"main.handler" would call the handler method defined in main.py.
Since in this case I have uploaded connector.py file and entry function was called lambda_handler() so correct Handler would be connector.lambda_handler
Click on Save and you are done.
Hope it helps.

Run node_modules binary on Lambda?

I'm trying to compress an image on Lambda using mozjpeg, but am having some issues.
The binary doesn't have execute permissions, and so I'm getting this error:
"exports._errnoException (util.js:870:11)",
"ChildProcess.spawn (internal/child_process.js:298:11)",
"Object.exports.spawn (child_process.js:362:9)",
"ret.catch.module.exports.promise (/var/task/node_modules/imagemin-mozjpeg/node_modules/exec-buffer/node_modules/execa/index.js:132:26)",
"/var/task/node_modules/imagemin-mozjpeg/node_modules/exec-buffer/index.js:36:15"
When I try to fix the permissions, I get this error:
'chmod: changing permissions of ‘/var/task/node_modules/imagemin-mozjpeg/node_modules/mozjpeg/vendor/cjpeg’: Read-only file system\n'
Is there a way to get the binaries to execute within node_modules, or an alternative to executing them manually from the tmp dir without the benefit of their nodejs wrappers?
You need to ensure that the method you use to zip your files includes preserving or setting the execute permissions in Unix format. They will then be preserved when the file is unzipped from S3.

Resources