My Lambda function is working inside a VPC and connects to a DB that is only reachable within VPC. And my script connects to the DB on initialisation. The Problem is when I want to deploy because it can't connect to the database it doesn't deploy. What I did is below
import os
if os.environ["CONFIG"] == 'dev':
db_host = "127.0.0.1"
db_username = "xxx"
db_password = "xxx"
db_name = "xxx"
db_port = 10000
else:
db_host = "xxxx.eu-west-1.rds.amazonaws.com"
db_username = "xxx"
db_password = "xxx"
db_name = "xxx"
db_port = 3306
So I can deploy it to AWS and then I have to change the env variable CONFIG to prod (something other than 'dev') manually to let the code connect to DB properly. I'm sure this is not the way to do. What should I be doing instead of this? Please help ..
You should set the correct environment variables for each of your stages (dev, production, etc.). Take a look at the documentation.
@BigChief45 .. Thanks for that. I have already looked at it and now put the stages, But the problem doesn't solved.
So if I do ,
chalice deploy --profile xxx --stage prod
with this config.
{
"profile": "xxx",
"stages": {
"dev": {
"api_gateway_stage": "api",
"environment_variables": {
"CONFIG": "dev"
}
},
"prod": {
"api_gateway_stage": "api",
"environment_variables": {
"CONFIG": "prod"
},
"lambda_functions": {
"index": {
"subnet_ids": ["subnet-xxx", "subnet-xxx"],
"security_group_ids": ["sg-xxx"]
}
}
}
},
"version": "2.0",
"app_name": "xxxx"
}
Chalice still wants to connect to the DB and i doesn't let me deploy..
So to make it more clear, I do this on my module..
import logging
from datetime import datetime, date
from chalicelib import Db
from chalicelib import t_config
conn = Db.conn() # this is the place that I make the connection. I don't want to create a connection on each request that's why I put it here.
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def a_function(id, xxx):
You should definitely move your initialization logic out of the module level. Importing a module shouldn't perform operations for you or you run into issues like this. You could lazily load it with a pattern like this:
conn = None
def get_conn():
global conn
if conn is None:
conn = Db.conn()
return conn
@app.route('/')
def index():
conn = get_conn()
...
Now its only loaded when you actually need it, and it is reused between requests as long as your container is up.
This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.
Most helpful comment
You should definitely move your initialization logic out of the module level. Importing a module shouldn't perform operations for you or you run into issues like this. You could lazily load it with a pattern like this:
Now its only loaded when you actually need it, and it is reused between requests as long as your container is up.