Modern Cloud Workflow with Pebl - Part 1
Tags: devops, cloud, infrastructure, pebl
Published: 2023-04-21 22:28:18

In part 1, we are going to get our setup and get started with a simple serverless deployment with [Flask](https://flask.palletsprojects.com/en/2.2.x/).

## Setup

1. Make sure that you have the latest [docker](https://www.docker.com/) installed.
    
2. Install the pebl cli by following these steps: https://docs.pebl.io/setup
    
3. Head over to [pebl.io](https://www.pebl.io) and create a free account
    

Make sure to claim your free `*.pebl.rocks` subdomain (https://www.pebl.io/signup/claim), as we'll be using this for the rest of the series! We will be using `hey.pebl.rocks` to make things concrete, so make sure to replace any instances of that with your own subdomain.

## Project Creation

1. Now it's time to create our project! Feel free to create a folder to fit your own workflow. For this tutorial we'll be creating `pebl-tutorial`:
    
    ```bash
    mkdir ~/work/pebl-tutorial
    ```
    
2. Inside the project, create a `Dockerfile` with this content:
    
    ```bash
    FROM peblcloud/python:0.0.8
    COPY main.py .
    RUN pip install Flask
    ENTRYPOINT python -u main.py
    ```
    
3. Create a `main.py` that's going to contain our main application. For now let's put a super simple hello world example just to get started:
    
    ```python
    print("hello world from pebl!")
    ```
    

## Local Runtime

Now we can start our local pebl runtime to execute our project locally. Use the pebl CLI:

```bash
$ pebl up
 >> initializing new pebl cluster...
 >> restoring any previous state
 >> starting cluster state restore
 >> finished cluster state restore
 >> done!
 >> run `pebl info` to see logs, endpoints, and other info about the running cluster!
```

And as the output says, we can use `pebl info` command to inspect the running cluster, see logs from running workloads, and more. The info screen should look something like this:

```bash

  pebl cluster version: 0.0.8
  ctrl-C or q to exit the info pane
  (exiting will not stop the cluster)


══ logs ═════════════════════════════
```

And you can exit the info pane by pressing `q` or `Ctrl-C`.

### Running the Project

Now back at the root of the project folder, let's run the project! Simply execute `pebl run`:

```bash
$ pebl run
 >> Building...
 >> starting container...
```

And now if you check your info pane, you should see the "hello world from pebl!" in the logs:

```bash

  pebl cluster version: 0.0.8
  ctrl-C or q to exit the info pane
  (exiting will not stop the cluster)


══ logs ═════════════════════════════
[4c368c] hello world from pebl!
```

## Adding a Server

Now to make it a bit more interesting!

Let's add a Flask application, and use pebl to create a serverless endpoint. Update `main.py` to this:

```python
import pebl
from flask import Flask

app = Flask(__name__)

@app.route("/")
def root():
  return "Hello!"

pebl.service(app, "hey.pebl.rocks")
```

This is a basic Flask configuration, with a single route at `/`. The crucial line that ties all this together is the last line, where we use the [pebl sdk](https://docs.pebl.io/reference/SDK) to invoke `pebl.service`.

Run this with `pebl run` at the root of the project just like before. Now the info pane will list the new endpoint resource, in the resources section which sits above the logs.

```bash

  pebl cluster version: 0.0.8
  ctrl-C or q to exit the info pane
  (exiting will not stop the cluster)

      endpoint:hey.pebl.rocks → localhost:32785

══ logs ═══════════════════════════════════════
```

You can try sending it a request! You can use a browser and point it to the listed port, or you can use curl (make sure to use the actual port that's listed on your info pane):

```bash
$ curl localhost:32785
Hello!
```

## Cloud Runtime

One key feature of pebl is its ability to infer the correct infrastructure configuration directly from your code. This has a profound effect on changing the way you code and interact with common cloud concepts. Specifically, this means that there's no longer a need to keep track of local vs. production environments in code:

```python
import os

if os.getenv("PRODUCTION"):
  return PRODUCTION_CONFIGS
else:
  return LOCAL_CONFIGS
```

Nor do you have to have some json or yaml configuration file that details the cloud infrastructure manually. Instead think of your code as a description of your infrastructure! So when we want to deploy our code to the cloud, we don't have to do anything special. The code will run in the cloud runtime without any modification as it did on our local runtime.

Use the `pebl deploy` command at the root of the project folder:

```bash
$ pebl deploy
```

Then make sure it's live by sending a curl request, making sure to use `https` and substituting your own `*.pebl.rocks` subdomain.

```bash
$ curl https://hey.pebl.rocks
Hello!
```

This highlights the power of pebl — it forces you to encode your infrastructure details into your code, and with a single `pebl.service` method it configures DNS updates, TLS certs, load balancing, and more on your behalf.

## Next in Part 2

A server without any persistence is quite limited in what it can do. In part 2 we will cover adding [redis](https://redis.io/) to our application, and how to iterate with a fast local workflow that's not possible with other platforms.