Migrating to Cloud Run's Secret Manager Integration from Libraries
Tags: googlecloud, serverless, googlecloudrun, secretmanager
Published: 2021-05-27 16:36:32

Secret Manager is easier to use with Cloud Run since I last wrote about "[serverless mysteries](https://dev.to/googlecloud/serverless-mysteries-with-secret-manager-libraries-on-google-cloud-3a1p)". At the time of that writing, the best way to use a Secret in Cloud Run was to add code to your service to integrate with the Secret Manager API. Now Cloud Run does that for you!

In this post we'll migrate the "Loyalty Checker" service from using the [Secret Manager](https://cloud.google.com/secret-manager/docs) client library to use Cloud Run's direct Secret Manager integration. (As of this writing, the integration is still in preview.)

## Prepare to follow along

If you're still set up from last time, just open [Cloud Shell](https://console.cloud.google.com/home/dashboard?cloudshell=true) and continue where we left off.

Otherwise,  please walk through the [last post](https://dev.to/googlecloud/serverless-mysteries-with-secret-manager-libraries-on-google-cloud-3a1p) then continue here to enjoy the feeling of deleting code.

## Modify the Loyalty Checker code

Direct integration with Secret Manager makes the code simpler:

1. Remove code to initialize the client library
2. Remove code to retrieve the secret on the first request
3. Rename the `SECRET_NAME` variable to `SECRET_VALUE`, the service will have direct access to the secret from the environment variable.

After these changes the service has shrunk by 46 lines of code, and no longer has any dependencies on Secret Manager:

{% gist https://gist.github.com/grayside/9b70d398d4531c141c38edff4c88a457 file=index.v2.js %}

## Re-deploy the Service

We'll combine the Secret integration with the new all-in-one deploy command:

```sh
gcloud beta run deploy loyalty \
  --source . \
  --remove-env-vars SECRET_NAME \
  --update-secrets SECRET_VALUE=you-know-who:1
```

* The `--source` flag tells gcloud to check for a Dockerfile in the current directory and use Cloud Build under the hood to build a container image without a separate command. (Read more about [deploying from source code](https://cloud.google.com/run/docs/deploying-source-code) if you're curious about the details.)
* The `--remove-env-vars` flag removes the no-longer-needed reference to a secret for our code to load.
* The `--update-secrets` flag integrates a new secret with the service without disrupting any existing secrets. (Use `--set-secrets` to simultaneously remove any the other secrets.)

One of the nice things about Cloud Run's secrets flag is how you can use just the "resource ID" part of the overall resource name. Secrets are normally referenced as "`projects/PROJECT_ID/secrets/SECRET_ID/versions/VERSION`" but all you need to reference a secret in the same project is `SECRET_ID:VERSION`.

## No IAM Update?!

There's no need to change IAM settings. Last time we set up a dedicated service account and gave it access to the secret. The same configuration works for client libraries or for this direct integration.

## Next Steps

If you want to be able to "live refresh" the secret in your service, check out the [documentation to learn how to mount your secrets as a volume](https://cloud.google.com/run/docs/configuring/secrets). The environment variable method in this post is configured when an instance is started and is never updated.

_All code © Google w/ Apache 2 license_