question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CDK tool does not include installed external packages in virtualenvs when deploying

See original GitHub issue

Reproduction Steps

from glom import glom

$ cdk deploy

Error Log

I have a python code that imports glom. I used poetry add glom to install it. Lamda function couldn’t find it. I tried pip install glom, Lambda function still encountered an error.

The error is

[ERROR] Runtime.ImportModuleError: Unable to import module 'auto_tag': No module named 'glom'

Environment

  • **CLI Version : 1.18.0 (build bc924bc)
  • **Framework Version: Python 3.7
  • **OS : MAC OSX
  • **Language : Python

Other

By following this solution, I got it to work. However, my project now contains external library that has to be committed to the git project. It looks messy. https://stackoverflow.com/questions/58855739/how-to-install-external-modules-in-a-python-lambda-function-created-by-aws-cdk

I got it to work by running pip install glom --target lambda where lambda is the directory configured in this block

        eventHandler = _lambda.Function(
            self,
            'resourceTagger',
            runtime = _lambda.Runtime.PYTHON_3_7,
            code = _lambda.Code.asset('lambda'), <------- THIS
            handler = 'auto_tag.handler'
        )

But look how messy it is now - https://i.imgur.com/I5YMwtR.png

Are there other ways to do it something that we don’t have to install it in the path specified in _lambda.Code.asset?


This is 🐛 Bug Report

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
asfourcocommented, Mar 6, 2020

@kgunny There is a way to test your lambda function locally, it requires AWS SAM installed

# from within your project
$ cdk synth > template.yml
# you'll need the function name that cdk assigned to your lambda function
# e.g., fn_start_trainEO123Q2
$ sam local invoke --no-event --debug  _cdk_assigned_function_name_ 

N.B. I’ve had to modify the template to point to the correct asset in cdk.out in the past. Not sure if it’s still the case.

Here’s the mock.sh script I created to run within my cdk python app project for local testing:

#!/bin/bash

# run cdk synth once to look for the function name and put it here
functionName=" GET FROM THE TEMPLATE"

cdk synth | gsed -r 's/^(\s*aws:asset:path:\s*)(asset\w*)/\1cdk.out\/\2/' > template.yml 
sam local invoke --no-event --debug  $functionName 
1reaction
c0debreakercommented, Dec 20, 2019

Your layers worked like a charm! Thanks a lot! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Managing dependencies - AWS Documentation
Dependencies for your AWS CDK app or library are managed using package management tools. These tools are commonly used with the programming language...
Read more >
How to Deploy Cloud Infrastructure Using AWS Cloud ...
Here, following python practices, we're going to create a .venv directory and install our external dependencies (the CDK libraries we need).
Read more >
How to install external modules in a Python Lambda Function ...
I have used this exact configuration in my CDK deployment and it works well. And for Python, it is simply aws_lambda.Function( self, "Function", ......
Read more >
Install CDK for Terraform and Run a Quick Start Demo
With CDKTF, you can use hundreds of providers and thousands of module definitions provided by HashiCorp and the Terraform community.
Read more >
A Guide to Python's Virtual Environments |
Install packages on a host on which you do not have admin privileges. ... A virtual environment is a Python tool for dependency...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found