A demo of how I ended up loading in secrets into terraform from google cloud secrets manger
I am using terraform to manage my Cloud Run deployments but the Cloud Run deployments depend on secrets. Therefore Secret Manager secrets have to exist before terraform apply
is run otherwise Cloud Run
services fail to state
All the tutorials online use hardcoded secrets which defeat the purpose of using a secrets manger. Following John Hanley comment from https://stackoverflow.com/questions/76149258 the solution I settled on is to have secrets separate from terraform with terraform only injecting google_secret_manager_secret
so it does not even know the values and will not keep values in state.
I am also choosing to prefill my secrets with random values if they do not exist. This allows me to start up the cloud run service, although the service will not correctly boot up due to missing secrets, I know my infrastructure is set up.
Afterwards i can set the secrets to real values and restart the cloud run service and all is good.
- uses GCP bucket for tf state (this is for future profin and sharing, since tf only store
google_secret_manager_secret
and notgoogle_secret_manager_secret_version
, tf does not track secrets values so encrypted sate is not needed ) - secrets are bootstrapped with a bash script with random values if no secrete value is present
- secrets are imported into terraform
- terraform creates the infrastructure to run a cloud run service with the secrets
# update SECRETS in gcloud.init.sh and in `main.tf` for different secrets
> ./gcloud.init.sh -p [GCLOUD_PROJECT_ID]
# will create state.tf and terraform.tfvars
# will create state bucket
# will seed the secrets
# will run terraform init, need to import afterwards
# will import the secrets into terraform
> terraform apply
gcloud.init.sh
: bash script to bootstrap the project with secretsmain.tf
: tf local and providerproject.tf
: service enabling and service account for cloud runsecrets.tf
: definesgoogle_secret_manager_secret
resources that are imported bygcloud.init.sh
and used by cloud run serviceservice.tf
: defines the cloud run servicevariables.tf
: variable validationstate.tf.template
: a template used bygcloud.init.sh
to generate astate.tf
file. Am not allowed to reverence variables instate.tf
so my bash script create it using the project id that is passed into the bash scriptstate.tf
: agcloud.init.sh
generate state fileterraform.tfvars
: variables,project
andregion
ar added in bygcloud.init.sh
- secret keys are duplicated in two places, in
gcloud.init.sh
and inmain.locals
- terraform will destroy imported secrets on
terraform destroy
so it can clean up everything, be careful - terraform cloud run does not clean up a starts cloud service on error you will have to manually delete it if you need to run
terraform apply
after an error
Error creating Service: googleapi: Error 409: Resource 'my-service' already exists