Hi :wave: I’m working on an open-source Pulumi lib...
# getting-started
i
Hi 👋 I’m working on an open-source Pulumi library called pulumi-aws-django written in TypeScript for deploying web apps (3-tier web apps using Django, Vue, Postgres, hosted on AWS with ECS Fargate). I have been following the Organizing Projects and Stacks article and I have so far organized my library into 2 top-level components: base and app. The base stack includes things like VPC, RDS, ALB, etc, and the app stack mostly includes ECS resources. 👉 I’m having an issue using values from the base stack in my app stack inside of a taskDefinition > containerDefinition > environment variables section. I’ll post details and links in this thread🧵
Here’s the issue I’m having: some of the environment variables that I’m passing to my containers have the following content:
Copy code
Calling [toJSON] on an [Output] 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.
The base stack uses a component that defines an RDS instance, and I export the db address in the following way:
Copy code
const adHocBaseEnv = new AdHocBaseEnvComponent('myAdHocBaseEnv', {
  certificateArn: process.env.CERTIFICATE_ARN,
  domainName: process.env.DOMAIN_NAME
});

export const rdsAddress = adHocBaseEnv.databaseInstance.address;
In the Pulumi console I can see that the RDS address is showing up correctly. 👍
In the app stack I use the rds address (and other values taken from the base stack) in the following way:
Copy code
const org = process.env.PULUMI_ORG;
const environment = process.env.PULUMI_ENVIRONMENT;

const stackReference = new pulumi.StackReference(`${org}/ad-hoc-base/${environment}`)

// ...
const rdsAddress = stackReference.getOutput("rdsAddress") as pulumi.Output<string>;

// ad hoc app env
const adHocAppComponent = new AdHocAppComponent("AdHocAppComponent", {
  // ...
  rdsAddress,
});
In the app stack I define a variable called
envVars
with values that I want to pass to containers in the ECS task definitions like this:
Copy code
const envVars: { [key: string]: pulumi.Output<string> | string }[] = [
      {
        name: "POSTGRES_SERVICE_HOST",
        value: pulumi.interpolate `${props.rdsAddress}`,
      },
      // ...
    ];
I’m not sure if the type I have selected here for
envVars
is correct, but it doesn’t throw any errors in my editor.
I then pass the
envVars
value to components that define taskDefinitions, like this:
Copy code
const apiService = new WebEcsService("ApiWebService", {
      name: "gunicorn",
      command: ["gunicorn", "-t", "1000", "-b", "0.0.0.0:8000", "--log-level", "info", "backend.wsgi"],
      envVars,
      // ...
    });
In this component I define taskDefinitions:
Copy code
ts
    const taskDefinition = new aws.ecs.TaskDefinition(`${props.name}TaskDefinition`, {
      containerDefinitions: JSON.stringify([
        {
          name: props.name,
          image: props.image,
          command: props.command,
          environment: props.envVars,
          // ...
On the component I define
envVars
in the interface like this:
Copy code
envVars?: { [key: string]: pulumi.Input<string> | string }[];
This has been tricky to debug, but I imagine that it is a common pattern in apps that use ECS. Does anyone have pointers for how I can resolve this issue? 🙏
The container is otherwise starting correctly and passing health checks, and the target group is healthy. I guess that since the environments key in the containerDefinition is being `JSON.stringify`ed, it is trying to convert values to json. I have tried to do what it suggested with
.apply(x => x.toJSON())
without any luck
d
This was added in the last release, hope it works for you: https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/#jsonStringify
i
Thanks @dry-keyboard-94795, I will try to use the
jsonStringify
method
I’m not sure how to use the jsonStringify method mentioned above
@dry-keyboard-94795, I got it working using pulumi.jsonStringify instead of JSON.stringify, thank you for the suggestion