The following code throws an error for calling `id...
# general
s
The following code throws an error for calling
id
on undefined
sourceAccounts is of type
aws.organizations.Account[]
a
pulumi.Output<aws.organizations.Account[]>
right?
s
it is not wrapped in pulumi.Output
its the result of calling
new aws.organizations.Account
a
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
yes
account.id
a
I have a feeling it's how the array is generated
s
im just instantiating an array like:
const sourceAccounts = [account1, account2, …]
👍 1
a
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
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
yeah that sounds circular for sure
s
not sure why TS lets you do that w/o warning
a
i'm still confused what error you're seeing though. the undefined thing is definitely from backwards apply and map logic.
s
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
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