|
| 1 | +# PGAdapter Cloud Run Sample for Python |
| 2 | + |
| 3 | +This sample application shows how to build and deploy a Python application with PGAdapter to Google Cloud Run. |
| 4 | + |
| 5 | +## Build |
| 6 | + |
| 7 | +First build the Docker image locally and tag it. Replace the `IMAGE_URL` in the script with your own repository. |
| 8 | +Make sure that the repository that you are referencing exists. |
| 9 | +See https://cloud.google.com/artifact-registry/docs/repositories/create-repos for more information on how to set up |
| 10 | +Google Cloud artifact repositories. |
| 11 | + |
| 12 | +```shell |
| 13 | +export IMAGE_URL=my-location-docker.pkg.dev/my-project/my-repository/my-image:latest |
| 14 | +docker build . --tag $IMAGE_URL |
| 15 | +``` |
| 16 | + |
| 17 | +Example: `export IMAGE_URL=europe-north1-docker.pkg.dev/my-project/sample-test/pgadapter-sample:latest` |
| 18 | + |
| 19 | +Test the Docker image locally: |
| 20 | + |
| 21 | +```shell |
| 22 | +docker run \ |
| 23 | + -p 8080:8080 \ |
| 24 | + -v /path/to/local_credentials.json:/credentials.json:ro \ |
| 25 | + --env GOOGLE_APPLICATION_CREDENTIALS=/credentials.json \ |
| 26 | + --env SPANNER_PROJECT=my-project \ |
| 27 | + --env SPANNER_INSTANCE=my-instance \ |
| 28 | + --env SPANNER_DATABASE=my-database \ |
| 29 | + $IMAGE_URL |
| 30 | +``` |
| 31 | + |
| 32 | +Verify that the connection to Cloud Spanner works correctly by opening a new shell and executing: |
| 33 | + |
| 34 | +```shell |
| 35 | +curl http://localhost:8080 |
| 36 | +``` |
| 37 | + |
| 38 | +## Deploying to Cloud Run |
| 39 | + |
| 40 | +Push the Docker image to Artifact Registry: |
| 41 | + |
| 42 | +```shell |
| 43 | +gcloud auth configure-docker |
| 44 | +docker push $IMAGE_URL |
| 45 | +``` |
| 46 | + |
| 47 | +Then deploy the image as a service on Cloud Run: |
| 48 | + |
| 49 | +```shell |
| 50 | +export SERVICE=my-service |
| 51 | +gcloud run deploy $SERVICE \ |
| 52 | + --image $IMAGE_URL \ |
| 53 | + --update-env-vars SPANNER_PROJECT=my-project,SPANNER_INSTANCE=my-instance,SPANNER_DATABASE=my-database |
| 54 | +``` |
| 55 | + |
| 56 | +__NOTE__: This example does not specify any credentials for PGAdapter when it is run on Cloud Run. This means that |
| 57 | +PGAdapter will use the default credentials that is used by Cloud Run. This is by default the default compute engine |
| 58 | +service account. See https://cloud.google.com/run/docs/securing/service-identity for more information on how service |
| 59 | +accounts work on Google Cloud Run. |
| 60 | + |
| 61 | +Test the service (replace URL with your actual service URL): |
| 62 | + |
| 63 | +```shell |
| 64 | +curl https://my-service-xyz.run.app |
| 65 | +``` |
| 66 | + |
| 67 | +### Authenticated Cloud Run Service |
| 68 | + |
| 69 | +If your Cloud Run service requires authentication, then first add an IAM binding for your own account and include |
| 70 | +an authentication header with the request: |
| 71 | + |
| 72 | +```shell |
| 73 | +gcloud run services add-iam-policy-binding $SERVICE \ |
| 74 | + --member='user:your-email@gmail.com' \ |
| 75 | + --role='roles/run.invoker' |
| 76 | +curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" https://my-service-xyz.run.app |
| 77 | +``` |
| 78 | + |
0 commit comments