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

abundant-dawn-11116

01/10/2023, 2:21 AM
Hi Everyone. I have a newbie question that I've been stuck on for longer than I care to admin. Working in a dotnet project, I want to create a bucket and then immediately inject the name of this newly created bucket into an ECS Task Definition. Can someone assist with this? I figured the following would work but it doesn't. I also tried
bucket.Apply(b=>b.BucketName)
and
bucket.BucketName.Apply(v => v)
but neither work. Am I going about this horribly wrong? Thanks in advance.
Copy code
using System.Collections.Generic;
using Pulumi;
using Aws = Pulumi.Aws;

return await Deployment.RunAsync(() => 
{
    var bucket = new Aws.S3.Bucket($"bucket", new()
    {
        Versioning = new BucketVersioningArgs { Enabled = true },
    });

    var test = new Aws.Ecs.TaskDefinition("test", new()
    {
        Family="family",
        ContainerDefinitions = $@"[{{
            ...
            ""environment"": [{{
                ""name"": ""BUCKETNAME"", 
                ""value"": ""{{bucket.BucketName}}""
            }}],
            ...
        }}]"
    });
});
b

billowy-army-68599

01/10/2023, 2:31 AM
you need to use
bucket.BucketName.Apply(name => JSON STRING HERE)
a

abundant-dawn-11116

01/10/2023, 5:33 AM
That doesn't seem to work as expected.. I'm getting something along the lines of this as my value ...
Copy code
"value":"Calling [ToString] on an [Output<T>] is not supported.\n\nTo get the value of an Output<T> as an Output<string> consider:\n1. o.Apply(v => $\"prefix{v}suffix\")\n2. Output.Format($\"prefix{hostname}suffix\");\n\nSee <https://pulumi.io/help/outputs> for more details.\nThis function may throw in a future version of Pulumi."}]
b

billowy-army-68599

01/10/2023, 5:45 AM
please share what you have
a

abundant-dawn-11116

01/10/2023, 6:29 AM
I got it figured out actually.. what I did was something like ...
Copy code
string bucketName;
b

bumpy-grass-54508

01/11/2023, 2:51 PM
ah you should not do it like that.
bucket.BucketName
is an
Output<string>
which is sort of like a task or promise - it represents a value that does not necessarily exist yet. you cannot use an
Output<string>
of a value that does not exist (but may exist in the future) to create a
string
value that you want "now" so you setting
ContainerDefinitions = someString
will not work as you are seeing luckily, pulumi lets you easily chain and combine Outputs to create other Outputs.
ContainerDefinitions
is an
Input<string>
which allows you to pass in a
string
(for convenience), or
Input<string>
or an
Output<string>
essentially in your first example you are trying to do something like this: (putting it in 2 separate statements to show what types you are working with)
Copy code
string containerDefs = $@"[ ... ]";
ContainerDefinitions = containerDefs
there is no (good) way to take an Output<string> like bucket.BucketName and get it to appear somewhere in that
string containerDefs
what you want to do instead is something like
Copy code
Output<string> containerDefs = ... something
ContainerDefinitions = containerDefs
and to do that, you have a few options.
Apply
is one way like @billowy-army-68599 mentioned:
Copy code
Output<string> containerDefs = bucket.BucketName.Apply(x =>
{
   // here, x is a string you can use normally
   return $"some string with {x} inside";
});
ContainerDefinitions = containerDefs
you can also use the convenience helper method Output.Format
Copy code
Output<string> containerDefs = Output.Format($"some string using {bucket.BucketName} inside");
ContainerDefinitions = containerDefs
Output.Format takes in a formattable string and does the Apply for you behind the scenes
2 Views