“Unable to import module ‘lambda_function’: No module named ‘parameter_store_extension'”

huangapple go评论55阅读模式
英文:

Unable to import module 'lambda_function': No module named 'parameter_store_extension'

问题

# resumebuilder_utils.zip
.
├── __pycache__
│   ├── __init__.cpython-310.pyc
│   └── utils.cpython-310.pyc
├── python
│   └── lib
│       └── python3.10
│           └── site-packages
├── requirements.txt
└── utils.py
# parameter_store_extension.zip
.
└── parameter_store_extension.py
# parameter_store_extension.py
import urllib
import json
import os

from urllib.parse import urlencode

aws_session_token = os.environ.get('AWS_SESSION_TOKEN')
port = '2773';

def get_param(name: str):
    """
    Get SALT form systems manager/parameter store
    """
    params = dict(name=name)
    req = urllib.request.Request(
        f"http://localhost:{port}/systemsmanager/parameters/get/?{urlencode(params)}&withDecryption=true")
    req.add_header('X-Aws-Parameters-Secrets-Token', aws_session_token)
    config = urllib.request.urlopen(req).read()

    return json.loads(config)
英文:

I created two lambda layers. Both from zip files, in which the first one includes python dependencies

# resumebuilder_utils.zip
.
├── __pycache__
│   ├── __init__.cpython-310.pyc
│   └── utils.cpython-310.pyc
├── python
│   └── lib
│       └── python3.10
│           └── site-packages
├── requirements.txt
└── utils.py

My lambda function can use the functions inside utils.py like this

from utils import hash_password, normalize_value_object

As for the second layer, this one does not need any dependencies since it only consists of one file

# parameter_store_extension.zip
.
└── parameter_store_extension.py
# parameter_store_extension.py
import urllib
import json
import os

from urllib.parse import urlencode

aws_session_token = os.environ.get('AWS_SESSION_TOKEN')
port = '2773'


def get_param(name: str):
    """
    Get SALT form systems manager/parameter store
    """
    params = dict(name=name)
    req = urllib.request.Request(
        f"http://localhost:{port}/systemsmanager/parameters/get/?{urlencode(params)}&withDecryption=true")
    req.add_header('X-Aws-Parameters-Secrets-Token', aws_session_token)
    config = urllib.request.urlopen(req).read()

    return json.loads(config)

However, I cannot use functions inside parameter_store_extension.py, as I do with the first layer.

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'parameter_store_extension'

答案1

得分: 0

我应该将 parameter_store_extension.py 移动到 python/lib/python3.10/site-packages/ 再进行压缩。

英文:

my mistake, I should move the parameter_store_extension.py inside python/lib/python3.10/site-packages/ before zipping

huangapple
  • 本文由 发表于 2023年5月24日 23:33:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定