我正在撰寫一個使用 Pulumi 自動化 API 的 Flask 應用程式。我正在關注自動化 API 專案示例。但是當我發送一個 POST 請求時,我得到了一個沒有 Pulumi 引擎可用的程式運行;使用 pulumi CLI錯誤重新運行。自動化 API 不應該自己運行 CLI 嗎?
Pulumi CLI 可用:
普魯米版 v3.24.1
編輯:我遵循了 pulumi over HTTP 示例,這是我的 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)
uj5u.com熱心網友回復:
我發現了問題,這是因為我在 create_stack 中將引數傳遞給程式函式
stack = automation.create_stack(stack_name=stack_name, project_name=project_name, program=create_pulumi_program(content))
它應該是這樣的:
stack = automation.create_stack(stack_name=stack_name, project_name=project_name, program=create_pulumi_program)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/432455.html
