AWS Lambda and python numpy module

前端 未结 5 1436
梦毁少年i
梦毁少年i 2021-01-07 08:57

I am trying to import a python deployment package in aws lambda. The python code uses numpy. I followed the deployment package instructions for virtual env but it still gave

5条回答
  •  礼貌的吻别
    2021-01-07 09:40

    The easiest way is to use AWS Cloud9, there is no need to start EC2 instances and prepare deployment packages.

    Step 1: start up Cloud9 IDE

    • Go to the AWS console and select the Cloud9 service.
    • Create environment
    • enter Name
    • Environment settings (consider using t2.small instance type, with the default I had sometimes problems to restart the environment)
    • Review
    • click Create environment

    Step 2: create Lambda function

    • Create Lambda Function (bottom of screen)
    • enter Function name and Application name
    • Select runtime (Python 3.6) and blueprint (empty-python)
    • Function trigger (none)
    • Create serverless application (keep defaults)
    • Finish
    • wait couple of seconds for the IDE to open

    Step 3: install Numpy

    • at the bottom of the screen there is a command prompt
    • go to the Application folder, for me this is

      cd App
      
    • install the package (we have to use pip from the virtual environment, because the default pip points to /usr/bin/pip and this is python 2.7)

      venv/bin/pip install numpy -t .
      

    Step4: test installation

    • you can test the installation by editing the lambda_function.py file:

      import numpy as np
      def lambda_handler(event, context):
          return np.sin(1.0)
      
    • save the changes and click the green Run button on the top of the screen

    • a new tab should appear, now click the green Run button inside the tab
    • after the Lambda executes I get:

      Response
      0.8414709848078965
      

    Step 5: deploy Lambda function

    • on the right hand side of the screen select your Lambda function
    • click the upwards pointing arrow to deploy
    • go to the AWS Lambda service tab
    • the Lambda function should be visible, the name has the format

      cloud9-ApplicationName-FunctionName-RandomString
      

提交回复
热议问题