https://pulumi.com logo
#typescript
Title
# typescript
r

refined-terabyte-65361

08/16/2021, 4:41 PM
Hi i am getting below error while trying to use bucket name
Copy code
Calling [toString] on an [Output<T>] is not supported.

 o get the value of an Output<T> as an Output<string> consider
code :
Copy code
const accessLogsBucket = new aws.s3.Bucket(dev-access-logs,{bucket:dev-access-logs});
    const accessbucketPolicy = new aws.s3.BucketPolicy(`${accessLogsBucket.id}`, {
      bucket: accessLogsBucket.bucket,
      policy: accessLogsBucket.bucket.apply(accessLogsBucketPolicy)
    })
g

green-stone-37839

08/16/2021, 5:12 PM
Meaning you're trying to do something like:
accessLogsBucket.toString()
? If so, you'll need to use the
apply()
function. This behaves like a
promise<T>
and will allow you to access the underlying types of an
Output<T>
. As an example:
Copy code
accessLogsBucket.id.apply(id => {
  console.log(id);
};
r

refined-terabyte-65361

08/16/2021, 5:15 PM
i am actually trying to access the bucket id from multiple locations with in the same file (created bucket using the same file)
g

green-stone-37839

08/16/2021, 5:29 PM
You'll need to use the `apply()`` function to access the id of the bucket. See above example. Also, with typescript you can use the
interpolate
function to shorten usage. If you havent already I recommend reading this documentation.
s

steep-toddler-94095

08/16/2021, 6:38 PM
interpolate
can't be used in the first parameter of a Pulumi resource, just fyi. If you want to create a resource with the name of an
Output<string>
you'll have to create the resource within an
apply
r

refined-terabyte-65361

08/16/2021, 7:52 PM
Hi @green-stone-37839 @steep-toddler-94095 I tried to use like below
Copy code
const accessLogsBucket = new aws.s3.Bucket(dev-access-logs,{bucket:dev-access-logs});

    const bucketName = accessLogsBucket.id.apply(
      (id) => id
    );
when i tried to pass the bucketName to alb
Copy code
"<http://alb.ingress.kubernetes.io/load-balancer-attributes|alb.ingress.kubernetes.io/load-balancer-attributes>": `access_logs.s3.enabled=true,access_logs.s3.bucket=${bucketName},access_logs.s3.prefix=dev-Access-Logs`
it still fails with below error
Copy code
access_logs.s3.enabled=true,access_logs.s3.bucket=Calling [toString] on an [Output<T>] is not supported.
when i tried to print accessLogsBucket i get below output
Copy code
Bucket {
      __pulumiResource: true,
      __transformations: [],
      __name: 'dev-access-logs',
      __providers: {},
      __protect: false,
      __aliases: [],
      id: OutputImpl {
        __pulumiOutput: true,
        resources: [Function (anonymous)],
        allResources: [Function (anonymous)],
        isKnown: Promise { <pending> },
        isSecret: Promise { <pending> },
        promise: [Function (anonymous)],
        toString: [Function (anonymous)],
        toJSON: [Function (anonymous)]
      },
how to access __name: 'dev-access-logs', ?
s

steep-toddler-94095

08/16/2021, 8:21 PM
Copy code
const accessLogsBucket = new aws.s3.Bucket(dev-access-logs,{bucket:dev-access-logs});
const accessbucketPolicy = accessLogsBucket.id.apply(id =>
  new aws.s3.BucketPolicy(id, {
    bucket: accessLogsBucket.bucket,
    ...
}))
it needs to be done within the
apply
so you can access the value as a
string
instead of an
Output<string>
l

little-cartoon-10569

08/16/2021, 9:16 PM
You should not be using the id of one resource as the Pulumi name of another resource. Even inside an
apply
. It's an antipattern and will cause issues down the line. Pulumi names are like pointers in your stack. They're not intended to be dynamic. Think of a Pulumi name as an LHS (name of a variable) rather than an RHS (value). You don't want the name of a variable to be unknown at compile time.
In this case, this code will work better:
Copy code
const accessLogsBucket = new aws.s3.Bucket(dev-access-logs,{bucket:dev-access-logs});
    const accessbucketPolicy = new aws.s3.BucketPolicy(dev-access-logs, {
      bucket: accessLogsBucket.bucket,
      policy: {
   // Put your policy document here.
      }
    })
Aside: I haven't corrected the issue with
-
in variable names. That won't work either...
s

steep-toddler-94095

08/16/2021, 11:32 PM
agree with what tenwit said. I was too focused on just answering the question to see the antipattern