I am new to Pulumi and I am trying to figure out h...
# general
m
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
Normally it resolves Outputstring to string in cases like that. Did you try
Copy code
{ name: 'DD_API_KEY', value: ddAPIKey }
m
Yup.
Same error.
I thought that would work as well given as that's what I saw in the example.
a
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
Thanks for trying!
a
I def have overthought resolving vars before, and later the fix has been to not try to do the applies.
t
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
@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
yes, apply is not needed
m
The one difference being that the type of
ddAPIKey
changed from
pulumi.Output<string>
to
string
t
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
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
Now I see the problem...
m
I didn't want to make it too easy for you!
t
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
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.