The Ops Community ⚙️

Cover image for Local upload package of AWS no service development lambda series to lambda
Feng Lei
Feng Lei

Posted on

Local upload package of AWS no service development lambda series to lambda

This demonstration is divided into two types

1. No third party dependent package

2. There are third-party dependent packages

1. No third party dependent package

Create lambda as a deployment package Zip file.

1. Create a function (myfunc) project directory and enter it

bash-4.2# mkdir myfunc
bash-4.2# cd myfunc/
Enter fullscreen mode Exit fullscreen mode

2.Write your lambda function here as a demonstration

import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
Enter fullscreen mode Exit fullscreen mode

3.Lambda_function. Add py file to The root directory of the zip file

bash-4.2# zip my-function.zip lambda_function.py 
  adding: lambda_function.py (deflated 19%)
bash-4.2# ls -l
total 8
-rw-r--r--. 1 root root 165 Sep 18 07:49 lambda_function.py
-rw-r--r--. 1 root root 320 Sep 18 07:51 my-function.zip
Enter fullscreen mode Exit fullscreen mode

4.Here, you can use AWS cli to create functions, or you can upload the zip package directly through AWS console (an example is as follows)

A.AWS Console upload

Image description

B.AWS cli creates the function and specifies the zip package
Please modify the corresponding parameters by yourself, such as ARN of Iam
bash-4.2# aws lambda create-function --function-name my-func --zip-file fileb://my-function.zip --handler lambda_function.lambda_handler --runtime python3.6 --role arn:aws:iam::your-account-id:role/lambda-ex
Enter fullscreen mode Exit fullscreen mode

2.There are third-party dependent packages

  1. Set the directory structure of the deployment package (. Zip file).
  2. Create a deployment package for lambda functions with runtime dependencies.
  3. Use AWS cli to upload deployment package and create lambda function.
  4. Call lambda function to return source code

Create lambda as a deployment package Zip file.

1. Create the function (mylib func) project directory and enter it

bash-4.2# mkdir mylib-func
bash-4.2# cd mylib-func
Enter fullscreen mode Exit fullscreen mode

2.Write sample code lambda_ function. Py (where requests are third-party packages)

import requests
def main(event, context):   
    response = requests.get("https://www.baidu.com/")
    print(response.text)
    return response.text
if __name__ == "__main__":   
    main('', '')
Enter fullscreen mode Exit fullscreen mode

3.Install the request Library in the new package directory.

bash-4.2# pip3 install --target ./package requests
Enter fullscreen mode Exit fullscreen mode

4.Use the installed library to create the deployment package in the root directory (create a zip package in the parent directory and add the things in the package directory to the zip package)

bash-4.2# cd package/
bash-4.2# zip -r ../mylib-func.zip .
Enter fullscreen mode Exit fullscreen mode

5.Lambda_function. Add the PY file to the root directory of the zip file

bash-4.2# zip -g mylib-func.zip lambda_function.py 
  adding: lambda_function.py (deflated 32%)
bash-4.2# 
Enter fullscreen mode Exit fullscreen mode

6.Upload package to AWS lambda

A. AWS cli creates a function and selects a package (please modify the corresponding parameters by yourself)
bash-4.2# aws lambda create-function --function-name my-sourcecode-function --zip-file fileb://mylib-func.zip --handler lambda_function.main --runtime python3.8 --role arn:aws:iam::your-account-id:role/lambda-ex     
Enter fullscreen mode Exit fullscreen mode
B.Use AWS console to create functions and upload zip packages

Image description

Top comments (0)