One way to do it is to write your own CloudFormation Macro or use an existing one. Have a look at the example Macros at https://github.com/aws-cloudformation/aws-cloudformation-macros to see what's possible. The PyPlate macro there may suit your needs without you having to write a new Macro.
Another option is the write a Custom Resource. If you do, I strongly recommend making use of something like crhelper (https://github.com/aws-cloudformation/custom-resource-helper) - it makes it so much easier.
I would not recommend creating a custom resource for this at all. Indeed a macro is more appropriate for what you are trying to do. But I would still recommend to stick to what CloudFormation "native" has to offer in this particular use-case for the bucket name: have a parameter for the bucket name. Personally though, I much prefer to to add region and account number with the native functions. The only parameter you need is a prefix (or suffix) to the bucket name. Hope this helps :)
Relevant questions
How to append timestamp to s3 bucket name in suffix using cloudformation template?
asked a month agoCloudformation deletion deleting the S3 bucket in spite of DeletionPolicy:
asked a year agoCloudformation - Check if S3 folder exists and apply policy to the specific folder
Accepted Answerasked 4 months agoLocked out of S3 Bucket due to Bucket Policy in GovCloud
Accepted Answerasked 6 months agoS3 bucket name is not configured while creating stack through CloudFormation template.
asked 2 months agoCan s3Client be made without using s3 bucket region?
Accepted Answerasked a month agoHow to declare multi region access point in cloudformation template
asked 2 months agoCan AWS CloudFormation template access s3 from any region other than its current region?
asked 2 months agoAWS::Synthetics::Canary Not Creating S3 Bucket in EU
Accepted Answerasked 7 months agoHow to refer arn for an S3 bucket created using cfn resource in cdk python ?
asked 2 months ago