How would I get the current aws account id, so tha...
# aws
p
How would I get the current aws account id, so that I can interpolate it into the middle of a role ARN?
l
You can call sts:get-caller-identity. via the AWS SDK.
You shouldn't need to do this if you're using Pulumi to manage the role, though. Are you using the SDK directly?
For convenience, there's a Pulumi wrapper for it: https://www.pulumi.com/registry/packages/aws/api-docs/getcalleridentity/
p
It's a system created role... I guess I should use pulumi to get it, now that I think about it?
l
90% of the time, this sort of thing can be provided as a global const or config value. Would it be best to pass it in from the Pulumi.stack.yaml file?
Code isn't always the best solution 🙂
If the only thing you ever use is a constant string value (the ARN), then just set up a constant string value with the ARN in it...
p
Ah I haven't figured out when to use those extra files yet
l
Start off by defining it as
const systemCreatedRoleArn: string = "x:y:z...";
. When that's not good enough any more, improve it.
p
It's just obviously going to be the first thing that breaks when I try to run the same stack against a new account in prod :)
l
Then put it in your stack file.
Unless you will never create a new account. In which case, don't optimize prematurely 🙂
p
_looks at his dev account, which is separate from his prod account_…
l
Put the string in your stack yaml file, and get it using
new pulumi.Config().require(...)
p
It’s not a string that’s decided by my configuration, though, it’s a string that’s decided by which account I’m logged in to.
if I do this:
export const accountId = current.then(current => current.accountId);
do I have to still use pulumi.interpolate? Or can I do a normal string ala
arn:aws:iam::${accountId}…
?
l
But stacks should be hard-mapped to accounts, right? You don't have two dev stacks, one for account A and one for account B...?
export const accountId = current.then(current => current.accountId);
doesn't mean anything, it's the same as
export const accountId = current.accountId;
p
Account A is the “developer” account, so it’s basically disposable, nothing goes into it that matters. Production deployments will happen in account B, which… I dunno, may or may not have a variety of different deployment names.
So what I’m doing now is:
Copy code
const current = aws.getCallerIdentity({});
  const accountId = current.then((current) => current.accountId);
  ...
    executionRoleArn: pulumi.interpolate`arn:aws:iam::${accountId}:role/ecsTaskExecutionRole`,
  ...
I’m still a bit fuzzy on when interpolate is needed, or when the promise resolution satisfies it.
l
But the point is, the value that goes into Pulumi.A.yaml will be constant, and the value that goes into Pulumi.B.yml is constant (but different to the value in Pulumi.A.yml).
So don't bother with potentially-buggy code. Hard code the values into the config.
p
heh, I understand.
Thanks for the guidance!