Receive error in Lambda: Unable to import module 'functions': No module named 'functions' Traceback

0

Hello,

I receive the error in Lambda: Receive error in Lambda: Unable to import module 'functions': No module named 'functions' Traceback

I have researched the issue and from what I understand it's a problem bundling some of python depencies and libraries. The issue I'm having, is I'm trying to find a fix for this issue via CDK. We deploy our resources via CDK and I would like to add the fix to the CDK stack.

How do I implement a deployment package with my lambda in CDK? Are there resources I can find for these steps?

Thanks

1 Resposta
0

Yes, it is possible to include your deployment package with your Lambda function in your CDK stack. Here's an example of how you can achieve this:

First, make sure you have the necessary dependencies installed to build and deploy your CDK stack. You can follow the instructions here to set up your environment: https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html#getting_started_prerequisites.

Create a new directory for your CDK project and initialize a new CDK app:

mkdir my-cdk-project
cd my-cdk-project
cdk init app --language python

Create a new directory inside your project directory for your Lambda function and add a Python file with your function code:

mkdir functions
touch functions/my_function.py

Add your Lambda function code to my_function.py.

Add the necessary CDK modules to your project:

pip install aws-cdk.aws-lambda
pip install aws-cdk.aws-apigateway

In your app.py file, create a new Lambda function using the aws_lambda.Function class and specify the path to your deployment package using the code parameter. Here's an example:

from aws_cdk import (
    core,
    aws_lambda,
)

class MyCdkStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Create a new Lambda function
        my_function = aws_lambda.Function(
            self, "MyFunction",
            runtime=aws_lambda.Runtime.PYTHON_3_8,
            handler="functions.my_function.handler",
            code=aws_lambda.Code.asset("path/to/your/deployment/package"),
        )

Make sure to replace "path/to/your/deployment/package" with the actual path to your deployment package.

Deploy your CDK stack using the cdk deploy command.

cdk deploy

This will create your Lambda function and deploy your deployment package to AWS.

I hope this helps!

profile pictureAWS
respondido há um ano
  • Thanks for help,

    I actually havae this already. I'm using Jenkins to deploy it.

    I think it's important to note that I have the stack written in TypeSCript. But the lambda is in python.

    However, I do have the code=Code.fromAsset("<my_path">).

    Which now confuses me more why my lambda doesn't work.

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas