https://pulumi.com logo
#general
Title
# general
m

melodic-family-5784

11/14/2019, 7:48 PM
I am new to Pulumi and I am trying to figure out how to convert
Output<string>
to a
string
in TypeScript. My code looks something like:
Copy code
const config = new pulumi.Config();
const ddAPIKey = config.requireSecret("my-api-key");

const envVars = [
    { name: 'ECS_FARGATE', value: 'true' },
    { name: 'DD_API_KEY', value: ddAPIKey.apply(dd => dd) }
];
I am seeing the error:
Copy code
Type 'Output<string>' is not assignable to type 'string'
a

acceptable-army-69872

11/14/2019, 7:51 PM
Normally it resolves Outputstring to string in cases like that. Did you try
Copy code
{ name: 'DD_API_KEY', value: ddAPIKey }
m

melodic-family-5784

11/14/2019, 7:51 PM
Yup.
Same error.
I thought that would work as well given as that's what I saw in the example.
a

acceptable-army-69872

11/14/2019, 7:54 PM
I still have trouble with the whole apply thing. It's still kind of wonky in my brain. Hopefully an official pulumi person will show up and be able to help you.
m

melodic-family-5784

11/14/2019, 7:55 PM
Thanks for trying!
a

acceptable-army-69872

11/14/2019, 7:56 PM
I def have overthought resolving vars before, and later the fix has been to not try to do the applies.
t

tall-librarian-49374

11/14/2019, 8:06 PM
Basically, you can't convert
Output<T>
to
T
In case of secrets, it's
Output
that ensures the secretness
Your code seems fine though. Where do you get the error?
m

melodic-family-5784

11/14/2019, 8:24 PM
@tall-librarian-49374 I see the error when the code is transpiled.
I thought that I would be able to use
Output<string>
without needing to use
apply
as well.
Just as an exercise, I removed the encrypted version of my API key from the config and then put an unencrypted version of the API key into the config.
I then changed:
Copy code
const ddAPIKey = config.requireSecret("my-api-key");
to
Copy code
const ddAPIKey = config.require("my-api-key");
and the error went away.
t

tall-librarian-49374

11/14/2019, 8:34 PM
yes, apply is not needed
m

melodic-family-5784

11/14/2019, 8:34 PM
The one difference being that the type of
ddAPIKey
changed from
pulumi.Output<string>
to
string
t

tall-librarian-49374

11/14/2019, 8:36 PM
I see the error when the code is transpiled.
again, your code snippet compiles fine. do you get an error from some further lines?
m

melodic-family-5784

11/14/2019, 8:36 PM
Sorry! Yes.
Copy code
const ddContainerDefinition: aws.ecs.ContainerDefinition = {
        name: 'datadog-statsd',
        cpu: 102,
        memory: 256,
        environment: envVars,
        //environment: ddAPIKey.apply(dd => [
        //      { name: 'ECS_FARGATE', value: 'true' },
        //      { name: 'DD_API_KEY', value: dd }
        //]),
        image: 'datadog/agent:latest',
        portMappings: [{ containerPort: 8125, protocol: 'udp' }]
    };
    return ddContainerDefinition;
}
The error is coming form the assignment of the environment property of the ContainerDefinition that I am trying to create.
t

tall-librarian-49374

11/14/2019, 8:38 PM
Now I see the problem...
m

melodic-family-5784

11/14/2019, 8:39 PM
I didn't want to make it too easy for you!
t

tall-librarian-49374

11/14/2019, 8:42 PM
You'll have to keep using apply along the way, e.g.
Copy code
const ddContainerDefinition =
  ddAPIKey.apply(apiKey => <aws.ecs.ContainerDefinition>{
    name: 'datadog-statsd',
    cpu: 102,
    memory: 256,
    environment: [
      { name: "ECS_FARGATE", value: "true" },
      { name: "DD_API_KEY", value: apiKey },
  ],
    image: 'datadog/agent:latest',
    portMappings: [{ containerPort: 8125, protocol: 'udp' }]
  });
I'm not sure what your other program is doing, maybe there could be more elegant ways
E.g., base on
return
I'm guessing it's a function. You could add
apiKey
as the function parameter and then requireSecret and apply would move outside the function
Anyway, once you have a secret, you are stuck with applies for non-trivial transformations
m

melodic-family-5784

11/14/2019, 8:49 PM
OK, thanks for your help. I’ll give it a try. I’m basically trying to add a data dog sidecar to an ECS/Fargate deployment.
And I didn’t want to expose the API key in code.
I think I get the gist of things now.