Skip to main content

AWS Lambda


How to Create a Lambda Function (NodeJS)

Build Your NodeJS App

  1. npm init -y
  2. 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

  1. node index.js

Setup in AWS Lambda

  1. Zip up the entire folder (including node_modules)
  2. AWS Console > Lambdas
  3. Create Function > NodeJS 16 x86
  4. Upload From > Zip File
  5. Configuration > Create URL > None
    1. This gives you a URL that you can call via Postman, fetch, etc.
  6. Configuration > General Settings > Update memory and timeout
    1. Give yourself enough time and memory to complete your task
    2. Example: Memory = 1024 MB; Timeout = 1 min
  7. Set Role
  8. 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

  1. You may be able to make some code changes in the online editor
  2. 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

  1. Start a virtual environment
    1. mkdir my-python-app
    2. cd my-python-app
    3. virtualenv .
    4. .\Scripts\activate.ps1
    5. pip install xxx
    6. 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

  1. python .\Lib\site-packages\lambda_function.py

Setup in AWS Lambda

  1. Zip up the /Lib/site-packages/ folder
  2. AWS Console > Lambdas
  3. Create Function > Python 3.9
  4. Upload From > Zip File
  5. Configuration > Create URL > None
    1. This gives you a URL that you can call via Postman, fetch, etc.
  6. Configuration > General Settings > Update memory and timeout
    1. Give yourself enough time and memory to complete your task
    2. Example: Memory = 1024 MB; Timeout = 1 min
  7. Set Role
  8. 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

  1. Usually you can't edit online because there's too many files, so you have to keep zipping/uploading (this is painful)