我刚刚编写了一个Python Lambda函数,该函数执行一些MySQL的工作,然后将消息发送给SQS。所有这些在本地都可以正常运行,并且消息已成功发送到SQS。
我正在像这样创建一个SQS客户端:
sqsClient = boto3.client('sqs')
但是,在将打包的Lambda函数部署到AWS并运行内置的Lambda测试功能之后。我收到以下错误:
{
"errorMessage": "Unknown service: 'sqs'. Valid service names are: ",
"errorType": "UnknownServiceError",
"stackTrace": [
" File \"/var/lang/lib/python3.7/imp.py\", line 234, in load_module\n return load_source(name, filename, file)\n",
" File \"/var/lang/lib/python3.7/imp.py\", line 171, in load_source\n module = _load(spec)\n",
" File \"<frozen importlib._bootstrap>\", line 696, in _load\n",
" File \"<frozen importlib._bootstrap>\", line 677, in _load_unlocked\n",
" File \"<frozen importlib._bootstrap_external>\", line 728, in exec_module\n",
" File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n",
" File \"/var/task/main.py\", line 20, in <module>\n sqsClient = boto3.client('sqs')\n",
" File \"/var/task/boto3/__init__.py\", line 91, in client\n return _get_default_session().client(*args, **kwargs)\n",
" File \"/var/task/boto3/session.py\", line 263, in client\n aws_session_token=aws_session_token, config=config)\n",
" File \"/var/task/botocore/session.py\", line 838, in create_client\n client_config=config, api_version=api_version)\n",
" File \"/var/task/botocore/client.py\", line 79, in create_client\n service_model = self._load_service_model(service_name, api_version)\n",
" File \"/var/task/botocore/client.py\", line 117, in _load_service_model\n api_version=api_version)\n",
" File \"/var/task/botocore/loaders.py\", line 132, in _wrapper\n data = func(self, *args, **kwargs)\n",
" File \"/var/task/botocore/loaders.py\", line 378, in load_service_model\n known_service_names=', '.join(sorted(known_services)))\n"
]
}
我最初的想法是,可能Boto3的Lambda版本尚未包含SQS。我不认为这是问题,因为我正在部署自己的boto和botocore版本。我还在函数中添加了以下几行,以确保Lambda使用的是Boto的已部署版本,而不是其内置版本:
print("boto3 version:"+boto3.__version__)
print("botocore version:"+botocore.__version__)
正在输出以下版本:
boto3 version:1.9.87
botocore version:1.12.87
这些是正确的,并且与我的本地版本相同。
任何帮助将不胜感激。谢谢。
编辑: 我正在使用一个批处理文件来部署当前的代码,如下所示:
powershell.exe -nologo -noprofile -command "& {& Remove-Item .\deploy -Force -Recurse }"
mkdir deploy
powershell.exe -nologo -noprofile -command "& {& Get-ChildItem .\src -Exclude '__pycache__' | Copy-Item -Destination .\deploy }"
cd deploy
pipenv lock -r > dependencies.txt
pip install -r dependencies.txt --no-deps -t .
powershell.exe -nologo -noprofile -command "& {& Get-ChildItem . -Exclude '__pycache__' | Compress-Archive -DestinationPath deploy.zip -Force }"
aws lambda update-function-code --function-name API-LogUserInterest --zip-file fileb://deploy.zip
echo Deployment finished!
pause
这将在我的工作目录中创建一个“ deploy”文件夹,在其中安装依赖项,并从“ src”文件夹复制我的源代码。然后,使用powershell将整个部署文件夹压缩,然后使用AWS CLI update-function-code
命令将其推送到Lambda。
这是deploy.zip内容的屏幕截图: