Hello! I have a 2.0 question about registering and...
# prefect-community
b
Hello! I have a 2.0 question about registering and using blocks, specifically infrastructure blocks.
1
I have the following code in a file called `block.py`:
Copy code
from prefect.infrastructure.kubernetes import KubernetesJob

kubernetes_job = KubernetesJob(
    env={"HELLO": "WORLD"},
    name="my-custom-kubernetes-job",
    image="prefecthq/prefect:2.0.1-python3.9",
    service_account_name="my-service-account",
    image_pull_policy="IfNotPresent",
)
I am trying to register it as follows:
Copy code
❯ prefect block register -f block.py
Successfully registered 1 block

┏━━━━━━━━━━━━━━━━━━━┓
┃ Registered Blocks ┃
┡━━━━━━━━━━━━━━━━━━━┩
│ Kubernetes Job    │
└───────────────────┘

 To configure the newly registered blocks, go to the Blocks page in the Prefect UI.
However, when I go to the Prefect UI, I don't see the new block. I have checked and I am correctly logged into the expected workspace. Is this expected behavior, or am I doing something wrong? Thanks!
a
registering is for block types. I think you’re trying to save new values to an existing block type
Copy code
from prefect.infrastructure.kubernetes import KubernetesJob

kubernetes_job = KubernetesJob(
    env={"HELLO": "WORLD"},
    name="my-custom-kubernetes-job",
    image="prefecthq/prefect:2.0.1-python3.9",
    service_account_name="my-service-account",
    image_pull_policy="IfNotPresent",
)
kubernetes_job.save("my_kubernetes_job_name")
See: https://orion-docs.prefect.io/concepts/blocks/#saving-blocks
b
Got it, that worked! (although the block name can't contain underscores 😛 ). I was confused because
KubernetesJob
inherits from Block. Looks like no CLI commands needed.
1
a
yup! only if you’re creating a new block type, you need to register through CLI
b
Makes sense. Funny that it reports a success message.
@Andrew Huang Do I have it correct that I cannot attach the
KubernetesJob
directly to a flow, but must first save the block and then reference it in a
deployment build
command?
a
I think
.load()
recreates the base object, so I think theoretically you can use it directly in the flow
e.g.
Copy code
from prefect.blocks.system import JSON

json_block = JSON(value={"the_answer": 42})
json_block.save(name="life-the-universe-everything")
loaded_json_block = json_block.load("life-the-universe-everything")

assert json_block == loaded_json_block
b
Thanks. Will experiment with this. I suspect more changes are coming to the API :)
1
🙌 1