hambone johnston
11/15/2025, 3:36 AMenv:
DB_PASS: "{{ prefect.blocks.gcpsecret.postgres-admin-db-password }}"
but that gives me the whole secret block, private key credentials and allMarvin
11/15/2025, 3:36 AMMarvin
11/15/2025, 3:42 AM# inside prefect.yaml
deployments:
- name: ...
work_pool:
name: ...
job_variables:
env:
DB_PASS: "{{ prefect.blocks.secret.postgres-admin-db-password.value }}"
Notes:
- The block type slug is secret
- The .value attribute resolves to just the secret string
- Avoid referencing the block without .value or you’ll get the full JSON payload
If you’re using the GCP Secret Manager block (GcpSecret from prefect-gcp), templating cannot call methods to fetch the remote secret. Templating can only access block fields, so referencing the GcpSecret block will return its configuration (including credentials), not the secret value. In that case, use one of these patterns instead:
- Store the secret value in a Prefect Secret block and reference {{ prefect.blocks.secret.<name>.value }} in prefect.yaml (recommended for env injection)
- Or, fetch the secret at runtime in your flow code:
from prefect import flow
from prefect_gcp.secret_manager import read_secret
from prefect_gcp.credentials import GcpCredentials
@flow
def my_flow():
db_pass = read_secret(
secret_name="postgres-admin-db-password",
gcp_credentials=GcpCredentials.load("my-gcp-creds"),
version_id="latest",
)
# use db_pass...
Docs for reference:
- prefect.yaml templating
- Secret block (store and reference secrets)
- prefect-gcp integration
If you share which block type you created (Secret vs GcpSecret) and your prefect.yaml snippet, I can tailor the exact template for you.hambone johnston
11/15/2025, 3:48 AMenv:
DB_PASS: "{{ prefect.blocks.gcpsecret.postgres-admin-db-password }}"Marvin
11/15/2025, 3:48 AMMarvin
11/15/2025, 3:48 AM{{ prefect.blocks.gcpsecret.postgres-admin-db-password }} returns the entire block document (including service account info), not the secret’s value from Secret Manager.
- Adding .value won’t help here because the value isn’t a stored string; it’s retrieved via an API call at runtime.
Options that will work:
1) Recommended for env injection: Store the secret string in a Prefect Secret block and template its value
job_variables:
env:
DB_PASS: "{{ prefect.blocks.secret.postgres-admin-db-password.value }}"
2) Fetch from GCP Secret Manager at runtime in your flow (don’t inject via YAML)
from prefect import flow
from prefect_gcp.secret_manager import read_secret
from prefect_gcp.credentials import GcpCredentials
@flow
def my_flow():
db_pass = read_secret(
secret_name="postgres-admin-db-password",
gcp_credentials=GcpCredentials.load("my-gcp-creds"),
version_id="latest",
)
# use db_pass directly to init your DB client
If you must have an environment variable, you can set it in your flow before using it, but note that only the current process (and any children started after setting it) will see the var.
Docs:
- prefect.yaml templating
- Secret block (store and reference secrets)
- prefect-gcp integration
If you’d like, share your worker type (Docker/Kubernetes/Process) and I can suggest a concrete pattern to fetch the GCP secret at container start and export it before your flow runs.