AWS Lambda
How to Create a Lambda Function (NodeJS)
Build Your NodeJS App
npm init -y
touch index.js
//const mysql = require("mysql2"); // Example of import
exports.handler = async (event) => {
const myValue = event.queryStringParameters.myValue;
const myRet = { val: myValue };
return {
statusCode: 200,
body: JSON.stringify(myRet),
};
};
Test Your NodeJS App
node index.js
Setup in AWS Lambda
- Zip up the entire folder (including
node_modules
) AWS Console > Lambdas
Create Function > NodeJS 16 x86
Upload From > Zip File
Configuration > Create URL > None
- This gives you a URL that you can call via Postman, fetch, etc.
Configuration > General Settings > Update memory and timeout
- Give yourself enough time and memory to complete your task
- Example:
Memory = 1024 MB
;Timeout = 1 min
Set Role
- Make sure that the
index.js
file is inside the ROOT folder. Depending on how you zip it, it might be one folder too low
Test it in AWS
- You may be able to make some code changes in the online editor
- If you make a change, be sure to "Deploy" it
How to Create a Lambda Function (Python)
Build Your Python App
Instructions from here: https://medium.com/bi3-technologies/creating-python-deployment-package-for-aws-lambda-function-25205f033ac5
- Start a virtual environment
mkdir my-python-app
cd my-python-app
virtualenv .
.\Scripts\activate.ps1
pip install xxx
touch ./Lib/site-packages/lambda_function.py
#!/usr/bin/python3
def lambda_handler(event, context):
myValue = ""
# Get query string params...
try:
searchTerm = event["queryStringParameters"]["myValue"]
except:
return {
'statusCode': 400,
'body': "Bad Request"
}
myRet = { val: myValue }
return myRet
Test Your Python App
python .\Lib\site-packages\lambda_function.py
Setup in AWS Lambda
- Zip up the
/Lib/site-packages/
folder AWS Console > Lambdas
Create Function > Python 3.9
Upload From > Zip File
Configuration > Create URL > None
- This gives you a URL that you can call via Postman, fetch, etc.
Configuration > General Settings > Update memory and timeout
- Give yourself enough time and memory to complete your task
- Example:
Memory = 1024 MB
;Timeout = 1 min
Set Role
- Make sure that the
index.js
file is inside the ROOT folder. Depending on how you zip it, it might be one folder too low
Test it in AWS
- Usually you can't edit online because there's too many files, so you have to keep zipping/uploading (this is painful)