Search code examples
pythonflaskpulumi

How do I run a Flask API that has Pulumi code?


I’m new to Pulumi and a newbie. I want to write a Flask API that provisions Scaleway instances using Pulumi. I might be doing this completely wrong, but I'm thinking it will work as a regular api endpoint that will trigger the Pulumi code.

main.py :

import pulumi
import pulumi_scaleway as scaleway
import flask

app = flask.Flask(__name__)
app.config["DEBUG"] = True

@app.route('/', methods=['GET'])
def home():
    return "<h1>Hello</p>"

@app.route('/create', methods=['GET'])
def create():

    public_ip = scaleway.InstanceIp("example")

    server = scaleway.InstanceServer("example",
                                     image="ubuntu_focal",
                                     type="DEV1-S",
                                     ip_id=public_ip.id,
                                     tags=["python"]
                                     )

if __name__ == '__main__':
    app.run()

When I do pulumi up I get :

/home/.../venv/bin/python: No module named

error: an unhandled error occurred: Program exited with non-zero exit code: 1

I don't really understand what the error means here, does someone know how I should go about this?

edit1: Figured out what I was doing wrong thanks to the [pulumi/automation-api-examples][1] repo. I renamed __ main__.py to app.py and did FLASK_RUN_PORT=1337 FLASK_ENV=development venv/bin/flask run

edit2: The solution above doesn't work after I deployed the API in a container. And now I get a Program run without the Pulumi engine available; re-run using the pulumi CLI error


Solution

  • Figured out what I was doing wrong thanks to the pulumi/automation-api-examples repo. I renamed __ main__.py to app.py and did FLASK_RUN_PORT=1337 FLASK_ENV=development venv/bin/flask run