export async function onEventHandler()

in source/aws-bootstrap-kit/lib/account-handler/index.ts [34:65]


export async function onEventHandler(
  event: any
): Promise<OnEventResponse> {
  console.log("Event: %j", event);

  switch (event.RequestType) {
    case "Create":
      const awsOrganizationsClient = new Organizations({region: 'us-east-1'});
      try {
        const tags: { Key: string; Value: any; }[] = [];
        Object.keys(event.ResourceProperties).forEach( propertyKey => {
          if( propertyKey != 'ServiceToken' ) tags.push({Key: propertyKey, Value: event.ResourceProperties[propertyKey]});
        });
        const data = await awsOrganizationsClient
        .createAccount({
          Email: event.ResourceProperties.Email,
          AccountName: event.ResourceProperties.AccountName,
          Tags: tags
        })
        .promise();
        console.log("create account: %j", data);
        return { PhysicalResourceId: data.CreateAccountStatus?.Id };
      } catch (error) {
        throw new Error(`Failed to create account: ${error}`);
      }
    case "Update":
      return { PhysicalResourceId: event.PhysicalResourceId, ResourceProperties: event.ResourceProperties };
    default:
      throw new Error(`${event.RequestType} is not a supported operation`);
  }

}