Python 如何在AWS Lambda函数中重用数据库连接?

Python 如何在AWS Lambda函数中重用数据库连接?,python,amazon-web-services,aws-lambda,cassandra,Python,Amazon Web Services,Aws Lambda,Cassandra,我有一个AWS Lambda函数,在该函数中我连接到一个Cassandra数据库。我试图在下次运行函数时重用会话,以减少函数的运行时间。当我运行该函数时,有时会收到警告消息。我如何修复这种奇怪的行为并消除警告消息 lambda_函数.py: import sessions cassandra_db_session = None cassandra_db_username = 'your-username' cassandra_db_password = 'your-password' cas

我有一个AWS Lambda函数,在该函数中我连接到一个Cassandra数据库。我试图在下次运行函数时重用会话,以减少函数的运行时间。当我运行该函数时,有时会收到警告消息。我如何修复这种奇怪的行为并消除警告消息

lambda_函数.py:

import sessions


cassandra_db_session = None
cassandra_db_username = 'your-username'
cassandra_db_password = 'your-password'
cassandra_db_endpoints = ['your-endpoint']
cassandra_db_port = 9142


def lambda_handler(event, context):
    global cassandra_db_session
    if not cassandra_db_session:
        cassandra_db_session = sessions.create_cassandra_session(
            cassandra_db_username,
            cassandra_db_password,
            cassandra_db_endpoints,
            cassandra_db_port
        )
    result = cassandra_db_session.execute('select * from "your-keyspace"."your-table";')
    return 'ok'
from ssl import SSLContext
from ssl import CERT_REQUIRED
from ssl import PROTOCOL_TLSv1_2
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider


def create_cassandra_session(db_username, db_password, db_endpoints, db_port):
    ssl_context = SSLContext(PROTOCOL_TLSv1_2)
    ssl_context.load_verify_locations('your-path/AmazonRootCA1.pem')
    ssl_context.verify_mode = CERT_REQUIRED
    auth_provider = PlainTextAuthProvider(username=db_username, password=db_password)
    cluster = Cluster(db_endpoints, ssl_context=ssl_context, auth_provider=auth_provider, port=db_port)
    session = cluster.connect()
    return session
sessions.py:

import sessions


cassandra_db_session = None
cassandra_db_username = 'your-username'
cassandra_db_password = 'your-password'
cassandra_db_endpoints = ['your-endpoint']
cassandra_db_port = 9142


def lambda_handler(event, context):
    global cassandra_db_session
    if not cassandra_db_session:
        cassandra_db_session = sessions.create_cassandra_session(
            cassandra_db_username,
            cassandra_db_password,
            cassandra_db_endpoints,
            cassandra_db_port
        )
    result = cassandra_db_session.execute('select * from "your-keyspace"."your-table";')
    return 'ok'
from ssl import SSLContext
from ssl import CERT_REQUIRED
from ssl import PROTOCOL_TLSv1_2
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider


def create_cassandra_session(db_username, db_password, db_endpoints, db_port):
    ssl_context = SSLContext(PROTOCOL_TLSv1_2)
    ssl_context.load_verify_locations('your-path/AmazonRootCA1.pem')
    ssl_context.verify_mode = CERT_REQUIRED
    auth_provider = PlainTextAuthProvider(username=db_username, password=db_password)
    cluster = Cluster(db_endpoints, ssl_context=ssl_context, auth_provider=auth_provider, port=db_port)
    session = cluster.connect()
    return session
第一次呼叫

Function logs:
START RequestId: b0080f19-15d0-443b-9cb1-eb5ecf024257 Version: $LATEST
[WARNING]   2020-10-04T14:23:37.715Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Cluster.__init__ called with contact_points specified, but no load_balancing_policy. In the next major version, this will raise an error; please specify a load-balancing policy. (contact_points = ['your-endpoint], lbp = None)
[WARNING]   2020-10-04T14:23:37.766Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Downgrading core protocol version from 66 to 65 for X.XXX.XX.XXX:XXXX. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
[WARNING]   2020-10-04T14:23:37.806Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Downgrading core protocol version from 65 to 4 for X.XXX.XX.XXX:XXXX. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
[WARNING]   2020-10-04T14:23:37.905Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Found multiple hosts with the same endpoint (X.XXX.XX.XXX:XXXX). Excluding peer X.XXX.XX.XXX
END RequestId: b0080f19-15d0-443b-9cb1-eb5ecf024257
REPORT RequestId: b0080f19-15d0-443b-9cb1-eb5ecf024257  Duration: 834.69 ms Billed Duration: 900 ms Memory Size: 128 MB Max Memory Used: 62 MB  Init Duration: 201.93 ms
Function logs:
START RequestId: 9f8af381-69df-40ac-a8ee-ad482df59760 Version: $LATEST
END RequestId: 9f8af381-69df-40ac-a8ee-ad482df59760
REPORT RequestId: 9f8af381-69df-40ac-a8ee-ad482df59760  Duration: 22.95 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 64 MB  
Function logs:
START RequestId: 2265835f-6334-4c4f-b1d5-24deb307aa8a Version: $LATEST
[WARNING]   2020-10-04T14:20:33.268Z    2265835f-6334-4c4f-b1d5-24deb307aa8a    Failed to create connection pool for new host X.XXX.XX.XXX:XXXX:
Traceback (most recent call last):
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 3175, in run_add_or_renew_pool
    new_pool = HostConnection(host, distance, self)
  File "/opt/python/lib/python3.8/site-packages/cassandra/pool.py", line 402, in __init__
    self._connection = session.cluster.connection_factory(host.endpoint)
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 1620, in connection_factory
    return self.connection_class.factory(endpoint, self.connect_timeout, *args, **kwargs)
  File "/opt/python/lib/python3.8/site-packages/cassandra/connection.py", line 760, in factory
    conn = cls(endpoint, *args, **kwargs)
  File "/opt/python/lib/python3.8/site-packages/cassandra/io/asyncorereactor.py", line 351, in __init__
    self._connect_socket()
  File "/opt/python/lib/python3.8/site-packages/cassandra/connection.py", line 827, in _connect_socket
    raise socket.error(sockerr.errno, "Tried connecting to %s. Last error: %s" %
OSError: [Errno 6] Tried connecting to [('X.XXX.XX.XXX:XXXX', XXXX)]. Last error: TLS/SSL connection has been closed (EOF) (_ssl.c:1123)[WARNING]   2020-10-04T14:20:33.298Z    2265835f-6334-4c4f-b1d5-24deb307aa8a    Host X.XXX.XX.XXX:XXXX has been marked down
END RequestId: 2265835f-6334-4c4f-b1d5-24deb307aa8a
REPORT RequestId: 2265835f-6334-4c4f-b1d5-24deb307aa8a  Duration: 51.60 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 64 MB
第二次呼叫

Function logs:
START RequestId: b0080f19-15d0-443b-9cb1-eb5ecf024257 Version: $LATEST
[WARNING]   2020-10-04T14:23:37.715Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Cluster.__init__ called with contact_points specified, but no load_balancing_policy. In the next major version, this will raise an error; please specify a load-balancing policy. (contact_points = ['your-endpoint], lbp = None)
[WARNING]   2020-10-04T14:23:37.766Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Downgrading core protocol version from 66 to 65 for X.XXX.XX.XXX:XXXX. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
[WARNING]   2020-10-04T14:23:37.806Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Downgrading core protocol version from 65 to 4 for X.XXX.XX.XXX:XXXX. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
[WARNING]   2020-10-04T14:23:37.905Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Found multiple hosts with the same endpoint (X.XXX.XX.XXX:XXXX). Excluding peer X.XXX.XX.XXX
END RequestId: b0080f19-15d0-443b-9cb1-eb5ecf024257
REPORT RequestId: b0080f19-15d0-443b-9cb1-eb5ecf024257  Duration: 834.69 ms Billed Duration: 900 ms Memory Size: 128 MB Max Memory Used: 62 MB  Init Duration: 201.93 ms
Function logs:
START RequestId: 9f8af381-69df-40ac-a8ee-ad482df59760 Version: $LATEST
END RequestId: 9f8af381-69df-40ac-a8ee-ad482df59760
REPORT RequestId: 9f8af381-69df-40ac-a8ee-ad482df59760  Duration: 22.95 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 64 MB  
Function logs:
START RequestId: 2265835f-6334-4c4f-b1d5-24deb307aa8a Version: $LATEST
[WARNING]   2020-10-04T14:20:33.268Z    2265835f-6334-4c4f-b1d5-24deb307aa8a    Failed to create connection pool for new host X.XXX.XX.XXX:XXXX:
Traceback (most recent call last):
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 3175, in run_add_or_renew_pool
    new_pool = HostConnection(host, distance, self)
  File "/opt/python/lib/python3.8/site-packages/cassandra/pool.py", line 402, in __init__
    self._connection = session.cluster.connection_factory(host.endpoint)
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 1620, in connection_factory
    return self.connection_class.factory(endpoint, self.connect_timeout, *args, **kwargs)
  File "/opt/python/lib/python3.8/site-packages/cassandra/connection.py", line 760, in factory
    conn = cls(endpoint, *args, **kwargs)
  File "/opt/python/lib/python3.8/site-packages/cassandra/io/asyncorereactor.py", line 351, in __init__
    self._connect_socket()
  File "/opt/python/lib/python3.8/site-packages/cassandra/connection.py", line 827, in _connect_socket
    raise socket.error(sockerr.errno, "Tried connecting to %s. Last error: %s" %
OSError: [Errno 6] Tried connecting to [('X.XXX.XX.XXX:XXXX', XXXX)]. Last error: TLS/SSL connection has been closed (EOF) (_ssl.c:1123)[WARNING]   2020-10-04T14:20:33.298Z    2265835f-6334-4c4f-b1d5-24deb307aa8a    Host X.XXX.XX.XXX:XXXX has been marked down
END RequestId: 2265835f-6334-4c4f-b1d5-24deb307aa8a
REPORT RequestId: 2265835f-6334-4c4f-b1d5-24deb307aa8a  Duration: 51.60 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 64 MB
第三次

Function logs:
START RequestId: b0080f19-15d0-443b-9cb1-eb5ecf024257 Version: $LATEST
[WARNING]   2020-10-04T14:23:37.715Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Cluster.__init__ called with contact_points specified, but no load_balancing_policy. In the next major version, this will raise an error; please specify a load-balancing policy. (contact_points = ['your-endpoint], lbp = None)
[WARNING]   2020-10-04T14:23:37.766Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Downgrading core protocol version from 66 to 65 for X.XXX.XX.XXX:XXXX. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
[WARNING]   2020-10-04T14:23:37.806Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Downgrading core protocol version from 65 to 4 for X.XXX.XX.XXX:XXXX. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
[WARNING]   2020-10-04T14:23:37.905Z    b0080f19-15d0-443b-9cb1-eb5ecf024257    Found multiple hosts with the same endpoint (X.XXX.XX.XXX:XXXX). Excluding peer X.XXX.XX.XXX
END RequestId: b0080f19-15d0-443b-9cb1-eb5ecf024257
REPORT RequestId: b0080f19-15d0-443b-9cb1-eb5ecf024257  Duration: 834.69 ms Billed Duration: 900 ms Memory Size: 128 MB Max Memory Used: 62 MB  Init Duration: 201.93 ms
Function logs:
START RequestId: 9f8af381-69df-40ac-a8ee-ad482df59760 Version: $LATEST
END RequestId: 9f8af381-69df-40ac-a8ee-ad482df59760
REPORT RequestId: 9f8af381-69df-40ac-a8ee-ad482df59760  Duration: 22.95 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 64 MB  
Function logs:
START RequestId: 2265835f-6334-4c4f-b1d5-24deb307aa8a Version: $LATEST
[WARNING]   2020-10-04T14:20:33.268Z    2265835f-6334-4c4f-b1d5-24deb307aa8a    Failed to create connection pool for new host X.XXX.XX.XXX:XXXX:
Traceback (most recent call last):
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 3175, in run_add_or_renew_pool
    new_pool = HostConnection(host, distance, self)
  File "/opt/python/lib/python3.8/site-packages/cassandra/pool.py", line 402, in __init__
    self._connection = session.cluster.connection_factory(host.endpoint)
  File "/opt/python/lib/python3.8/site-packages/cassandra/cluster.py", line 1620, in connection_factory
    return self.connection_class.factory(endpoint, self.connect_timeout, *args, **kwargs)
  File "/opt/python/lib/python3.8/site-packages/cassandra/connection.py", line 760, in factory
    conn = cls(endpoint, *args, **kwargs)
  File "/opt/python/lib/python3.8/site-packages/cassandra/io/asyncorereactor.py", line 351, in __init__
    self._connect_socket()
  File "/opt/python/lib/python3.8/site-packages/cassandra/connection.py", line 827, in _connect_socket
    raise socket.error(sockerr.errno, "Tried connecting to %s. Last error: %s" %
OSError: [Errno 6] Tried connecting to [('X.XXX.XX.XXX:XXXX', XXXX)]. Last error: TLS/SSL connection has been closed (EOF) (_ssl.c:1123)[WARNING]   2020-10-04T14:20:33.298Z    2265835f-6334-4c4f-b1d5-24deb307aa8a    Host X.XXX.XX.XXX:XXXX has been marked down
END RequestId: 2265835f-6334-4c4f-b1d5-24deb307aa8a
REPORT RequestId: 2265835f-6334-4c4f-b1d5-24deb307aa8a  Duration: 51.60 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 64 MB

我怀疑你的方法,因为Lambda的主要用途是无服务器。它不应用于存储会话以供下次调用。@Leondkr在本文的介绍部分,作者解释了为什么应在AWS Lambda函数中重复使用数据库连接。我完全同意作者的观点。现在,我似乎已经成功地重新使用了Cassandra连接,但警告消息让我感到困扰。如果你有更具建设性的想法,我很乐意倾听。相关:和@jarmod我已经读了第二篇文章。当然,区别在于
集群的设置。第二篇文章的作者使用了这样的代码:
cluster=cluster(ENDPOINT.split(“,”),connection\u class=TwistedConnection,load\u balancement\u policy=dcawarerroundrobinipolicy(local\u dc=local\u dc))
。我更关注官方AWS。在我的
集群
设置中,我是否需要
连接类
负载平衡策略
选项以供您参考?