Prerequisites
Before we begin, you’ll need the following:- Runpod CLI
- Python 3.8 or later
Set up the project environment
In this first step, you’ll set up your project environment using the Runpod CLI. Set your API key in the Runpod CLI configuration file.project create command to create a new directory and files for your project.
Write the code
Next, you’ll write the code to get the IP address of the machine your code is running on. Usehttpbin to retrieve the IP address and test the code locally.
Change directories to the project directory and open the src/handler.py file in your text editor.
httpbin to get the IP address of the machine your code is running on.
Run this code locally to get the IP address of your machine, for example:
Run a development server
Now let’s run the code you’ve written using Runpod’s development server. You’ll start a development server using the Runpod CLIproject dev command.
Runpod provides a development server that allows you to quickly make changes to your code and test these changes in a live environment. You don’t need to rebuild a Docker image or redeploy your code to the Runpod platform just because you made a small change or added a new dependency.
To run a development server, use the Runpod CLI project dev command and select a Network volume.
requirements.txt.
When the Pod is running you should see the following logs:
[lug43rcd07ug47] is your Worker Id. The https://lug43rcd07ug47-8080.proxy.runpod.net is the URL to access your Pod with the 8080 port exposed. You can interact with this URL like you would any other Endpoint.
Interact with your code
In this step, you’ll interact with your code by running acurl command to fetch the IP address from the development server. You’ll learn how to include dependencies in your project and how to use the Runpod API to run your code.
You might have noticed that the function to get an IP address uses a third-party dependency requests. This means by default it’s not included in Python or the Runpod environment.
To include this dependency, you need to add it to the requirements.txt file in the root of your project.
runsync method on the Runpod API to run your code synchronously.
The previous command returns a response:
Deploy your code
Now that you’ve tested your code in the development environment, you’ll deploy it to the Runpod platform using the Runpod CLIproject deploy command. This will make your code available as a Serverless Endpoint.
Stop the development server by pressing Ctrl + C in the terminal.
To deploy your code to the Runpod platform, use the Runpod CLI project deploy command.
You can follow the logs to see the status of your deployment. You may notice that the logs show the Pod being created and then the Endpoint being created.
Interact with your Endpoint
Finally, you’ll interact with your Endpoint by running acurl command to fetch the IP address from the deployed Serverless function. You’ll see how your code runs as expected and tested in the development environment.
When the deployment completes, you can interact with your Endpoint as you would any other Endpoint.
Replace the previous Endpoint URL and specify the new one and add your API key.
Then, run the following command: