Skip to content

Commit

Permalink
feat(core): add support for AccountIdFromAlias #27642
Browse files Browse the repository at this point in the history
  • Loading branch information
YYang30 committed Nov 28, 2023
1 parent 640817b commit 1bd4042
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
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

0 comments on commit 1bd4042

Please sign in to comment.