You can use the Secret block to store secret strings.
Copy
from prefect.blocks.system import Secretsecret = Secret(value="Marvin's surprise birthday party is on the 15th")secret.save("surprise-party")
You can access the secret value in your workflow by loading a Secret block with the same name.
Copy
from prefect import flow from prefect.blocks.system import Secret@flowdef my_flow(): secret = Secret.load("surprise-party") print(secret.get())
Secret values are encrypted at rest when stored in your Prefect backend.To update a secret, save the new value with the overwrite parameter set to True.
Copy
from prefect.blocks.system import Secretsecret = Secret(value="Marvin's surprise birthday party is actually on the 16th")secret.save("surprise-party", overwrite=True)
Secret blocks can also be created and updated through the Prefect UI.
Prefect offers a set of blocks for storing common configuration.Many of these blocks are included as part of Prefect’s integration libraries.For example, use the AwsCredentials block to store AWS keys to connect to AWS services in your workflows.
You can install prefect-aws by running pip install prefect-aws.
Copy
from prefect_aws import AwsCredentialsaws_credentials = AwsCredentials( aws_access_key_id="my-access-key-id", aws_secret_access_key="nice-try-you-are-not-getting-my-key-friend")aws_credentials.save("aws-credentials")
You can load the block to use the credentials in your workflow.
Copy
from prefect_aws import AwsCredentialsimport boto3# Load AWS credentials from Prefectcreds = AwsCredentials.load("aws-credentials")# Create boto3 session using the credentialssession = boto3.Session( aws_access_key_id=creds.aws_access_key_id, aws_secret_access_key=creds.aws_secret_access_key)# Create S3 clients3_client = session.client('s3')# List all bucketsresponse = s3_client.list_buckets()for bucket in response['Buckets']: print(f"Bucket name: {bucket['Name']}")