Programmatically finding the S3 bucket where CloudFormation uploads templates

0

Say I prepare a CFT and start to deploy it in the CloudFormation Console: on the first "Create Stack" page, when choosing to use an existing template, the page will upload the chosen local template to an S3 bucket, specially created with the first template. Or similarly, when using the aws cloudformation create-stack command, a local CFT gets uploaded to that same bucket, and then CloudFormation does its thing using that S3 location.

The bucket is always of the form cf-templates-<gibberish>-<region>. The question: is there a simple AWS CLI command to show what that resulting bucket name is for a given account?

Rather than constantly using the same local CFT for multiple deployments (which would upload a copy every time), we're going to upload the CFT to S3 ahead of time and then merely pass the URL of the template (basic stuff here). And we figured to use the same bucket that the CFN Console itself has already created during interactive Console use, because its permissions are already handled correctly for sharing templates. The kicker, of course, is the gibberish string added to make the bucket name globally unique. Currently I'm retrieving the name using

aws --output text s3api list-buckets --query 'Buckets[?starts_with(Name, `cf-templates-`)] | [?contains(Name, `'"${our_aws_region_variable}"'`)].Name'

which works reliably, at the cost of some ugly shell quoting games to wedge a variable expansion in there without messing with all the backticks. Wondering if there was a more robust / faster / generally cleaner way. ("aws cloudformation get-the-unique-magic-bucket" is probably too much to hope for, heheh?)

asked 23 days ago210 views
1 Answer
2
Accepted Answer

Hello.

As of May 2024, as far as I know, I don't think it's possible to get the S3 bucket name where the template is stored with the AWS CLI's CloudFormation command.
https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/index.html

If you use the "--s3-bucket" option of the "aws cloudformation deploy" command, you can specify the S3 bucket to upload to, so you may not need to search for the S3 bucket.
By the way, please note that the bucket specified with "--s3-bucket" must be an S3 in the same region as the stack creation region, or an error will occur.
https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/deploy.html

So, the "s3api list-buckets" command you are using is a valid method.

profile picture
EXPERT
answered 22 days ago
profile picture
EXPERT
reviewed 21 days ago
profile picture
EXPERT
A_J
reviewed 22 days ago
  • That's what I was afraid of, but thank you for confirming! (And even when we're specifying a location with --s3-bucket we're still wanting to use the same bucket as the one that CFN Console uploads to, so to keep things tidy and controlled.) I appreciate your answer!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions