is it possible to add a cloud sql connection with ...
# prefect-gcp
s
is it possible to add a cloud sql connection with a serverless push deployment? i have a serverless push deployment that submits cloud run jobs. it needs to connect with cloud sql. the gcp docs say to add the cloud sql instance to the cloud run job, but i’m not sure how to do that when the job is created by prefect
for anybody else wondering, you have to update your work pool base template by crafting it fit with this resource declaration: https://cloud.google.com/run/docs/reference/rest/v1/namespaces.jobs#JobSpec
this is what the
job_configuration
object in our work pool template looks like
Copy code
"job_configuration": {
    "env": "{{ env }}",
    "name": "{{ name }}",
    "labels": "{{ labels }}",
    "region": "{{ region }}",
    "command": "{{ command }}",
    "timeout": "{{ timeout }}",
    "job_body": {
      "kind": "Job",
      "spec": {
        "template": {
          "metadata": {
            "name": "{{ name }}",
            "annotations": {
              "<http://run.googleapis.com/cloudsql-instances|run.googleapis.com/cloudsql-instances>": "{{ cloudsql_instance }}"
            }
          },
          "spec": {
            "template": {
              "spec": {
                "containers": [
                  {
                    "image": "{{ image }}",
                    "command": "{{ command }}",
                    "resources": {
                      "limits": {
                        "cpu": "{{ cpu }}",
                        "memory": "{{ memory }}"
                      },
                      "requests": {
                        "cpu": "{{ cpu }}",
                        "memory": "{{ memory }}"
                      }
                    }
                  }
                ],
                "timeoutSeconds": "{{ timeout }}",
                "serviceAccountName": "{{ service_account_name }}"
              }
            }
          }
        }
      },
      "metadata": {
        "name": "{{ name }}",
        "annotations": {
          "<http://run.googleapis.com/launch-stage|run.googleapis.com/launch-stage>": "BETA",
          "<http://run.googleapis.com/vpc-access-connector|run.googleapis.com/vpc-access-connector>": "{{ vpc_connector_name }}"
        }
      },
      "apiVersion": "<http://run.googleapis.com/v1|run.googleapis.com/v1>"
    },
    "keep_job": "{{ keep_job }}",
    "credentials": "{{ credentials }}"
  }
the annotations is key. you can get your existing work pool template from the “advanced” tab where you edit your work pool. and use that to figure out how to fit the gcp JobSpec
for the vpc_connector_name variable, you’ll need to define that in the work pool template variables like this:
Copy code
"cloudsql_instance": {
        "type": "string",
        "title": "Cloud SQL Instances",
        "description": "The Cloud SQL instance to connect to. Must be in the format `project:region:instance`. See <https://cloud.google.com/sql/docs/mysql/connect-run>."
      }
and you can set the variable value in prefect.yaml like this
Copy code
work_pool:
    name: my-work-pool
    work_queue_name: default
    job_variables:
      cloud_sql_instances: my-cloud-sql-instance