Python aws lambda with docker loading external lib did not compile

Hi there,

I am using mac osx, trying to deploy python function with mysql lib to aws lambda. The function is just simply testing the connection between RDS and lambda function. The following lists the files and I followed https://serverless.com/blog/serverless-python-packaging/ guide to set it up from my mac osx env. Any one could help? The error message I got is “ValueError: source code string cannot contain null bytes”

////////// Error Log //////////

Error --------------------------------------------------

Traceback (most recent call last):
File “/var/lang/lib/python3.6/runpy.py”, line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File “/var/lang/lib/python3.6/runpy.py”, line 142, in _get_module_details
return _get_module_details(pkg_main_name, error)
File “/var/lang/lib/python3.6/runpy.py”, line 109, in _get_module_details
import(pkg_name)
File “/var/lang/lib/python3.6/site-packages/pip/init.py”, line 21, in
from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
ValueError: source code string cannot contain null bytes

 For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

////////// Serverless.yml //////////

service: aws-python-wechatapi

frameworkVersion: “>=1.2.0 <2.0.0”

provider:
name: aws
stage: dev
region: us-east-1
runtime: python3.6
vpc:
securityGroupIds:
- sg-xxxxxxxxx
subnetIds:
- subnet-xxxxxxxxx
- subnet-xxxxxxxxx
- subnet-xxxxxxxxx

functions:
currentTime:
handler: handler.mysql_test
events:
- http:
path: mysql
method: get
listAll:
handler: handler.listObject
events:
- http:
path: listAll
method: get

resources:
Resources:
WriteDashPostLogGroup:
Type: AWS::Logs::LogGroup
Properties:
RetentionInDays: “30”

custom:
pythonRequirements:
dockerizePip: non-linux

plugins:

  • serverless-python-requirements

////////// handler.py //////////

import sys
import logging
import pymysql

name = “xxxx”
password = “xxxxxxxxxxxx”
db_name = “sample”
rds_host = “test-lambda.xxxxxxxxxxx.us-east-1.rds.amazonaws.com
port = 3306

logger = logging.getLogger()
logger.setLevel(logging.INFO)

try:
conn = pymysql.connect(rds_host, user=name,
passwd=password, db=db_name, connect_timeout=5)
except:
logger.error(“ERROR: Unexpected error: Could not connect to MySql instance.”)
sys.exit()

logger.info(“SUCCESS: Connection to RDS mysql instance succeeded”)

def mysql_test(event, context):
“”"
This function inserts content into mysql RDS instance
“”"
item_count = 0

with conn.cursor() as cur:
    cur.execute("create table Employee3 (EmpID  int NOT NULL, Name varchar(255) NOT NULL, PRIMARY KEY (EmpID))")
    cur.execute('insert into Employee3 (EmpID, Name) values(1, "Joe")')
    cur.execute('insert into Employee3 (EmpID, Name) values(2, "Bob")')
    cur.execute('insert into Employee3 (EmpID, Name) values(3, "Mary")')
    conn.commit()

    cur.execute("select * from Employee3")
    for row in cur:
        item_count += 1
        logger.info(row)

response = {
    "statusCode": 200,
    "body": "Added %d items to RDS MySQL table" %(item_count)
}

return response

def listObject(event, context):

with conn.cursor() as cur:
    cur.execute("select * from Employee3")
    result = []
    for row in cur:
        logger.info("Employee name " + row[1])
        result.append(row[1])

logger.info(result);

response = {
    "statusCode": 200,
    "body": result
}

return response

This is the complete error log:

Serverless: Installing required Python packages with python3.6…
Serverless: Docker Image: lambci/lambda:build-python3.6

Error --------------------------------------------------

Traceback (most recent call last):
File “/var/lang/lib/python3.6/runpy.py”, line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File “/var/lang/lib/python3.6/runpy.py”, line 142, in _get_module_details
return _get_module_details(pkg_main_name, error)
File “/var/lang/lib/python3.6/runpy.py”, line 109, in _get_module_details
import(pkg_name)
File “/var/lang/lib/python3.6/site-packages/pip/init.py”, line 21, in
from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
ValueError: source code string cannot contain null bytes

 For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Forums: forum.serverless.com
Chat: gitter.im/serverless/serverless

Your Environment Information -----------------------------
OS: darwin
Node Version: 8.6.0
Serverless Version: 1.26.1