title: How to deploy Python AWS Lambda with its dependencies using Javascript AWS CDK?
body: <p>I have an AWS CDK stack built using javascript. The Stack has multiple lambdas. Most of them are in javascript. I want to add one lambda in python. The python lambda works fine when I don't import external dependencies, but doesn't understand when I install them. I've tried installing the packages in a <code>package</code> folder or a <code>python</code> folder and zipping them as these articles suggested but didn't work:</p>
  <p><a href="https://docs.aws.amazon.com/lambda/latest/dg/python-package.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/lambda/latest/dg/python-package.html</a>
  <a href="https://aws.amazon.com/premiumsupport/knowledge-center/lambda-import-module-error-python/" rel="nofollow noreferrer">https://aws.amazon.com/premiumsupport/knowledge-center/lambda-import-module-error-python/</a></p>
  <p>The error I'm getting is <code>&quot;Unable to import module 'py1': No module named 'x'&quot;</code>
  (x refer to any package name I'm trying to import)</p>
  <p>My javascript CDK library code is like this:</p>
  <pre class="lang-js prettyprint-override"><code>// javascript lambda
  new lambda.Function(this, 'lambda-js1', {
    functionName: `js1`,
    code: lambda.Code.fromAsset('assets/lambdajs'),
    handler: 'js1.handler',
    runtime: lambda.Runtime.NODEJS_14_X,
  })
  
  // python lambda
  new lambda.Function(this, 'lambda-py1', {
    functionName: `py1`,
    code: lambda.Code.fromAsset('assets/lambdapy'),
    handler: 'py1.handler',
    runtime: lambda.Runtime.PYTHON_3_8,
  })
  </code></pre>
  <p>I installed the dependency in <code>assets/lambdapy</code> using</p>
  <pre><code>pip install x
  pip install --target ./package x
  pip install -t python/ x
  </code></pre>
  <p>I zipped them after</p>
  <p>My python code is in <code>assets/lambdapy/py1.py</code></p>
  <pre class="lang-py prettyprint-override"><code># not sure how to import. none of the below worked
  import x
  import package.x
  import python.x
  
  def handler(event, context):
      return { 
          'statusCode': 200
      }
  </code></pre>
  <p>Upon invocation of the python lambda, I get</p>
  <pre><code>{
    &quot;errorMessage&quot;: &quot;Unable to import module 'py1': No module named 'x'&quot;,
    &quot;errorType&quot;: &quot;Runtime.ImportModuleError&quot;,
    &quot;stackTrace&quot;: []
  }
  </code></pre>