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

best-lifeguard-91445

11/19/2021, 2:27 AM
Is there a hook that I can get the outputs of a resource creation that can be used to write to a database / file?
l

little-cartoon-10569

11/19/2021, 2:46 AM
Is exporting outputs from your Pulumi program not doing everything you need? Can you describe your intended flow?
b

best-lifeguard-91445

11/19/2021, 2:59 AM
I want to take the output of creating some resources and create a dynamodb TableItem with the contents
l

little-cartoon-10569

11/19/2021, 3:03 AM
You want to use outputs in the JSON going into the TableItem? You need to either 1) create the TableItem inside an
apply()
, or 2) create the TableItem outside your Pulumi program, possibly in another program, in another inline automation-api program, or outside Pulumi altogether, using the normal DynamoDB SDK or API.
If the TableItem's Item property was a pulumi.Input<string>, you could do it the normal way....
Oh cool, it is! The docs don't say that it is. You don't need to create the resource inside
apply
, you can use
pulumi.interpolate
.
Someday, someone will make a
pulumi.interpolateJson
function to make it even easier...
r

red-match-15116

11/19/2021, 3:07 AM
The docs omit
pulumi.Input
from all input types just because of redundancy but they're always `Input`s. It would probably make sense to have some sort of one-liner to that effect in the docs though.
l

little-cartoon-10569

11/19/2021, 3:08 AM
They're not always Inputs though. They should be, but there are some places where it's been missed.
I come across them quite frequently. More than once a week.
r

red-match-15116

11/19/2021, 3:08 AM
Like what? I know the resource_name is a
str
but other than that I didn't know that that was a thing.
(No need to find one now but lemme know next time you come across one)
👍 1
b

best-lifeguard-91445

11/19/2021, 3:25 AM
I thought I saw somewhere in the docs that you shouldn't create resources within apply() ?
l

little-cartoon-10569

11/19/2021, 3:27 AM
You shouldn't, if you can avoid it. And you can avoid it this time. You need to build your JSON string inside an apply, but you can pass the result to TableItem's args.item.
1
You can use pulumi.interpolate in lieu of apply(), it's the same thing.
b

best-lifeguard-91445

11/19/2021, 3:30 AM
gotcha!
l

little-cartoon-10569

11/19/2021, 3:31 AM
Something a bit like this (where foobar is an output on a resource you've created):
Copy code
const item = resource.foobar.apply(foo => JSON.stringify({ "key": `${foo}bar`})); // item is a pulumi.Output<string>
const ti = new aws.dynamodb.TableItem("ti", {
  tableName: table.name,
  hashKey: table.hashKey,
  item: item
}, opts);
b

best-lifeguard-91445

11/19/2021, 3:32 AM
oh! that makes sense. Thanks for the example.
👍 1
@little-cartoon-10569, if I had an object =>
Copy code
{ 
   foo: Output<string>
   bar: Output<string>
}
would Pulumi.output() evaluate each property?
l

little-cartoon-10569

11/19/2021, 3:40 AM
Your options there are to chain your calls to
apply()
, or use
pulumi.all()
.
❤️ 1
I sometimes use chained calls to
apply()
, it it's more readable. Though since this is a TableItem and you're building JSON, I think you're probably better off with
pulumi.all()
.