使用Python从AWS Lambda访问Oracle

使用Python从AWS Lambda访问Oracle

问题描述:

我正在(希望)编写一个简单的AWS Lambda,它将执行RDS Oracle SQL SELECT并通过电子邮件发送结果.到目前为止,我一直在使用Lambda管理控制台,但是我遇到的所有示例都在谈论制作Lambda部署程序包.所以我的第一个问题是可以从Lambda管理控制台执行此操作吗?

I am writing (hopefully) a simply AWS Lambda that will do an RDS Oracle SQL SELECT and email the results. So far I have been using the Lambda Management Console, but all the examples I've run across talk about making a Lambda Deployment Package. So my first question is can I do this from the Lambda Management Console?

下一个问题,我要为Oracle DB API导入什么?在我看到的所有示例中,他们都使用pip下载并构建了一个程序包,但这似乎意味着使用了部署程序包(请参见上文).尝试导入示例中列出的这些模块中的任何一个,只需给没有名为"的模块...

Next question I have is what to import for the Oracle DB API? In all the examples I have seen, they download and build a package with pip, but that would then seem to imply using a Deployment Package (see above). Trying to import any of these modules listed in the examples simply give "No module named "...

写完上面的内容后,我研究了boto3 API参考,并提出了:

After writing the above I dug into the boto3 API referrence and came up with:

import boto3
client = boto3.client('rds-data')

但是它给出了错误:未知服务:"rds-data".

But it gives the error: Unknown service: 'rds-data'.

所以我仍然迷路.

您可能会说,我是Lambda环境的新手.任何建议或示例将不胜感激.谢谢.

As you can probably tell, I'm new to the Lambda environment. Any suggestions or examples would be greatly appreciated. Thanks.

经过反复的and吟和咬牙切齿,我提出了一个成功的解决方案.

After much groaning and gnashing of teeth I have come up with a successful solution.

rds_data(由AWS Support确认)仅支持Aurora数据库.希望AWS文档提到此. 8 {(>

rds_data (as confirmed by AWS Support) is only supporting Aurora Databases. Wish the AWS documents mentioned this. 8{(>

感谢上述答案以及 Jason Landrey 提供有关解决方案的提示.

Thanks to the answers above as well as Jason Landrey for hints as to the solution.

为了访问RDS/Oracle,您需要使用cx_Oracle.但是等等,还有更多.

In order to access RDS/Oracle, you need to use cx_Oracle. But wait, there's more.

cx_Oracle不在标准的Lambda环境中,因此您需要自带.我的开发环境是Windows,而Lambda环境是Linux.因此,您需要下载并安装在您的打包目录中,该目录是我从 https://pypi获得的目录中.org/project/cx-Oracle/#files .在本地安装:

cx_Oracle is not in the standard Lambda environment, so you need to bring your own. My development environment is on Windows, but the Lambda environment is Linux. So, you need to download and install in your packaging directory I got mine from https://pypi.org/project/cx-Oracle/#files. Install locally with:

pip install cx_Oracle-7.1.2-cp37-cp37m-manylinux1_x86_64.whl -t .

您将看到出现几个文件.然后,您需要找到一个Linux系统并下载/lib64/libaio.so.1.0.1并在打包目录中将其命名为libaio.so.1.
然后,您需要从 http上下载Oracle Instant Client Basic和SDK软件包. ://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html .

You will see several file appear in . Then you need to find a Linux system and download /lib64/libaio.so.1.0.1 and call it libaio.so.1 in your packaging directory.
And then you need to download both Oracle instant client basic and SDK packages from http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html.

使用所有这些项目(包括您自己的Python源代码)创建一个zip文件.为此,将Oracle Instant Client文件libclntsh.so.11.1重命名为libclntsh.so,将libocci.so.11.1重命名为libocci.so.

Create a zip file with all these items (including your own Python source). In doing so, rename Oracle instant client files libclntsh.so.11.1 to libclntsh.so and libocci.so.11.1 to libocci.so.

将zip压缩到S3存储桶中,因为直接部署限制为66mb,并且此zip压缩了一些.

Upload the zip to a S3 bucket as the direct deploy is limited to 66mb and this zip is a bit larger.

使用适当的IAM权限和VPC访问权限创建Lambda,安装该软件包,应该可以了.

Create a Lambda with the appropriate IAM permissions and VPC access, install the package and it should be good to go.

我发现,如果不包括所有即时客户端文件,您将开始收到有关缺少时区和NLS信息的Oracle错误.

I found that if you don't include all the instant client files you start getting Oracle errors about missing timezone and NLS information.

zip内容列表(对我来说,是YMMV):

List of zip contents (for me, YMMV):

    7996693  08/24/2013 12:30   libnnz11.so
          0  03/11/2019 16:10   cx_Oracle-7.1.1.data/
          0  03/11/2019 16:10   cx_Oracle-7.1.1.data/data/
          0  03/11/2019 16:10   cx_Oracle-7.1.1.data/data/cx_Oracle-doc/
          0  03/11/2019 16:10   cx_Oracle-7.1.1.dist-info/
       1325  03/13/2019 12:35   Email.py
       1805  02/19/2019 21:11   cx_Oracle-7.1.1.data/data/cx_Oracle-doc/LICENSE.txt
        163  02/19/2019 21:11   cx_Oracle-7.1.1.data/data/cx_Oracle-doc/README.txt
        851  02/19/2019 21:11   cx_Oracle-7.1.1.dist-info/METADATA
        628  02/19/2019 21:12   cx_Oracle-7.1.1.dist-info/RECORD
        109  02/19/2019 21:12   cx_Oracle-7.1.1.dist-info/WHEEL
         10  02/19/2019 21:11   cx_Oracle-7.1.1.dist-info/top_level.txt
    2270301  02/19/2019 21:11   cx_Oracle.cpython-37m-x86_64-linux-gnu.so
       2140  03/13/2019 14:21   getSecrets.py
       5560  03/12/2019 08:48   libaio.so.1
   53865194  08/24/2013 12:30   libclntsh.so
  118738042  08/24/2013 12:30   libociei.so
       7633  03/13/2019 16:39   scheduleReports.py