Detailed Guide to Create S3 Bucket Using AWS CloudFormation

CloudFormation can help you to create and manage S3 buckets via codes. This article offers you some CloudFormation Templates and also a detailed instruction using CloudFormation to create S3 buckets.

Amelia

By Amelia / Updated on October 15, 2024

Share this: instagram reddit

What is AWS S3 Bucket CloudFormation

CloudFormation is Amazon's IaC service that allows for the creation, management, and provisioning of AWS and third-party resource collections across accounts and regions. All of this can be done through code, also, S3 buckets, EC2 instances, Amazon S3 encryption, databases, etc, can be defined via JSON or YAML.

AWS CloudFormation Templates for S3 Bucket

A basic CloudFormation template typically consists of the following components: AWSTemplateFormationVersion, Description, and Resources. What follows is a basic example of a CloudFormation template creating an S3 storage bucket.

→Example 1: Simple S3 Bucket Template (YAML Format)

AWSTemplateFormatVersion: '2010-09-09'
Description: Simple CloudFormation Template to create an S3 bucket
 
Resources:
MyS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-cloudformation-bucket

→Example 2: S3 Bucket with Versioning and Lifecycle Rules

AWSTemplateFormatVersion: '2010-09-09'
Description: S3 Bucket with Versioning and Lifecycle Rules
 
Resources:
VersionedS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-versioned-bucket
VersioningConfiguration:
Status: Enabled
LifecycleConfiguration:
Rules:
- Id: CleanupRule
>Status: Enabled
ExpirationInDays: 30
Prefix: "logs/"
NoncurrentVersionExpirationInDays: 60

→Example 3: S3 Bucket with Server-Side Encryption

AWSTemplateFormatVersion: '2010-09-09'
Description: S3 Bucket with Server-Side Encryption (SSE-S3)
 
Resources:
EncryptedS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-encrypted-bucket
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256

→Example 4: S3 Bucket with Bucket Policy

AWSTemplateFormatVersion: '2010-09-09'
Description: S3 Bucket with Bucket Policy to restrict public access
 
Resources:
SecuredS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-secured-bucket
 
SecuredS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref SecuredS3Bucket
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Deny
Principal: '*'
Action: 's3:*'
Resource: !Sub '${SecuredS3Bucket.Arn}/*'
Condition:
Bool:
'aws:SecureTransport': 'false'

How to Use CloudFormation to Create S3 Bucket

AWS S3 bucket policy CloudFormation enables you to create and manage S3 buckets as code, here are the detailed steps to create S3 buckets using CloudFormation.

📌Note: Before heading into creating an S3 bucket, you need to make sure there is CloudFormation permission for users to create, update, and delete stacks. And also the S3 bucket name is globally unique.

Step 1. Create a New Bucket

In the AWS console homepage, navigate to S3 > Create bucket to open the create page, then select Enable and Amazon S3-managed keys (SSE-S3).

create bucket

enable amazon s3 managed keys

Step 2. Upload the CloudFormation Template to AWS

1. In the AWS console homepage, navigate to CloudFormation > Create stack > With new resource (standard).

create stack

2. After opening the create stack page, select the Template is ready, you can use the template in example 1. Then select Update a template file, and click Choose file to upload the code. You can click View in Designer to view the graphical display of the infrastructure.

in create stack page

3. In the Specify stack details tab, ensure your Stack name is unique. For Parameters, if your template contains parameters, you can enter them here. After all is done, click Next.

in the specify stack details tab

4. On the next screen, you can leave all the options at default, and click Next.

check options and click next

5. In the final page, you will see the things you have completed, if everything is OK, click Create stack.

in final page

Step 3. Monitor the Creation Process

Once you click Create stack, the CloudFormation will start the creation process. When the process is complete, you can see the created resources in the Resources tab.

in resource tab

📌Tips: Using CloudForation for S3 Buckets can provide several benefits, including infrastructure as code, automated resource management, rollbacks and update, and scalability and flexibility.

How to Automate Data Backup to Amazon S3

For data security, after creating S3 buckets, you can back up your data to Amazon S3. AOMEI Cyber Backup is a professional backup software that can ensure that data is consistently protected and easily recoverable. Following are more features you can enjoy while using AOMEI Cyber Backup.

😊User-Friendly Interface: It can provide you simple backup process with an intuitive, easy-to-use interface.
🔰Incremental and Differential Backup: It can reduce backup time and storage space by only backup changed data.
✨Flexible Recovery Options: It restores data from any backup point with ease.
🔆Automated Backups: It schedules backups to run automatically at specified intervals.

Download Free Trial Centralized Backup Solution
Secure Download

🎈Follow the following steps to begin using AOMEI Cyber Backup with Amazon S3.

Step 1. On the AOMEI Cyber Backup homepage, navigate to Target Storage > Amazon S3 > Add Target to open the add target page. Enter the required information, including Username, Password, and Bucket, then click Confirm. Make sure you have the necessary permissions set up in your AWS account.

add amazon s3

Step 2. Navigate to Backup Task, and click Create New Task to start archiving the important data to Amazon S3. For example, select File Backup and choose files or folders for backup.

file backup

Step 3. Select Archiving backup versions to Amazon S3 to choose the added Amazon S3.

archive to Amazon S3

Step 4. Schedule backup tasks to run daily, weekly, or monthly, and select backup retention policies to delete old backups automatically.

Step 5. Click Start Backup to begin the backup process. It will first create backups locally or on the NAS and then upload the backup to Amazon S3. According to the 3-2-1 backup rule, ensures the security of critical data and business continuity.

start backup

Conclusion

No matter if you are creating S3 buckets or implementing lifecycle policies and encryption, with CloudFormation, you can achieve these in codes. Also, it can create and manage S3 buckets efficiently, making sure the AWS environment is in a consistent and automated state.

Amelia
Amelia · Editor
Amelia is an editor from AOMEI. Trained in professional systems, she specializes in solving problems in enterprise databases, virtual machines, and physical environments. With a high sense of responsibility, she is dedicated to protecting business data and security.