如何将 Pulumi 的 Output<T> 传递给 ecs 中任务的容器定义?
How to pass Pulumi's Output<T> to the container definition of a task within ecs?
任务定义中的 containerDefinition 需要作为单个有效 JSON 文档提供。我正在创建一个应该处理动态数据的通用 ECS 服务。这是代码:
genericClientService(environment: string, targetGroupArn: Output<string>) {
return new aws.ecs.Service(`${this.domainName}-client-service-${environment}`, {
cluster: this.clientCluster.id,
taskDefinition: new aws.ecs.TaskDefinition(`${this.domainName}-client-${environment}`, {
family: `${this.domainName}-client-${environment}`,
containerDefinitions: JSON.stringify(
clientTemplate(
this.defaultRegion,
this.domainName,
this.taskEnvVars?.filter((object: { ENVIRONMENT: string }) => object.ENVIRONMENT === environment),
this.ecrRepositories
)
),
cpu: "256",
executionRoleArn: taskDefinitionRole.arn,
memory: "512",
networkMode: "awsvpc",
requiresCompatibilities: ["FARGATE"],
}).arn,
desiredCount: 1,
...
需要来自已构建资源 this.ecrRepositories 的信息,它表示所需的 ECR 存储库列表。这里的问题是假设您要检索存储库 URL 并应用必要的 'apply()' 方法,它将 return 一个 Output。这通常没问题,但由于 containerDefinitions 需要是一个有效的 JSON 文档,Pulumi 无法处理它,因为 Output 上的 JSON 是不支持;
Calling [toJSON] on an [Output<T>] is not supported. To get the value of an Output as a JSON value or JSON string consider either: 1: o.apply(v => v.toJSON()) 2: o.apply(v => JSON.stringify(v)) See https://pulumi.io/help/outputs for more details. This function may throw in a future version of @pulumi/pulumi.
Blockquote
由于动态传递的变量包含在 toJSON 函数回调中,因此上述建议的考虑因素均无效。因此,您如何传递资源信息并不重要,因为它始终是 Output。
有没有办法处理这个问题?
假设 clientTemplate
工作正常并且错误发生在您分享的代码段中,您应该能够通过
解决它
containerDefinitions: pulumi.all(
clientTemplate(
this.defaultRegion,
this.domainName,
this.taskEnvVars?.filter((object: { ENVIRONMENT: string }) => object.ENVIRONMENT === environment),
this.ecrRepositories
)).apply(JSON.stringify),
任务定义中的 containerDefinition 需要作为单个有效 JSON 文档提供。我正在创建一个应该处理动态数据的通用 ECS 服务。这是代码:
genericClientService(environment: string, targetGroupArn: Output<string>) {
return new aws.ecs.Service(`${this.domainName}-client-service-${environment}`, {
cluster: this.clientCluster.id,
taskDefinition: new aws.ecs.TaskDefinition(`${this.domainName}-client-${environment}`, {
family: `${this.domainName}-client-${environment}`,
containerDefinitions: JSON.stringify(
clientTemplate(
this.defaultRegion,
this.domainName,
this.taskEnvVars?.filter((object: { ENVIRONMENT: string }) => object.ENVIRONMENT === environment),
this.ecrRepositories
)
),
cpu: "256",
executionRoleArn: taskDefinitionRole.arn,
memory: "512",
networkMode: "awsvpc",
requiresCompatibilities: ["FARGATE"],
}).arn,
desiredCount: 1,
...
需要来自已构建资源 this.ecrRepositories 的信息,它表示所需的 ECR 存储库列表。这里的问题是假设您要检索存储库 URL 并应用必要的 'apply()' 方法,它将 return 一个 Output
Calling [toJSON] on an [Output<T>] is not supported. To get the value of an Output as a JSON value or JSON string consider either: 1: o.apply(v => v.toJSON()) 2: o.apply(v => JSON.stringify(v)) See https://pulumi.io/help/outputs for more details. This function may throw in a future version of @pulumi/pulumi. Blockquote
由于动态传递的变量包含在 toJSON 函数回调中,因此上述建议的考虑因素均无效。因此,您如何传递资源信息并不重要,因为它始终是 Output
有没有办法处理这个问题?
假设 clientTemplate
工作正常并且错误发生在您分享的代码段中,您应该能够通过
containerDefinitions: pulumi.all(
clientTemplate(
this.defaultRegion,
this.domainName,
this.taskEnvVars?.filter((object: { ENVIRONMENT: string }) => object.ENVIRONMENT === environment),
this.ecrRepositories
)).apply(JSON.stringify),