Slackbot
01/29/2024, 7:00 AMMarvin
01/29/2024, 7:00 AMBen Zehavi
01/29/2024, 7:00 AMMarvin
01/29/2024, 7:01 AMBen Zehavi
01/29/2024, 7:02 AMMarvin
01/29/2024, 7:02 AMSecret
block in Prefect using the SecretStr
field type from Pydantic:
python
from typing import Optional
from prefect.blocks.core import Block
from pydantic import SecretStr
class AWSCredentials(Block):
aws_access_key_id: Optional[str] = None
aws_secret_access_key: Optional[SecretStr] = None
aws_session_token: Optional[SecretStr] = None
# Save the secret block with your credentials
my_aws_credentials = AWSCredentials(
aws_access_key_id="your_access_key_id",
aws_secret_access_key=SecretStr("your_secret_access_key"),
aws_session_token=SecretStr("your_session_token")
)
my_aws_credentials.save("my-aws-creds")
# Use the secret block within a flow
from prefect import flow
@flow
def my_flow():
# Load the secret block by name
aws_creds = AWSCredentials.load("my-aws-creds")
access_key_id = aws_creds.aws_access_key_id
secret_access_key = aws_creds.aws_secret_access_key.get_secret_value()
session_token = aws_creds.aws_session_token.get_secret_value()
# Use the AWS credentials in your task or service client setup...
# e.g., boto3 client setup
# client = boto3.client(
# 'your_aws_service',
# aws_access_key_id=access_key_id,
# aws_secret_access_key=secret_access_key,
# aws_session_token=session_token
# )
# Run the flow
my_flow()
Remember to replace the placeholders (your_access_key_id
, your_secret_access_key
, and your_session_token
) with your actual AWS credentials. Also, make sure to handle your credentials securely and avoid hardcoding them in your scripts.
The SecretStr
type ensures that the value is obfuscated when the block is inspected through the UI or logs, adding an extra layer of security. If you need to use the actual value within your flow, you can use the get_secret_value()
method as shown in the example above.