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

faint-motherboard-95438

04/16/2020, 10:24 PM
Hi there, How are you supposed now to successfully access a component’s value passed to a custom
ComponentResource
(in my use case, I need a bucket’s name) ? I tried to define an arg like
aws.s3.bucket
,
string
,
pulumi.Input<string>
,
pulumi.Input<aws.s3.bucket>
, I tried to pass the whole bucket object or the bucket name directly, I tried to convert the input as an output, I tried to apply, interpolate, whatever, all I get when I want to use the name is :
Copy code
Calling [toString] on an [Output<T>] is not supported.
    
    To get the value of an Output<T> as an Output<string> consider either:
    1: o.apply(v => `prefix${v}suffix`)
    2: pulumi.interpolate `prefix${v}suffix
Thanks
g

gentle-diamond-70147

04/16/2020, 10:25 PM
pulumi.Input<string>
is usually best, but you can also pass the full
aws.s3.Bucket
object. Can you share your code with how you're trying to use the bucket name?
f

faint-motherboard-95438

04/16/2020, 10:33 PM
something like
Copy code
const cdn = new CDN('website-cdn', {
  bucket: myBucket,
}, { provider })
where
myBucket
is an
aws.s3.Bucket
but I also tried directly
myBucket.bucket
with
Copy code
export interface CdnArgs {
  // choose your poison
  bucket: string | aws.s3.Bucket | pulumi.Input<string | aws.s3.Bucket>;
}

export class CDN extends pulumi.ComponentResource {
  constructor (
    name: string,
    args: CdnArgs,
    opts?: pulumi.ComponentResourceOptions,
  ) {
    super(`${pulumi.getProject()}:CDN`, name, {}, opts)

  // use it here

  }
}
g

gentle-diamond-70147

04/16/2020, 10:34 PM
Here's an example.
f

faint-motherboard-95438

04/16/2020, 10:38 PM
@gentle-diamond-70147 thanks, but nope, already tried that (was actually my first try).
g

gentle-diamond-70147

04/16/2020, 10:38 PM
That works for me - I just deployed it.
What part doesn't work for you?
f

faint-motherboard-95438

04/16/2020, 10:39 PM
I should mention I don’t use the value directly, I use it in a template literal
if that can make any difference
g

gentle-diamond-70147

04/16/2020, 10:42 PM
It does. In that case, you do need to use
.apply()
or
.interpolate
to use the value.
e.g.
Copy code
content: pulumi.interpolate`My bucket is named ${args.bucketName}`,
f

faint-motherboard-95438

04/16/2020, 10:43 PM
already tried that too, same error
g

gentle-diamond-70147

04/16/2020, 10:43 PM
Can you provide your full code? 🙂
That works for me - I just deployed it.
f

faint-motherboard-95438

04/16/2020, 11:07 PM
Ok I found out what’s wrong trying to make an mve. The problem was not in the args, but in the name of the component (the error was giving no clue though), because in my final code I do a template literal for the name with another output (which I dropped for the examples I gave you…)
looks like that’s not a good idea to do so
can’t really figure out how to get this output working, so i’ll use a static string as a name for now
Thanks @gentle-diamond-70147 for your quick help, sorry I didn’t understand where the error was coming from, that was misleading. Should always go back to an mve an start from there before searching for help.
@gentle-diamond-70147 another difference is that in your example you where passing directly the bucketName value, while I would like to pass the whole bucket object (to access several properties), and in that case I don’t really get how to do it. with an arg like
bucket: pulumi.Input<aws.s3.Bucket>;
const bucketName = bucket.bucket
does not work, and
const bucketName = pulumi.output(args.bucket).apply(b => b.bucket)
does not work either.
g

gentle-diamond-70147

04/16/2020, 11:34 PM
Ah, sorry. I did miss that in the code you initially provided. Resource and component names must be "hard coded" strings, not interpolated from resource outputs.
👍 1
You can pass the
aws.s3.Bucket
object in without the Input wrapper.
Passing
aws.s3.Bucket
as the argument...
f

faint-motherboard-95438

04/16/2020, 11:41 PM
ok, that works. I was over-thinking this one 😅 Thanks again for your help @gentle-diamond-70147 ! 🙏
g

gentle-diamond-70147

04/16/2020, 11:42 PM
You're welcome!
3 Views