Home > database >  How to pass Pulumi's Output<T> to the container definition of a task within ecs?
How to pass Pulumi's Output<T> to the container definition of a task within ecs?

Time:03-24

A containerDefinition within a Task Definition needs to be provided as a single valid JSON document. I'm creating a generic ECS service that should handle dynamic data. Here is the code:

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,
  ...

There is a need of information from an already built resource this.ecrRepositories which represents a list of ECR repositories needed. The problem here is that let's say you want to retrieve the repository URL and apply the necessary 'apply()' method, it will return an Output<string>. This would be fine normally, but since containerDefinitions needs to be a valid JSON document, Pulumi can't handle it since JSON on an Output<T> is not supported;

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

Neither of the suggested considerations above will work as the dynamicly passed variables are wrapped within a toJSON function callback. Because of this it won't matter how you pass resource information since it will always be an Output<T>.

Is there a way how to deal with this issue?

CodePudding user response:

Assuming clientTemplate works correctly and the error happens in the snippet that you shared, you should be able to solve it with

containerDefinitions: pulumi.all(
    clientTemplate(
        this.defaultRegion,
        this.domainName,
        this.taskEnvVars?.filter((object: { ENVIRONMENT: string }) => object.ENVIRONMENT === environment),
        this.ecrRepositories
  )).apply(JSON.stringify),
  • Related