<@ULVA73B9P> how does one inject the secret from a...
# ask-marvin
h
@Marvin how does one inject the secret from a secret block into the environment via prefect.yaml? i'm using:
Copy code
env:
      DB_PASS: "{{ prefect.blocks.gcpsecret.postgres-admin-db-password }}"
but that gives me the whole secret block, private key credentials and all
m
thought for 374.6 seconds
You’re seeing the entire block because your template is resolving to the whole block document, not just the secret string. If you’re using a Prefect Secret block (prefect.blocks.system.Secret), reference its value field explicitly:
Copy code
# 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:
Copy 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.
h
@Marvin i... already shared that. i'm using a gcpsecret and my snippet is:
Copy code
env:
      DB_PASS: "{{ prefect.blocks.gcpsecret.postgres-admin-db-password }}"
m
thought for 31.6 seconds
Thanks for the clarification — since you’re using a GcpSecret block, what you’re seeing is expected. In Prefect 3.x, templating in prefect.yaml can only serialize block fields; it cannot call methods to fetch remote values. So: -
{{ 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
Copy code
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)
Copy 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 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.