Home > front end >  Pulumi Automation API doesn't run the Pulumi CLI?
Pulumi Automation API doesn't run the Pulumi CLI?

Time:02-24

I'm writing a Flask app that uses Pulumi Automation API. I'm following the Automation API project examples. But when I send a POST request I get a Program run without the Pulumi engine available; re-run using the pulumi CLI error. Isn't the Automation API supposed to run the CLI on its own?

The Pulumi CLI is available:

pulumi version v3.24.1

edit: I followed the pulumi over HTTP example, here is my app.py

import pulumi
from flask import Flask, request, make_response, jsonify
from pulumi import automation as auto
import os
from pulumi_aws import s3

app = Flask(__name__)

# This function defines our pulumi s3 static website in terms of the content that the caller passes in.
# This allows us to dynamically deploy websites based on user defined values from the POST body.
def create_pulumi_program(content: str):
    # Create a bucket and expose a website index document

    site_bucket = s3.Bucket("s3-website-bucket", website=s3.BucketWebsiteArgs(index_document="index.html"))
    index_content = content

    # Write our index.html into the site bucket
    s3.BucketObject("index",
                    bucket=site_bucket.id,
                    content=index_content,
                    key="index.html",
                    content_type="text/html; charset=utf-8")

    # Set the access policy for the bucket so all objects are readable
    s3.BucketPolicy("bucket-policy",
                    bucket=site_bucket.id,
                    policy={
                        "Version": "2012-10-17",
                        "Statement": {
                            "Effect": "Allow",
                            "Principal": "*",
                            "Action": ["s3:GetObject"],
                            # Policy refers to bucket explicitly
                            "Resource": [pulumi.Output.concat("arn:aws:s3:::", site_bucket.id, "/*")]
                        },
                    })

    # Export the website URL
    pulumi.export("website_url", site_bucket.website_endpoint)

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


@app.route('/v1/code', methods=['POST'])
def create_handler():
    content = request.get_json()
    project_name = content.get('project_name')
    stack_name = content.get('stack_name')
    pulumi_access_token = request.headers['pulumi_access_token']
    os.environ['PULUMI_ACCESS_TOKEN'] = pulumi_access_token

    try:
        def pulumi_program():
            return create_pulumi_program(content)

        stack = auto.create_stack(stack_name=stack_name,
                                  project_name=project_name,
                                  program=create_pulumi_program(content))
        stack.workspace.install_plugin("aws", "v4.0.0")
        stack.set_config("aws:region", auto.ConfigValue(value="us-west-2"))
        stack.set_config("aws:region", auto.ConfigValue("us-west-2"))
        # deploy the stack, tailing the logs to stdout
        up_res = stack.up(on_output=print)
        return jsonify(id=stack_name, url=up_res.outputs['website_url'].value)
    except auto.StackAlreadyExistsError:
        return make_response(f"stack '{stack_name}' already exists", 409)
    except Exception as exn:
        return make_response(str(exn), 500)

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

CodePudding user response:

I found the issue, it was because I was passing a parameter to the program function in create_stack

stack = automation.create_stack(stack_name=stack_name, project_name=project_name, program=create_pulumi_program(content))

It should be instead like this:

stack = automation.create_stack(stack_name=stack_name, project_name=project_name, program=create_pulumi_program)
  • Related