Hi folks, new to Pulumi and getting our team accou...
# getting-started
a
Hi folks, new to Pulumi and getting our team account set up with some basic testing to see how things work. I have yet another question about
.apply
which I think I'm just getting syntax wrong. Code in my first reply, but the general gist of what I'm attempting: • Given two projects, we'll call them
bucket
and
iam
• Export an s3 bucket from the
bucket
project • Get the string value of that bucket's ARN in the
iam
project
bucket project
Copy code
export const bucket = new aws.s3.Bucket(
This works as expected, in the Pulumi cloud console I can see the exported value:
Copy code
{
  "arn": "arn:aws:s3:::my-awesome-bucket",
  ....
iam project
Copy code
// this works correctly
const bucketProject = new pulumi.StackReference('mycompany/bucketproject/testing');

// now need to get the bucket arn as a string
// i've tried about a dozen variations of this, all either resulting in "undefined" or Output<T>
const arnAsString = bucketProject.outputs.apply((bucket) => { bucket.arn });
Thanks for any help!
Ah, after searching more, I found https://pulumi-community.slack.com/archives/CJ909TL6P/p1652759592057809 which I think explains the problem for anyone else searching through here in future. Going to work on trying it
b
You cannot get a string from an output if you haven't figured that out yet
a
@bored-oyster-3147 hmm, I guess https://www.pulumi.com/docs/intro/concepts/inputs-outputs/#outputs-and-strings is then really confusing me
Copy code
// Would like to produce a string equivalent to: http://${hostname}:${port}/
let url = pulumi.all([hostname, port]).
    apply(([hostname, port]) => `http://${hostname}:${port}/`);
Creates a string. What I'm missing I think is that example isn't using an output from another stack?
l
That code does not create a string. It creates an output, that eventually will contain a string.
b
^ Sorry been busy working. Like he said. Outputs work like promises. All you are doing within an apply is transforming the eventual result. But the result is still eventual
l
However, in your use case, it doesn't matter. You don't need a string ARN to create a role that allows access to the bucket. You can use the ARN output (stack reference) directly. Pulumi knows that it is an output wrapping a string, and it will get the string correctly when it needs it.
☝️ 1
So long as you don't try to put the output into a string variable, you'll be fine. So don't try this:
Copy code
const arn: string = stack.require(bucketARN);
Just do this and it will work:
Copy code
const arn = stack.require(bucketARN);
You can use that variable as a property of an args object that you're passing to a Pulumi resource constructor. Pulumi will handle the rest.
c
”_!”
a
@bored-oyster-3147@little-cartoon-10569 thank you both very much, that was all really helpful. If you happen to notice anything wrong in what I wrote below let me know so I can update it for others. For anyone finding this in the future I'll add some details below; this is relevant to Pulumi's Node TypeScript package. A resource that was really helpful was https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/ particularly the section on StackReference. TypeScript - Exporting / Importing S3 Bucket, Using String Value e.g. ARN To set up a minimal example, let's say you have an s3 bucket in project1, and you need to reference its ARN in project2. Both projects have a stack named 'prod'. Note: when exporting from project1, you must export the bucket's ARN. As far as I can tell you cannot export the entire bucket and try to reference
bucket.arn
in project2, which is a bit unfortunate. So this works: project1
Copy code
// export the bucket's ARN
export const bucketArn = bucket.arn;
project2
Copy code
// import project1
const project1 = new pulumi.StackReference('myorganization/project1/prod');

// using the bucket ARN. after this, you can use the arn var as you'd expect
const arn = project1.requireOutputValue('bucketArn');
project2 - inside a string value
Copy code
// if you need to use the bucket ARN to create an actual string, which is pretty common, you can do the following. this happens to be part of an aws.iam.PolicyDocument where we need to list the bucket resource with an /* appended to it:

{
	Action: [
	  's3:GetObject',
	],
	Effect: 'Allow',
	Resource: [pulumi.interpolate`${project1.outputs.bucketArn}/*`],
}
✔️ 1
My only question now is how to handle things in a simple scenario where we have a
dev
and
prod
stack in each project...but I'll save that for another day 🙂