Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add support for AccountIdFromAlias #27642 #28180

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/aws-cdk-lib/core/lib/cfn-fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ export class Fn {
return Token.asList(new FnCidr(ipBlock, count, sizeMask));
}

/**
* The intrinsic function ``Fn::AccountIdfromAlias`` returns the AWS account ID that
* corresponds to the provided AWS account alias.
* @param alias The user-provided AWS account alias string.
* @returns a token represented as a string
*/
public static accountIdFromAlias(alias: string): string {
return Token.asString(new FnAccountIdFromAlias(alias));
}

/**
* Given an url, parse the domain name
* @param url the url to parse
Expand Down Expand Up @@ -675,6 +685,21 @@ class FnCidr extends FnBase {
}
}

/**
* The intrinsic function ``Fn::AccountIdFromAlias`` resolves an AWS account
* alias to its account id. This function is typically used to reference another
* AWS account within your template.
*/
class FnAccountIdFromAlias extends FnBase {
/**
* Creates an ``FnAccountIdFromAlias`` function.
* @param alias The user-provided account alias string.
*/
constructor(alias: string) {
super('Fn::AccountIdFromAlias', alias);
}
}

class FnConditionBase extends Intrinsic implements ICfnRuleConditionExpression {
readonly disambiguator = true;
constructor(type: string, value: any) {
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,10 @@ export class CfnParser {
}
return { Condition: condition.logicalId };
}
case 'Fn::AccountIdFromAlias': {
const value = this.parseValue(object[key]);
return Fn.accountIdFromAlias(value);
}
default:
if (this.options.context === CfnParsingContext.RULES) {
return this.handleRulesIntrinsic(key, object);
Expand Down
10 changes: 10 additions & 0 deletions packages/aws-cdk-lib/core/test/fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ test('Fn.len with resolved value', () => {
expect(Fn.len(Fn.split('|', 'a|b|c'))).toBe(3);
});

test('Fn.AccountIdFromAlias', () => {
const stack = new Stack();
const token = Fn.accountIdFromAlias('test-aws-account-alias');

expect(stack.resolve(token)).toEqual({
'Fn::AccountIdFromAlias':
'test-aws-account-alias',
});
});

function stringListToken(o: any): string[] {
return Token.asList(new Intrinsic(o));
}
Expand Down