How to create and deploy S3 bucket using CloudFormation template?
Objective
Create a CloudFormation stack that creates an S3 bucket with some commonly used options.
Prerequisites for this lab
- An AWS account.
- You need to have basic knowledge of AWS services like S3, CloudFormation.
Cloud formation template example to create AWS S3 bucket
Create a new file called “create-s3-cf.template” and paste the following code to create a simple S3 bucket.
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Cloud Formation Template To Create S3 bucket",
"Parameters" : {
"BucketName" : {
"Type" : "String",
"Default" : "cloudiofy-s3-bucket",
"Description" : "S3 bucket name."
}
},
"Resources": {
"CloudiofyS3Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": { "Ref" : "BucketName" },
"AccessControl": "Private",
"VersioningConfiguration": {
"Status": "Enabled"
},
"LifecycleConfiguration": {
"Rules": [
{
"Id": "DeleteOldFiles",
"Status": "Enabled",
"Prefix": "old/",
"ExpirationInDays": 30
}
]
},
"Tags": [
{
"Key": "Environment",
"Value": "Development"
},
{
"Key": "Owner",
"Value": "info@cloudiofy.com"
}
]
}
}
}
}
In this example, we are setting the following options:
BucketName: The name of the S3 bucket, value passing through parameter, you can override default value at the time of cloud formation stack deployment.
AccessControl: The access control policy for the bucket. Here, we are setting it to private, which means that only the bucket owner can access the contents of the bucket.
VersioningConfiguration: Enables versioning for the bucket.
LifecycleConfiguration: Configures a lifecycle rule that deletes all objects in the old/ prefix after 30 days.
Tags: Assigns two tags to the bucket, one for the environment and one for the owner.
Steps to deploy cloud formation template from AWS console
- Login to your AWS Console and navigate to the “CloudFormation” service
- Click on the “Create stack” button to start the cloud formation creation process.
- Upload your created template file “create-s3-cf.template”, Click on “Next”
- Specify the stack details, Click on “Next”
- Stack failure options, Select option “Roll back all stack resources”
- Review CreateS3Bucket, Click on “Submit”. AWS resources creation will be started as per the code in the template file.
- Go to “S3” service, and validate the S3 bucket “cloudiofy-s3-bucket” created by the cloud formation template.
- You can delete the stack, to delete same s3 bucket created by the cloud formation template.
Note: AWS CloudFormation is a powerful AWS service that enables infrastructure as code, simplifying the process of creating and managing AWS resources. With CloudFormation, you can automate the deployment and management of your cloud infrastructure, saving you time and effort. You can check this link to find many sample CloudFormation templates based on different AWS regions.