Adeel Shakir
08/02/2024, 6:20 AMMarvin
08/02/2024, 6:21 AMsh
ssh -i path-to-your-key.pem ec2-user@your-ec2-public-dns
- Install Dependencies (Git, Python, etc.):
sh
sudo yum update -y
sudo yum install git -y
sudo yum install python3 -y
- Clone Your Repo:
sh
git clone <https://github.com/your-username/your-repo-name.git>
cd your-repo-name
- Setup Virtual Environment:
sh
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
2. Configure Prefect to Run Flows from the Repo:
- Install Prefect (if not installed):
sh
pip install prefect
- Write a Flow Script in your cloned repo (or use an existing one):
python
from prefect import flow
@flow
def my_flow():
print("Hello from EC2!")
if __name__ == "__main__":
my_flow()
- Run the Flow:
sh
prefect run -p path/to/your/flow_script.py
3. Deploying and Running Flows with Prefect:
To ensure Prefect takes flows from your EC2-hosted repo, you will need to set up a Prefect worker on EC2. Let's search for some steps on deploying workers and running flows.
Let's think step-by-step. I'll search for relevant Prefect documentation and GitHub issues related to deploying workers and running flows from specific repositories. Give me a moment.
Here's a structured guide on setting up and deploying Prefect flows from your GitHub repository on an EC2 instance:
### 1. Setup Your EC2 Instance
- Launch an EC2 Instance: Choose an appropriate AMI and instance type.
- SSH into Your Instance:
sh
ssh -i path-to-your-key.pem ec2-user@your-ec2-public-dns
- Install Dependencies:
sh
sudo yum update -y
sudo yum install git -y
sudo yum install python3 -y
- Clone Your Repository:
sh
git clone <https://github.com/your-username/your-repo-name.git>
cd your-repo-name
- Setup Virtual Environment:
sh
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install prefect
### 2. Configuring Prefect
- Write a Flow Script in your repo:
python
from prefect import flow
@flow
def my_flow():
print("Hello from EC2!")
if __name__ == "__main__":
my_flow()
- Run the Flow:
sh
prefect run -p path/to/your/flow_script.py
### 3. Set Up Prefect Worker on EC2
To run flows from your repository using a Prefect worker, follow these steps:
#### Step 1: Install Docker
Ensure Docker is installed and running on your EC2 instance:
sh
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
Logout and log back in for group changes to take effect.
#### Step 2: Create a Prefect Worker
You can use ECS or a simple Docker setup on your EC2 instance. Here's an example of setting up a worker:
1. ECS Worker Service:
- Create a JSON file (e.g., ecs-worker.json
) with the following contents:
```json
{
"family": "prefect-worker-task",
"networkMode": "awsvpc",
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024",
"executionRoleArn": "<ecs-task-role-arn>",
"taskRoleArn": "<ecs-task-role-arn>",
"containerDefinitions": [
{
"name": "prefect-worker",
"image": "prefecthq/prefect:2-latest",
"cpu": 512,
"memory": 1024,
"essential": true,
"command": [
"/bin/sh",
"-c",
"pip install prefect-aws && prefect worker start"
],
"environment": [Marvin
08/02/2024, 6:21 AM2. **Using Prefect CLI**:
```python
from prefect import flow
from prefect.deployments import DeploymentSpec
DeploymentSpec(
flow_location="path/to/your/flow_script.py",
name="my-first-deployment",
work_pool_name="my-ec2-pool"
)
For Detailed Steps:
AWS ECS Worker Guide
GitHub Flow Deployment
#### Step 3: Deploy and Run
- Run Deployment:
sh
prefect deployment apply path/to/deployment.yaml
prefect deployment run "my-first-deployment"
### Resources:
- Prefect ECS Worker Guide
- Prefect Work Pools Tutorial
- Prefect Flows Tutorial