Hi, how do I construct s3 bucket using `Output<...
# general
a
Hi, how do I construct s3 bucket using
Output<string>
variable? I retrieve the region as type
Output<string>
which I want to use as prefix in the bucket name. I want to either convert it to
string
or use the
Output<string>
directly in the bucket creation. I tried the
apply
and
interpolate
syntax but I can’t figure out how to use correctly.
const region = pulumi.output(aws.getRegion()).name;
`const devDataBucket = new aws.s3.Bucket(
${region}-dev-data
);` Appreciate the help!
v
According to the documentation, `Output`s can only be used post-deployment, not as part of a deployment. Are you trying to do the latter? Documentation I'm referencing I pulled from the code for
Output<T>.get(): T
Copy code
/**
     * Retrieves the underlying value of this dependency.
     *
     * This function is only callable in code that runs in the cloud post-deployment.  At this
     * point all Output values will be known and can be safely retrieved. During pulumi deployment
     * or preview execution this must not be called (and will throw).  This is because doing so
     * would allow Output values to flow into Resources while losing the data that would allow
     * the dependency graph to be changed.
     */
a
Yes, I want to use the region specified in
Pulumi.dev.yaml
config in the s3 bucket name during creation of pulumi stack. In pulumi/aws v1.x package, we could retrieve it as a string using just
aws.getRegion().name
How do I retrieve and use this value in the latest version?
b
Hi @agreeable-machine-73141, consider this example:
Copy code
import * as pulumi from '@pulumi/pulumi';
import * as aws from '@pulumi/aws';
import { interpolate } from '@pulumi/pulumi';

const region = pulumi.output(aws.getRegion({}, { async: true})).name;
const prefix = interpolate`${region}-dev-data`
const devDataBucket = new aws.s3.Bucket('devDataBucket', {
  bucket: prefix
});
🤯 1
I would also recommend you to add some unique flavour to your prefix, as s3 bucket name are globally unique, hence name
${region}-dev-data
is quite generic and can easily overlap with someone else bucket.
a
Thanks. It worked with some changes.
Copy code
import * as pulumi from '@pulumi/pulumi';
import * as aws from '@pulumi/aws';
import { interpolate } from '@pulumi/pulumi';

const region = pulumi.output(aws.getRegion({}, { async: true})).name;
const prefix = interpolate`${region}-dev-data`
const devDataBucket = prefix.apply(p => {
  return new aws.s3.Bucket(p, {
    bucket: p
  })
});
The prefix has other qualifiers. I provided a simplified code snippet here.