I’m trying to deploy my flow backend to EC2, anyon...
# ask-community
j
I’m trying to deploy my flow backend to EC2, anyone has any resources to guide me?
a
@Joseph Oladokun crosschecking: do you want to use EC2 instance for a local agent together with Prefect Cloud, or do you want to maintain everything yourself and set up Prefect Server on EC2?
j
I’m currently using the local agent with prefect cloud
but with local, i can do scheduling to automate the flows, so i want to create the fow
flows on EC2 to be able to automate my flows
a
So you want to move from a Local agent running on your laptop to a Local agent running on EC2, correct? In that case, you can use pretty much the same setup as you did on your local machine, but ideally you can set a different label to the agent running on EC2 (say, “prod”) to easily distinguish it from the agent running on your laptop. When it comes to deploying an EC2 instance and installing Python and Prefect on it perhaps you could use the EC2 section of this post. Then, instead of ECS shown in this post, you can use a Local agent with Supervisor, as described here - this way, your agent will run as a background process on the EC2 instance.
j
Thank you
🙌 1
l
Hi! Sorry to jump on the thread, but I’m also trying to deploy prefect on EC2, and following the related tutorial you shared @Anna Geller (thank you!). I’m trying to automate the steps of the tutorial in a local prefect run--eg running a ShellTask to log into prefect. That way I can call the ec2_setup.py local flow in my CI/CD pipeline to set up the eks system. My issue is that: • when I run prefect cli commands from a ShellTask for a local flow run directly on the ec2 instance, I get a FAIL signal exit code 127 ◦ in this case kubectl commands successfully execute from ShellTask • when I run prefect cli commands from a ShellTask for a flow run in a poetry environment with prefect installed, the ShellTask runs successfully ◦ however in this case I get FAIL signal exit code 127 for the kubectl commands Running kubectl and prefect manually from the ec2 command line both work, so I can complete the tutorial manually. But I would like the DAG feature of a local run so I can perform some other tasks as well and set dependencies. There may be different approaches for the overall effort, but to boil this down to a simpler question: How can I ensure that ShellTask(command=“<some prefect cli command>“) recognizes the prefect path in a local flow run? (Again, the prefect command executes successfully both directly on the command line and in a ShellTask in a poetry env, but not in a ShellTask directly in the ec2 environment.) (I could also ask how to get kubectl recognized in a poetry env alongside prefect, but I’ll stick with the prefect cli focus for now.)
a
@luke b a short, but perhaps unsatisfactory answer is: if you want to run prefect CLI commands, you need to have prefect installed within this environment. So even doing something like:
Copy code
pip install prefect && prefect version
The same with kubectl: to run it, you would need to install kubectl first (here is install command for Mac, on Linux, checkout this):
Copy code
brew install kubectl && kubectl version --client
However, I didn’t understand what problem would this solve. Can you explain your use case a bit more? One thing that confused me for example is that you reference this EKS article which is all about a fully managed Kubernetes cluster with no EC2 instances that you would have to manage. Why are you setting up an EC2 instance? You could follow this tutorial entirely from your local machine. Also regarding this:
That way I can call the ec2_setup.py local flow in my CI/CD pipeline to set up the eks system
You don’t need to redeploy your Prefect agent every time within your CI/CD. The agent must be configured only once (e.g.
prefect agent kubernetes start --label your-agent-label
), after that you only need to register your flows while making sure that your flow is configured to be picked up by this agent through matching labels on the run configuration. Here is a small example (note that we use the same label for the agent startup command and flow):
Copy code
with Flow(
    "eks-flow",
    storage=set_storage(FLOW_NAME),
    run_config=KubernetesRun(labels=["your-agent-label"]),
) as flow:
    task1() # all your tasks here

flow.register(project="your-project")
Do you want to have a Kubernetes agent or a local agent running on EC2?
l
Hi @Anna Geller, thanks for the answer. I am using a single, small EC2 instance as if it’s my local machine, to follow the tutorial and spin up the EKS cluster from there. I want to be able to spin up and tear down infra in an automated way, so if I want to change the infra my flows run on I can do that with scripts. I use a local flow, with flow.run() (I don’t register it) to execute the prefect cli commands and spin up the EKS infra, and the kubernetes agent to handle subsequent flows as part of the stood up infra. One advantage of this is that I could run a script to run some workloads for a period of time, and then execute another script to shut everything off when I’m finished. One thing that confuses me about running a prefect cli command from the local flow run on EC2 is why doesn’t the flow, when executed, think that prefect is in my local environment? When I run the prefect commands directly from the command line in the same folder, they execute properly--so prefect is installed for cli usage. But if I paste the same command into a task and execute the flow locally, I get the 127 error. Another thing that confuses me is how the kubernetes agent picks up a flow and sends it to the EKS cluster. In the tutorial, I only see this line for installing the agent:
Copy code
prefect agent install kubernetes -t <MyEKS_on_Fargate_K8s_Token> --rbac | kubectl apply -f -
How does the agent it know to send flows to the EKS cluster that was just spun up? Is it because the cluster info is in the kube config? I didn’t see a
prefect agent kubernetes start
command in the tutorial. Would I run this after installing the agent, as above?
a
@luke b there are two ways to spin up an agent: 1. prefect agent <agent-type> start --label your-label 2. prefect agent <agent-type> install #1 will start the agent immediately in the terminal. #2 generates a template for you to install the agent into some environment that will allow this agent process to run as a service, e.g. a Kubernetes service, or a supervisord daemon process. Specifically for Kubernetes, #2 will generate a YAML file that you can apply to the cluster. Example: a) generate YAML
Copy code
prefect agent kubernetes install -k <API_KEY> --rbac >> k8s_agent.yaml
b) apply:
Copy code
kubectl apply -f k8s_agent.yaml
Note that tokens are deprecated (
-t
option in your command). I’d recommend checking out the latest documentation rather than this medium post because many things have changed since then. E.g., you should use API keys now rather than API tokens. Here is KubernetesAgent documentation that will explain it better: https://docs.prefect.io/orchestration/agents/kubernetes.html#requirements
l
Thank you, @Anna Geller! This is very helpful
🙌 1