I have a problem. I created a SAM Project and I want to used globals variables between all the consecutive lambda functions. For example, I want to connect to the database and use the connection in all the lambda of the step functions without reconnecting. As you can see :
Lambda function that connect to the database :
# import requests
'''
Get authetication token from RDS
'''
rds_client = boto3.client('rds')
auth_token = rds_client.generate_db_auth_token( os.environ['DB_HOST_PROD'], 3306, os.environ['DB_USER_PROD'])
logging.debug("response from generate_db_auth_token")
logging.debug(auth_token)
'''
This method will return an authentication token in the
form of a signed url. The we will use this token to create the bd connection
'''
# construct SSL
ssl = {'ca': '/opt/python/rds-combined-ca-bundle.pem'}
# Creating connection
db_connection_dw = pymysql.connect(
host=os.environ['DB_HOST_PROD'],
port=3306,
user=os.environ['DB_USER_PROD'],
passwd=auth_token,
db=os.environ['DB_NAME_PROD'],
charset='utf8',
ssl=ssl,
connect_timeout=5)
logging.debug("SUCCESS: Connection to MySQL database succeeded")
Template yaml of the SAM Project :
Parameters:
LambdaSg:
Description: "Lambda security group ID"
Type: "CommaDelimitedList"
# Type: List<AWS::EC2::SecurityGroup::Id>
LambdaSubnets:
Description: "Lambda subnets"
Type: List<AWS::EC2::Subnet::Id>
DBResourceId:
Type: String
Description: "RDS-DB instance resource identifier"
DBEndpoint:
Type: String
Description: "Amazon RDS MySQL endpoint"
DBName:
Type: String
Description: "Name of the database connected"
DBUsername:
Type: String
NoEcho: true
EDIT : Lambda functions are stateless and isolated environments. So the connection cannot be reuse in another lambda function. Therefore, it is not a problem to open many database connection.
CodePudding user response:
It isn't possible to share connections between lambdas. Lambda functions are stateless and isolated environments. Initiate one connection per lambda outside of the handler function. This connection will be re-used (in the same lambda only) in subsequent lambda invocations (as long as the lambda container is live).
Therefore it is better to avoid closing the connection, so that it can be re-used in the same lambda container. Unless you have too many lambdas and reach the database connection limit, this shouldn't be a problem. It is some kind of "connection pooling".