pbodsk
pbodsk

Reputation: 6876

AWS Lambda function using pyaudio

My Dream :)

I would like to use pyaudio for a function which will be running on AWS Lambda. However, I get a PythonPipBuilder:ResolveDependencies error when running sam build --use-container

My Setup

I've managed to build my project down to this.

requirements.txt

pyaudio

app.py

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello World'
    }

template.yaml (relevant pieces at least)

Resources:
  MyFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: my_app/
      Handler: app.lambda_handler
      Runtime: python3.6
      Events:
        MyEvent:
          Type: Api 
          Properties:
            Path: /test
            Method: get

My Problem

When running sam build --use-container --debug I see this output:

Traceback (most recent call last): File "/var/lang/lib/python3.6/site-packages/aws_lambda_builders/workflows/python_pip/actions.py", line 42, in execute requirements_path=self.manifest_path, File "/var/lang/lib/python3.6/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 137, in build_dependencies self._dependency_builder.build_site_packages(requirements_path, artifacts_dir_path, scratch_dir_path) File "/var/lang/lib/python3.6/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 201, in build_site_packages raise MissingDependencyError(packages_without_wheels) aws_lambda_builders.workflows.python_pip.packager.MissingDependencyError: {pyaudio==0.2.11(sdist)}

...

Build inside container returned response {"jsonrpc": "2.0", "id": 1, "error": {"code": 400, "message": "PythonPipBuilder:ResolveDependencies - {pyaudio==0.2.11(sdist)}"}}

My Theory

My theory is that this fails because PortAudio is not installed in the Docker container image used when running sam build --use-container

If I run pip install -r requirements.txt on my local machine where I have PortAudio installed, everything works as expected.

My suspicion is that I need to install PortAudio in the AWS Lambda environment somehow...but how? Layers? And if so, from where do I get the binary files I need?

TL;DR;

How do I install PortAudio in an AWS Lambda environment?

Upvotes: 1

Views: 627

Answers (1)

Greg
Greg

Reputation: 4518

The quick way to test your lambda is to install the package (e.g pyaudio) in the folder that contains the lambda code, and then zip it all up & upload it. See more info https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-dependencies

If you have any issue with installing the packages, then it's possible it may be down to the dependence and you may have to add a layer(s).

Once you have your lambda working, you can remove it and have the python package installed using the buildspec.yml. AWS will install the packages when it's running the code pipleine. More details https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html

Upvotes: 2

Related Questions