Summarize the following text in less than 50 words. Let's think step by step: detailed guide of how to set up mlflow on gcp in a secure way last updated on july 24, 2024 by editorial team author(s): yuki shizuya originally published on towards ai. introduction i recently needed to set up an environment of mlflow, a popular open-source mlops platform, for internal team use. we generally use gcp as an experimental platform, so i wanted to deploy mlflow on gcp, but i couldn't find a detailed guide on how to do so securely. there are several points that are stuck for beginners like me, so i decided to share a step-by-step guide to securely set up mlflow on gcp. in this blog, i will share how to deploy mlflow on cloud run with cloud iap, vpc egress, and gcs fuse. i referenced this great article [1, 2], and please note that this setup is not for free. system architecture the overall architecture is the diagram below. - cloud run for mlflow backend server mlflow needs a backend server to serve the ui and enable remote storage of run artifacts. we deploy it on cloud run to save costs because it doesn't need to run constantly. - cloud iap + cloud load balancing(https) for security cloud iap authenticates only authorized users who have an appropriate iam role. intuitively, an iam role defines fine-grained user access management. since we want to deploy a service for internal team use, cloud iap suits this situation. when using cloud iap, we must prepare the external http(s) load balancer, so we can configure both systems. - cloud storage for mlflow artifact storage mlflow needs to store artifacts such as trained models, training configuration files, etc. cloud storage is a low-cost, managed service for storing unstructured data (not table data). although we can set global ip for cloud storage, we want to avoid exposing it outside; thus, we use gcs fuse to be able to connect even without global ip. - cloud sql for mlflow metadata database mlflow also needs to store metadata such as metrics, hyperparameters of models, evaluation results, etc. cloudsql is a managed relational database service, so it is suitable for such a use case. we also want to avoid exposing it outside; thus, we use vpc egress to connect securely. now, let's configure this architecture step by step! i will use the gcloud cli as much as possible to reproduce results easily, but i will use gui for some parts. 1. prerequisites - install the gcloud cli from the official site i used a mac(m2 chip) with macos 14.4.1 for my environment. so, i installed the macos version. you can download it based on your environment. if you want to avoid setting up the environment in your local, you can also use cloud shell. for windows users, i recommend using cloud shell. - install direnv from the official site direnv is very convenient to manage environment variables. it can load and unload them depending on the current directory. if you use macos, you can download it using bash. note that you must hook direnv into your shell to correspond to your shell environment. - create google cloud project and user account i assume that you already have a google cloud project. if not, you can follow this instruction. furthermore, you already have a user account associated with that project. if not, please follow this site, and please run the following command. gcloud auth login - clone the git repository i compiled the necessary files for this article, so clone it in your preferred location. git clone https://github.com/tanukon/mlflow_on_gcp_cloudiap.git cd mlflow_on_gcp_cloudiap 2. define variables for the first step, we configure the necessary variables to develop the mlflow environment. please create a new file called .envrc. you need to set the following variables. export project_id = <the id of your google cloud project> export role_id=<the name for your custom role for mlflow server> export service_account_id=<the name for your service account> export vpc_network_name=<the name for your vpc network> export vpc_peering_name=<the name for your vpc peering service> export cloud_sql_name=<the name for cloudsql instance> export region=<set your preferred region> export zone=<set your preferred zone> export cloud_sql_user_name=<the name for cloudsql user> export cloud_sql_user_password=<the password for cloudsql user> export db_name=<the database name for cloudsql> export bucket_name=<the gcs bucket name> export repository_name=<the name for the artifact repository> export connector_name=<the name for vpc connector> export docker_file_name=<the name for docker file> export project_number=<the project number of your project> export domain_name=<the domain name you want to get> you can check the project id and number in the >> cloud overview >> dashboard. you also need to define the region and zone based on the google cloud settings from here. if you don't care about network latency, anywhere is ok. besides those variables, you can name others freely. after you define them, you need to run the following command. direnv allow . 3. enable api and define iam role the next step is to enable the necessary apis. to do this, run the commands below one by one. gcloud services enable servicenetworking.googleapis.com gcloud services enable artifactregistry.googleapis.com gcloud services enable run.googleapis.com gcloud services enable domains.googleapis.com next, create a new role to include the necessary permissions. gcloud iam roles create $role_id --project=$project_id --title=mlflow_server_requirements --description="necessary iam permissions to configure mlflow server" --permissions=compute.networks.list,compute.addresses.create,compute.addresses.list,servicenetworking.services.addpeering,storage.buckets.create,storage.buckets.list then, create a new service account for the mlflow backend server (cloud run). gcloud iam service-accounts create $service_account_id we attach a role we made in the previous step. gcloud projects add-iam-policy-binding $project_id --member=serviceaccount:$service_account_id@$project_id.iam.gserviceaccount.com --role=projects/$project_id/roles/$role_id moreover, we need to attach roles below. please run the command one by one. gcloud projects add-iam-policy-binding $project_id --member=serviceaccount:$service_account_id@$project_id.iam.gserviceaccount.com --role=roles/compute.networkuser gcloud projects add-iam-policy-binding $project_id --member=serviceaccount:$service_account_id@$project_id.iam.gserviceaccount.com --role=roles/artifactregistry.admin 4. create a vpc network we want to instantiate our database and storage without global ip to prevent public access; thus, we create a vpc network and instantiate them inside a vpc. gcloud compute networks create $vpc_network_name --subnet-mode=auto --bgp-routing-mode=regional --mtu=1460 we need to configure private services access for cloudsql. in such a situation, gcp offers vpc peering, so we can use this function. i referenced the official guide here. gcloud compute addresses create google-managed-services-$vpc_network_name --global --purpose=vpc_peering --addresses=192.168.0.0 --prefix-length=16 --network=projects/$project_id/global/networks/$vpc_network_name in the above code, addresses are anything fine if addresses satisfy the condition of private ip addresses. next, we create a private connection using vpc peering. gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com --ranges=google-managed-services-$vpc_network_name --network=$vpc_network_name --project=$project_id 5. configure cloudsql with a private ip address now, we configure cloudsql with a private ip address using the following command. gcloud beta sql instances create $cloud_sql_name --project=$project_id --network=projects/$project_id/global/networks/$vpc_network_name --no-assign-ip --enable-google-private-path --database-version=postgres_15 --tier=db-f1-micro --storage-type=hdd --storage-size=200gb --region=$region it takes a couple of minutes to build a new instance. we don't need a high-spec instance for cloudsql because it is only used internally, so i used the smallest instance to save costs. you can ensure your instance is configured for private services access using the following command. gcloud beta sql instances patch $cloud_sql_name --project=$project_id --network=projects/$project_id/global/networks/$vpc_network_name --no-assign-ip --enable-google-private-path for the next step, we need to create a login user so that mlflow backend can access. gcloud sql users create $cloud_sql_user_name --instance=$cloud_sql_name --password=$cloud_sql_user_password furthermore, we must create the database where the data will be stored. gcloud sql databases create $db_name --instance=$cloud_sql_name 6. create google cloud storage(gcs) without global ip address we will create a google cloud storage(gcs) bucket to store experiment artifacts. your bucket name must be unique. gcloud storage buckets create gs://$bucket_name --project=$project_id --uniform-bucket-level-access --public-access-prevention to secure our bucket, we add iam-policy-binding to the created one. thus, the only service account we created can access the bucket. gcloud storage buckets add-iam-policy-binding gs://$bucket_name --member=serviceaccount:$service_account_id@$project_id.iam.gserviceaccount.com --role=projects/$project_id/roles/$role_id 7. create secrets for credential information we store credential information, such as cloudsql uri and bucket uri, on google cloud secrets to securely retrieve them. we can create a secret by executing the following commands: gcloud secrets create database_url gcloud secrets create bucket_url now, we need to add the exact values for them. we define cloudsql url in the following format. "postgresql://<cloud_sql_user_name>:<cloud_sql_user_password>@<private ip address>/<db_name>?host=/cloudsql/<project_id>:<region>:<cloud_sql_name>" you can check your instance's private ip address through your cloudsql gui page. the red line rectangle part is your instance's private ip address. you can set your secret using the following command. please replace the placeholders in your setting. echo -n "postgresql://<cloud_sql_user_name>:<cloud_sql_user_password>@<private ip address>/<db_name>?host=/cloudsql/<project_id>:<region>:<cloud_sql_name>" | gcloud secrets versions add database_url --data-file=- for the gcs, we will use gcs fuse to mount gcs directly to cloud run. therefore, we need to define the directory we want to mount to the secret. for example, "/mnt/gcs". echo -n "<directory path>" | gcloud secrets versions add bucket_url --data-file=- 8. create artifact registry we must prepare the artifact registry to store a dockerfile for the cloud run service. first of all, we create a repository of it. gcloud artifacts repositories create $repository_name --location=$region --repository-format=docker next, we build a dockerfile and push it to the artifact registry. gcloud builds submit --tag $region-docker.pkg.dev/$project_id/$repository_name/$docker_file_name 9. prepare domain for an external load balancer before deploying our container to cloud run, we need to prepare an external load balancer. an external load balancer requires a domain; thus, we must get a domain for our service. firstly, you verify that other services are not using the domain you want to use. gcloud domains registrations search-domains $domain_name if another service uses it, consider the domain name again. after you check whether your domain is available, you need to choose a dns provider. in this blog, i used cloud dns. now, you can register your domain. it costs $12~ per year. please replace <your domain> placeholder. gcloud dns managed-zones create $zone --description="the domain for internal ml service" --dns-name=$domain_name.<your domain> then, you can register your domain. please replace <your domain> placeholder again. gcloud domains registrations register $domain_name.<your domain> 10. deploy cloud run using gui now, we deploy cloud run using a registered dockerfile. after this deployment, we will configure the cloud iap. please click cloud run >> create service. first, you must pick up the container image from your artifact registry. after you pick it up, the service name will automatically be filled in. you set the region as the same as the artifact registry location. we want to allow external load balancer traffic related to the cloud iap, so we must check it. next, the default setting allows us to use only 512 mb, which is not enough to run the mlflow server (i encountered a memory shortage error). we change the cpu allocation from 512 mb to 8gb. we need to get the secret variables for the cloudsql and gcs bucket path. please set variables following the image below. the network setting below is necessary to connect cloudsql and gcs bucket (vpc egress setting). for network and subnet placeholder, you must choose your vpc name. in the security tab, you must choose the service account defined previously. after scrolling to the end of the setting, you will see the cloud sql connections. you need to choose your instance. after you set up, please click the create button. if there is no error, the cloud run service will be deployed in your project. it takes a couple of minutes. after deploying the cloud run service, we need to update and configure the gcs fuse setting. please replace the placeholders corresponding to your environment. gcloud beta run services update <your service name> --add-volume name=gcs,type=cloud-storage,bucket=$bucket_name --add-volume-mount volume=gcs,mount-path=<bucket_url path> so far, we haven't been able to access the mlflow server because we haven't set up an external load balancer with cloud iap. google offers a convenient integration with other services for cloud run. please open the cloud run page for your project and click your service name. you will see the page below. after you click add integration, you will see the page below. please click choose custom domains google cloud load balancing. if there are any services you haven't granted, please click grant all. after that, please enter the domain you got in the previous section. after you fill in domain 1 and service 1, new resources will be created. it takes 5~30 minutes. after a while, a table is created with the dns records you need to configure: use this to update your dns records at your dns provider. please move to the cloud dns page and click your zone name. then, you will see the page below. please click the add standard. now, you can set the dns record using the global ip address shown in a table. the resource record type is a. ttl sets the default value and sets your global ip address in the table to ipv4 address 1 placeholder. after you update your dns at your dns provider, it can take up to 45 minutes to provision the ssl certificate and begin routing traffic to your service. so, please take a break! if you can see the screen below, you can successfully create an external load balancer for cloud run. finally, we can configure cloud iap. please open the security >> identity-aware proxy page and click the configure consent screen. you will see the screen below, please choose internal in user type and click create button. in the app name, you need to name your app and put your mail address for user support email and developer contact information. then click save and continue. you can skip the scope page, and create. after you finish configuring the oauth screen, you can turn on iap. check the checkbox and click the turn on button. now, please return to the cloud run integration page. when you access the url displayed in the custom domain, you will see the authentication failed display like below. the reason why you got this is that we need to add another iam policy to access our app. you need to add "roles/iap.httpsresourceaccessor" to your account. please replace <your account>. gcloud projects add-iam-policy-binding $project_id --member='user:<your account>' --role=roles/iap.httpsresourceaccessor after waiting a few minutes until the setting is reflected, you can finally see the mlflow gui page. 11. configure programmatic access for iap authentication to configure the programmatic access for iap, we use an oauth client. please move to apis & services >> credentials. the previous configuration of cloud iap automatically created an oauth 2.0 client; thus, you can use it! please copy the client id. next, you must download the service account key created in the previous process. please move to the iam & admin >> service accounts and click your account name. you will see the following screen. then, move to the keys tab and click add key >> create new key. set key type as "json" and click create. please download the json file and change the filename. please add the lines below to the .envrc file. note that replace placeholders based on your environment. export mlflow_client_id=<your oauth client id> export mlflow_tracking_uri=<your service url> export google_application_credentials=<path for your service account credential json file> don't forget to update the environment variables using the following command. direnv allow . i assume you already have a python environment and have finished installing the necessary libraries. i prepared test_run.py to check that the deployment works correctly. inside test_run.py, there is an authentication part and a part for sending parameters to the mlflow server part. when you run test_run.py, you can see the dummy results stored in the mlflow server. this is the end of this blog. thank you for reading my article! if i missed anything, please let me know. references [1] vargas, a., how to launch an mlflow server with continuous deployment on gcp in minutes, medium [2] mlflowgoogle kubernetes engine, cyberagent ai tech studio join thousands of data leaders on the ai newsletter. join over 80,000 subscribers and keep up to date with the latest developments in ai. from research to projects and ideas. if you are building an ai startup, an ai-related product, or a service, we invite you to consider becoming a sponsor. published via towards ai