Matthew Krausse
04/14/2024, 5:11 PMMarvin
04/14/2024, 5:12 PMprefect
and prefect-gcp
with cloud storage support:
bash
pip install prefect prefect-gcp[cloud_storage]
2. Create a Dockerfile with the necessary installations:
Dockerfile
FROM prefecthq/prefect:2-python3.11
RUN pip install "prefect-gcp[cloud_storage]"
Build and push it to Google Artifact Registry:
bash
gcloud artifacts repositories create my-repository --repository-format=docker --location=us
gcloud auth configure-docker us-docker.pkg.dev
docker build -t us-docker.pkg.dev/${GCP_PROJECT_ID}/my-repository/prefect-gcp:2-python3.11 .
docker push us-docker.pkg.dev/${GCP_PROJECT_ID}/my-repository/prefect-gcp:2-python3.11
### Use Prefect to Create Infrastructure as Code
3. Set your Prefect Cloud Run and GCS Bucket environment variables:
bash
export CREDENTIALS_BLOCK_NAME="BLOCK-NAME-PLACEHOLDER"
export CLOUD_RUN_JOB_BLOCK_NAME="cloud-run-job-example"
export CLOUD_RUN_JOB_REGION="us-central1"
export GCS_BUCKET_BLOCK_NAME="cloud-run-job-bucket-example"
export GCP_PROJECT_ID=$(gcloud config get-value project)
4. Initialize a Docker Deployment Recipe:
bash
prefect init --recipe docker \
--field image_name=my-repo/my-image \
--field tag=my-tag
5. Build Infrastructure:
- Add build_docker_image
to the build
section in your deployment specification file prefect.yaml
.
- Configure to build and push the Docker image and to use {{ image }}
in the prefect.yaml
.
6. Define the infrastructure and storage blocks in Prefect to refer to your Google Cloud Run service and Google Cloud Storage.
### Deploy to Cloud Run
7. Use Prefect CLI commands to create deployments and attach them to the previously configured Cloud Run and GCS blocks.
8. Ensure that the correct permissions and environment variables are set to allow your Prefect flows to access the necessary GCP resources.
Source links and reading for detailed information and commands:
- Prefect 2.0 Docker Deployments Guide
- Google Cloud Storage and Google Cloud Run Guide
- Prefect Deployment Storage Guide