export async function handleCreate()

in source/secondary-bucket-creator/source/index.ts [94:123]


export async function handleCreate(props: IResourceProperties): Promise<CompletionStatus> {
  try {
    const primaryBucket = props.PrimaryBucketName
    const secondaryRegion = props.SecondaryRegion

    // Create the prefix for the secondary region bucket based on how long the bucket name in the primary region is
    const s3BucketLenMax = 63;
    const secondaryRegionLen = secondaryRegion.length;
    const maxPrefixLen = s3BucketLenMax - secondaryRegionLen - 6; // the "-6" accounts for a "-" to join the region to the prefix and for "-logs" to be appended to the logs bucket
    let secondaryBucketPrefix = primaryBucket.length > maxPrefixLen ? primaryBucket.slice(-1 * maxPrefixLen) : primaryBucket;

    if (secondaryBucketPrefix.startsWith('-')) {
      secondaryBucketPrefix = `${primaryBucket.charAt(0)}-${secondaryBucketPrefix.slice(2)}`;
    }

    const secondaryLogsBucketName = `${secondaryBucketPrefix}-${secondaryRegion}-logs`;
    const createSecondaryLogsBucketRequest = await createBucketInSecondaryRegion(secondaryLogsBucketName, secondaryRegion);
    if (createSecondaryLogsBucketRequest.Status === StatusTypes.Failed) {
      return createSecondaryLogsBucketRequest;
    }

    const secondaryBucketName = `${secondaryBucketPrefix}-${secondaryRegion}`;
    return createBucketInSecondaryRegion(secondaryBucketName, secondaryRegion, secondaryLogsBucketName, 'artifact-store')
  } catch (error) {
    return {
      Status: StatusTypes.Failed,
      Data: error
    }
  }
}