Joshua Caskie
08/06/2024, 5:40 PMsqlalchemy-connector
Block?Marvin
08/06/2024, 5:41 PMsqlalchemy-connector
Block.
You can create and manage Prefect blocks in Python directly using prefect_sqlalchemy.SqlAlchemyConnector
. An example of using this in a workflow is below. You may need to adapt this according to your Terraform workflow configurations:
python
from prefect import flow, task
from prefect_sqlalchemy import SqlAlchemyConnector
@task
def setup_table(block_name: str) -> None:
with SqlAlchemyConnector.load(block_name) as connector:
connector.execute(
"CREATE TABLE IF NOT EXISTS customers (name varchar, address varchar);"
)
connector.execute(
"INSERT INTO customers (name, address) VALUES (:name, :address);",
parameters={"name": "Marvin", "address": "Highway 42"},
)
connector.execute_many(
"INSERT INTO customers (name, address) VALUES (:name, :address);",
seq_of_parameters=[
{"name": "Ford", "address": "Highway 42"},
{"name": "Unknown", "address": "Highway 42"},
],
)
@flow
def my_workflow():
setup_table("BLOCK_NAME")
if __name__ == "__main__":
my_workflow()
For more detailed information, you can reference the Prefect documentation here.
For up-to-date versions of Prefect, consider turning to Prefect Slack Community or the official Prefect Discourse for community support and Terraform-specific custom implementations.