BucketName property in the resource’s Properties section.
Why this matters:
- A custom bucket name can make resources easier to identify across environments.
- S3 bucket names must be globally unique, so plan names carefully.
- Renaming a bucket in a template often triggers replacement of the resource during stack updates.
Resources: top-level mapping that contains all resources in the template.MyS3Bucket: logical name used inside the template.Type: AWS resource type (AWS::S3::Bucket).Properties: resource-specific configuration.BucketName: the explicit S3 bucket name you want CloudFormation to create.
| Resource element | Purpose | Example |
|---|---|---|
| Resources | Container for all resource declarations | Resources: |
| Logical ID | Template-local name for the resource | MyS3Bucket |
| Type | AWS resource type identifier | AWS::S3::Bucket |
| Properties | Settings and options for the resource | Properties: |
| BucketName | The explicit name for the S3 bucket (must be globally unique) | BucketName: my-app-bucket-prod |
S3 bucket names must be globally unique and follow S3 naming rules (3–63 characters, lowercase letters, numbers, and hyphens). If you omit
BucketName, CloudFormation generates a unique name for you. For more on naming rules see the AWS S3 documentation.- Use predictable naming conventions scoped by environment (for example,
myapp-prod-bucket) to make lifecycle operations easier. - To avoid accidental deletion during stack updates or deletions, consider adding a
DeletionPolicy: Retainto the bucket resource so CloudFormation will not delete the bucket automatically:
- If you plan to manage bucket contents programmatically, remember CloudFormation does not empty a bucket before attempting deletion. Deleting a non-empty bucket fails and can cause stack updates to roll back.
Changing
BucketName in a template typically causes CloudFormation to create a new bucket and attempt to delete the old one. If the new name is already owned by another account, the update will fail. Also, CloudFormation will not empty buckets for you — deletion of a bucket containing objects will fail. Plan carefully and use DeletionPolicy: Retain or other safeguards when renaming buckets.BucketName:
- Confirm the desired name is available and follows S3 naming rules.
- Decide whether the bucket should be retained on stack deletion (
DeletionPolicy: Retain). - If renaming, ensure downstream references (applications, IAM policies, DNS, etc.) are updated.
- Consider using environment-specific prefixes or account IDs to reduce naming collisions.
- AWS CloudFormation User Guide
- Amazon S3 bucket restrictions and limitations
- CloudFormation resource and property types reference