我正在处理创建 AWS API 网关。我正在尝试创建 CloudWatch Log 组并将其命名为API-Gateway-Execution-Logs_${restApiId}/${stageName}。我在 Rest API 创建方面没有问题。
我的问题是将类型为 pulumi.Outout 的 restApi.id 转换为字符串。
我已经尝试了在他们的PR#2496中提出的这两个版本
const restApiId = apiGatewayToSqsQueueRestApi.id.apply((v) => `${v}`);const restApiId = pulumi.interpolate `${apiGatewayToSqsQueueRestApi.id}` 这是使用它的代码
const cloudWatchLogGroup = new aws.cloudwatch.LogGroup(
`API-Gateway-Execution-Logs_${restApiId}/${stageName}`,
{},
);
Run Code Online (Sandbox Code Playgroud)
stageName 只是一个字符串。
我也试过apply再次喜欢
const restApiIdStrign = restApiId.apply((v) => v);
我总是从 pulumi up
aws:cloudwatch:LogGroup API-Gateway-Execution-Logs_Calling [toString] on an [Output<T>] is not supported.
请帮我将输出转换为字符串
Mik*_*kov 14
@Cameron 回答了命名问题,我想在标题中回答您的问题。
不可能将 an 转换Output<string>为string或任何Output<T>为T。
Output<T>是未来值的容器,T即使在程序执行结束后也可能无法解析。也许,您restApiId是在部署时由 AWS 生成的,因此如果您在预览中运行您的程序,则restApiId.
Output<T>就像一个Promise<T>最终会解决的问题,可能是在云中创建了一些资源之后。
因此,唯一的操作Output<T>是:
Output<U>与apply(f),其中f:T -> UInput<T>以将其传递给另一个资源构造函数任何值操作都必须在apply调用中发生。
只要 Pulumi 脚本仍在运行时输出是可解析的,您就可以使用如下方法:
import {Output} from "@pulumi/pulumi";
import * as fs from "fs";
// create a GCP registry
const registry = new gcp.container.Registry("my-registry");
const registryUrl = registry.id.apply(_=>gcp.container.getRegistryRepository().then(reg=>reg.repositoryUrl));
// create a GCP storage bucket
const bucket = new gcp.storage.Bucket("my-bucket");
const bucketURL = bucket.url;
function GetValue<T>(output: Output<T>) {
return new Promise<T>((resolve, reject)=>{
output.apply(value=>{
resolve(value);
});
});
}
(async()=>{
fs.writeFileSync("./PulumiOutput_Public.json", JSON.stringify({
registryURL: await GetValue(registryUrl),
bucketURL: await GetValue(bucketURL),
}, null, "\t"));
})();
Run Code Online (Sandbox Code Playgroud)
需要澄清的是,这种方法仅在您进行实际部署(即pulumi up)时才有效,而不仅仅是预览。(正如这里所解释的)
不过,这对于我的用例来说已经足够了,因为我只想要一种在每次部署后存储注册表 url 等的方法,以便项目中的其他脚本知道在哪里可以找到最新版本。
小智 4
LogGroup您可以通过指定输入来指定物理名称name,并且可以id使用 API Gateway 输出来构造此名称pulumi.interpolate。您必须使用静态字符串作为资源的第一个参数。我建议使用您为 API Gateway 资源提供的相同名称作为日志组的名称。这是一个例子:
const apiGatewayToSqsQueueRestApi = new aws.apigateway.RestApi("API-Gateway-Execution");
const cloudWatchLogGroup = new aws.cloudwatch.LogGroup(
"API-Gateway-Execution", // this is the logical name and must be a static string
{
name: pulumi.interpolate`API-Gateway-Execution-Logs_${apiGatewayToSqsQueueRestApi.id}/${stageName}` // this the physical name and can be constructed from other resource outputs
},
);
Run Code Online (Sandbox Code Playgroud)
Pulumi 中每个资源类型的第一个参数是逻辑名称,用于 Pulumi 在内部跟踪从一个部署到下一个部署的资源。默认情况下,Pulumi会从此逻辑名称自动命名物理资源。您可以通过指定您自己的物理名称(通常是通过name资源的输入)来覆盖此行为。有关资源名称和自动命名的更多信息请参见此处。
这里的具体问题是逻辑名称不能从其他资源输出构建。它们必须是静态字符串。资源输入(例如name)可以从其他资源输出构建。
| 归档时间: |
|
| 查看次数: |
4097 次 |
| 最近记录: |