gpt4 book ai didi

docker - Pulumi 自动化 API 不运行 Pulumi CLI?

转载 作者:行者123 更新时间:2023-12-05 03:28:39 28 4
gpt4 key购买 nike

我正在编写一个使用 Pulumi Automation API 的 Flask 应用程序。我正在关注 Automation API project examples .但是当我发送一个 POST 请求时,我得到了一个没有 Pulumi 引擎可用的程序;重新运行使用pulumi CLI报错。 Automation API 不是应该自己运行 CLI 吗?

Pulumi CLI 可用:

pulumi版本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)

最佳答案

我发现了问题,那是因为我在 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
)

关于docker - Pulumi 自动化 API 不运行 Pulumi CLI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71205110/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com