private createOrganizationTree()

in source/aws-bootstrap-kit/lib/aws-organizations-stack.ts [121:168]


  private createOrganizationTree(oUSpec: OUSpec, parentId: string, previousSequentialConstruct: IDependable): IDependable {

    let organizationalUnit = new OrganizationalUnit(this, `${oUSpec.name}-OU`, {Name: oUSpec.name, ParentId: parentId});
    //adding an explicit dependency as CloudFormation won't infer that Organization, Organizational Units and Accounts must be created or modified sequentially
    organizationalUnit.node.addDependency(previousSequentialConstruct);

    previousSequentialConstruct = organizationalUnit;

    oUSpec.accounts.forEach(accountSpec => {
      let accountEmail: string;
      if(accountSpec.email)
      {
        accountEmail = accountSpec.email;
      }
      else if(this.emailPrefix && this.domain)
      {
        accountEmail = `${this.emailPrefix}+${accountSpec.name}-${Stack.of(this).account}@${this.domain}`
      }
      else
      {
        throw new Error(`Master account email must be provided or an account email for account ${accountSpec.name}`)
      }

      let account = new Account(this, accountSpec.name, {
        email: accountEmail,
        name: accountSpec.name,
        parentOrganizationalUnitId: organizationalUnit.id,
        type: accountSpec.type,
        stageName: accountSpec.stageName,
        stageOrder: accountSpec.stageOrder,
        hostedServices: accountSpec.hostedServices
      });
      // Adding an explicit dependency as CloudFormation won't infer that Organization, Organizational Units and Accounts must be created or modified sequentially
      account.node.addDependency(previousSequentialConstruct);
      previousSequentialConstruct = account;

      // Building stageAccounts array to be used for DNS delegation system
      if(['Prod', 'SDLC'].includes(oUSpec.name)) {
        this.stageAccounts.push(account);
      }
    });

    oUSpec.nestedOU?.forEach(nestedOU => {
      previousSequentialConstruct = this.createOrganizationTree(nestedOU, organizationalUnit.id, previousSequentialConstruct);
    });

    return previousSequentialConstruct;
  }