Search code examples
pythonamazon-web-servicesgitlabserverless-frameworkserverless

AWS: Unable to import module 'handler': No module named 'numpy'


AWS Newbie here

I have to host my python scripts via gitlab CI as a lambda and trigger it by cloudwatch on AWS.

I am following the following tutorials:

  1. https://docs.gitlab.com/ee/user/project/clusters/serverless/aws.html
  2. https://serverless.com/blog/serverless-python-packaging/

From the first tutorial, I am taking just the .gitlab-ci.yml file and adding it to the tutorial shown in the second tutorial (but instead of python 3.6, I am choosing 3.7 as the runtime)

I have the following serverless.yml file

service: numpy-test

provider:
  name: aws
  runtime: python3.7

functions:
  numpy:
    handler: handler.main

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: non-linux

package:
  exclude:
    - venv/**

and the following .gitlab-ci.yml file:

image: node:latest

stages:
  - deploy

production:
  stage: deploy
  before_script:
    - npm config set prefix /usr/local
    - npm install -g serverless
    - npm install --save serverless-python-requirements
  script:
    - serverless deploy
  environment: production

When I push the .gitlab-ci.yml file, it runs without any errors.

enter image description here

I can see the lambda function created in my AWS lambda. When I connect this lambda with the Cloudwatch so that the lambda gets triggered every 1 minute, I get the following error in the Logs:

enter image description here

I also have a requirements.txt file and it has

numpy==1.18.2

I have the following files/folders in my directory: enter image description here

Can someone please help me out with this and explain me the mistake I am doing and how can it be corrected?

Edit 1

I edited the serverless.yml file as pointed out by makozaki, and now when I push it in the repo, the CI file fails giving the following error:

enter image description here enter image description here


Solution

  • One of the reason could be docker image you have used in .gitlab-ci.yml file. But you want to deploy python lambda with some modules. Try to use image which has both configuration python as well node like this image:

    nikolaik/python-nodejs

    or if you want some specific version then you can do this also:

    nikolaik/python-nodejs:python3.7-nodejs13
    

    after you got error , i tried to replicate it in my account. so, now i made some changes in serverless.yml as well in .gitlab-ci.yml file here.

    you can change service name in serverless.yml

    and aws region in .gitlab-ci.yml

    after this it deployed in my account and worked fine.

    and here is the test result : Null is there because function is not returning any value but it printed the array.

    here is my cloudwatch log:

    Hope after these changes you will able to make it working.