How to create and deploy S3 bucket using CloudFormation template

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

  1. Login to your AWS Console and navigate to the “CloudFormation” service
    cloud-formation
  2. Click on the “Create stack” button to start the cloud formation creation process.
    cloud-formation-home
  3. Upload your created template file “create-s3-cf.template”, Click on “Next”
    cloud-formation-create-stack
  4. Specify the stack details, Click on “Next”
    cloud-formation-create-stack-details
  5. Stack failure options, Select option “Roll back all stack resources”
  6. Review CreateS3Bucket, Click on “Submit”. AWS resources creation will be started as per the code in the template file.
    cloud-formation-created
  7. Go to “S3” service, and validate the S3 bucket “cloudiofy-s3-bucket” created by the cloud formation template.
    cloud-formation-bucket-created
  8. 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.

How to create and deploy S3 bucket using CloudFormation template
Scroll to top