export async function isCompleteHandler()

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


export async function isCompleteHandler(
  event: IsCompleteRequest
): Promise<IsCompleteResponse> {
  console.log("Event: %j", event);

  if(!event.PhysicalResourceId) {
    throw new Error("Missing PhysicalResourceId parameter.");
  }

  const awsOrganizationsClient = new Organizations({region: 'us-east-1'});

  const describeCreateAccountStatusParams : Organizations.DescribeCreateAccountStatusRequest = {CreateAccountRequestId: event.PhysicalResourceId}
  const data: Organizations.DescribeCreateAccountStatusResponse  = await awsOrganizationsClient
    .describeCreateAccountStatus(describeCreateAccountStatusParams).promise();

  console.log("Describe account: %j", data);

  const CreateAccountStatus = data.CreateAccountStatus?.State;
  const AccountId = data.CreateAccountStatus?.AccountId;

  switch (event.RequestType) {
    case "Create":
      if (CreateAccountStatus === "FAILED") {
        throw new Error(`Error creating the account ${data.CreateAccountStatus?.AccountName}, cause: ${data.CreateAccountStatus?.FailureReason}`)
      }
      return { IsComplete: CreateAccountStatus === "SUCCEEDED", Data: {AccountId: AccountId} };
    case "Update":
      if(AccountId) {
        console.log(`Add tags: type = ${event.ResourceProperties.AccountType}`);
        const tags: { Key: string; Value: any; }[] = [];
        Object.keys(event.ResourceProperties).forEach( propertyKey => {
          if( propertyKey != 'ServiceToken' ) tags.push({Key: propertyKey, Value: event.ResourceProperties[propertyKey]});
        });
        const tagsUpdateRequestData = await awsOrganizationsClient
        .tagResource({
          ResourceId: AccountId!,
          Tags: tags
        })
        .promise();
        console.log("Updated account tags: %j", tagsUpdateRequestData);
      }
        return { IsComplete: CreateAccountStatus === "SUCCEEDED", Data: {AccountId: AccountId} };
    case "Delete":
      // TODO: figure out what to do here
      throw new Error("DeleteAccount is not a supported operation");
  }
}