简体   繁体   中英

Project (AWS CDK) and Lambda specific python imports

Image you are writing an IaaC (Infrastructure as a Code) project with python programming language. There is a good library/sdk that makes it convenient: AWS CDK (Amazon web services cloud development kit). In the same project you can specify lambda source code too.

Now imagine you want that python imports in the lambda function source code would work in the lambda environment and in the project. One of the simplest ways to achieve that is to try/except ImportError like this:

try:
    # Lambda-specific import.
    from some.path import PathModule
except ImportError:
    # Project-specific import.
    from iaac.project.source.some.path import PathModule 

However, this solution seems to be kinda ugly. Any better suggestions how to write imports that would work in the AWS CDK project and in the Lambda environment?

Is it possible to use the same path for both the Lambda environment and the local environment?

If you pack the module together with the lambda source code, then from iaac.project.source.some.path import PathModule should work both locally and in Lambda environment.

If you put the module into a lambda layer , then you can install that module locally using pip , and from some.path import PathModule should work for both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM