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

shy-microphone-28807

01/13/2020, 9:39 PM
The following code throws an error for calling
id
on undefined
sourceAccounts is of type
aws.organizations.Account[]
a

astonishing-cartoon-37000

01/13/2020, 9:42 PM
pulumi.Output<aws.organizations.Account[]>
right?
s

shy-microphone-28807

01/13/2020, 9:43 PM
it is not wrapped in pulumi.Output
its the result of calling
new aws.organizations.Account
a

astonishing-cartoon-37000

01/13/2020, 9:51 PM
can you pseudo code how you generate the array because
Copy code
const account = new aws.organizations.Account("foo", {email: "<mailto:foo12345@bar.com|foo12345@bar.com>"}, {})
const accountid = account.id;
seems to behave as expected for me.
with
accountid
being of type
pulumi.Output<string>
s

shy-microphone-28807

01/13/2020, 9:53 PM
yes
account.id
a

astonishing-cartoon-37000

01/13/2020, 9:54 PM
I have a feeling it's how the array is generated
s

shy-microphone-28807

01/13/2020, 9:54 PM
im just instantiating an array like:
const sourceAccounts = [account1, account2, …]
👍 1
a

astonishing-cartoon-37000

01/13/2020, 9:55 PM
let me try
oh I think I got where you went wrong. try and inverse map and apply.
actually this works even I believe
const accountids = accounts.map(account => account.id)
should give you a
pulumi.Output<string>[]
s

shy-microphone-28807

01/13/2020, 9:58 PM
you know what i bet its actually from a circular dependency
i saw this before
not 100% sure how to avoid it when trying to organize my ‘units’
ie, i have a
logging.ts
and
businessUnit.ts
and i am trying to use the acount.id from businessUnit in logging to set up the policy, and then the log bucket in businessUnit to send logs to the right place
the config has to be all in one file instead of being split by AWS account it seems
a

astonishing-cartoon-37000

01/13/2020, 10:03 PM
yeah that sounds circular for sure
s

shy-microphone-28807

01/13/2020, 10:06 PM
not sure why TS lets you do that w/o warning
a

astonishing-cartoon-37000

01/13/2020, 10:06 PM
i'm still confused what error you're seeing though. the undefined thing is definitely from backwards apply and map logic.
s

shy-microphone-28807

01/13/2020, 10:12 PM
Copy code
const sourceAccounts = [account1, account2, …]
const accountids = sourceAccounts.map(account => account.id)
that will throw the error also
say you have account1.ts and account2.ts, at the top of those files you have
import * as logging from "./logging"
then in the logging file you have
Copy code
import * as account1 from "account1"
import * as account 2 from "account2"
then the code above
it will pass the typescript compiler but when you run it you get
id called on undefined
because of the circular logic
basically have to move the import of logging below the creation of any resources that you might need to use in the ‘logging’ unit, hard to remember that though
so now i have:
Copy code
const account = new aws.organizations.Account("foo", {email: "<mailto:foo12345@bar.com|foo12345@bar.com>"}, {})

import * as logging from "./logging"
and it’s ok
a

astonishing-cartoon-37000

01/13/2020, 10:52 PM
Gotcha, yeah downside of just invoking code with import. I put nearly everything in functions and classes so I can invoke them in the orders I care about
👍 1
like
Copy code
import { Logger } from "./logging"
const account1 = new aws.organizations.Account("foo", {email: "<mailto:foo12345@bar.com|foo12345@bar.com>"}, {})
const account1Logger = new Logger(account1)
or
Copy code
import { createLogger } from "./logging"
const account1 = new aws.organizations.Account("foo", {email: "<mailto:foo12345@bar.com|foo12345@bar.com>"}, {})
const account1Logger = createLogger(account1)
but that's just my preference! glad to know you've figured out a work around