def create_lambda_app(node_id: str) -> FlaskLambda: """Create flask application for hosting on AWS Lambda. Args: node_id: ID used to identify this node. Returns: app : FlaskLambda App instance. """ # Register dialects to talk to AWS RDS via Data API import sqlalchemy_aurora_data_api sqlalchemy_aurora_data_api.register_dialects() database_name = os.environ.get("DB_NAME") cluster_arn = os.environ.get("DB_CLUSTER_ARN") secret_arn = os.environ.get("DB_SECRET_ARN") app = FlaskLambda(__name__) app.config.from_mapping( DEBUG=False, SQLALCHEMY_DATABASE_URI=f"mysql+auroradataapi://:@/{database_name}", SQLALCHEMY_ENGINE_OPTIONS={ "connect_args": dict(aurora_cluster_arn=cluster_arn, secret_arn=secret_arn) }, SQLALCHEMY_TRACK_MODIFICATIONS=False, ) # Register app blueprints from .main import ( auth, data_centric_routes, hook, local_worker, main_routes, model_centric_routes, ws, ) # Add a test route @main_routes.route("/test-deployment/") def test(): return Response( json.dumps({"message": "Serverless deployment successful."}), status=200, mimetype="application/json", ) # set_node_id(id) local_worker.id = node_id hook.local_worker._known_workers[node_id] = local_worker local_worker.add_worker(hook.local_worker) # Register app blueprints app.register_blueprint(main_routes, url_prefix=r"/") app.register_blueprint(model_centric_routes, url_prefix=r"/model-centric") app.register_blueprint(data_centric_routes, url_prefix=r"/data-centric") # Setup database global db db.init_app(app) s = app.app_context().push() # Push the app into context try: db.Model.metadata.create_all( db.engine, checkfirst=False) # Create database tables seed_db() # Seed the database except Exception as e: print("Error", e) db.session.commit() # Set Authentication configs app = auth.set_auth_configs(app) CORS(app) # Threads executor.init_app(app) app.config["EXECUTOR_PROPAGATE_EXCEPTIONS"] = True app.config["EXECUTOR_TYPE"] = "thread" return app
import sqlalchemy from sqlalchemy import create_engine from sqlalchemy_aurora_data_api import register_dialects register_dialects() cluster_arn = 'arn:aws:rds:us-east-1:797308887321:cluster:serratus-aurora' secret_arn = 'arn:aws:secretsmanager:us-east-1:797308887321:secret:rds-db-credentials/cluster-KOFPN4Q2TKDBO5FHY6QO5M3S7Q/serratus-agdBn9' table_name = 'srarun' engine = create_engine('postgresql+auroradataapi://:@/summary', connect_args=dict(aurora_cluster_arn=cluster_arn, secret_arn=secret_arn)) dtype = {} # infer from pandas dataframe def upload(df): with engine.connect() as con: df.to_sql(table_name, con, if_exists='append', index=False, dtype=dtype)
import os from sqlalchemy import create_engine import sqlalchemy_aurora_data_api sqlalchemy_aurora_data_api.register_dialects() def get_sqlalchemy_engine(): cluster_arn = os.environ.get("DB_CLUSTER_ARN") secret_arn = os.environ.get("DB_SECRET_ARN") return create_engine( "postgresql+auroradataapi://:@/postgres", connect_args=dict(aurora_cluster_arn=cluster_arn, secret_arn=secret_arn), ) def get_test_sqlalchemy_engine(): return create_engine("postgresql://*****:*****@localhost:6543/postgres")
import os import sys from sqlalchemy import create_engine import sqlalchemy_aurora_data_api as aurora import logging from alembic import context aurora.register_dialects() logging.basicConfig() logging.getLogger("aurora_data_api").setLevel(logging.DEBUG) log = logging.getLogger('db-migration') log.setLevel(logging.DEBUG) # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config # Interpret the config file for Python logging. # This line sets up loggers basically. # fileConfig(config.config_file_name) # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata target_metadata = None # other values from the config, defined by the needs of env.py,
def setUpClass(cls): register_dialects() cls.db_name = os.environ.get("AURORA_DB_NAME", __name__) dialect = "postgresql+auroradataapi://" # dialect = "postgresql+psycopg2://" + getpass.getuser() cls.engine = create_engine(dialect + ':@/' + cls.db_name)
def setUpClass(cls): register_dialects() cls.db_name = os.environ.get("AURORA_DB_NAME", __name__) cls.engine = create_engine(cls.dialect + ':@/' + cls.db_name + "?charset=utf8mb4")