DEV Community

Cover image for Create an S3 Bucket using AWS CloudFormation
Sarah Lean 🏴󠁧󠁢
Sarah Lean 🏴󠁧󠁢

Posted on • Updated on • Originally published at

Create an S3 Bucket using AWS CloudFormation

In this blog post I want to show you how to create an S3 bucket using a CloudFormation template. If you are new to Amazon Web Services (AWS)'s CloudFormation templates, this will be a great first template to deploy!

What is CloudFormation?

AWS CloudFormation is a tool that can help you create or manage your resources within AWS in a programmatic way using Infrastructure as Code (IaC). It can help you replicate your environment easily within a few clicks.
You can declare the resources that you need within your CloudFormation template. They will be created in the right order.

A CloudFormation template can either be YAML or JSON. In this tutorial, we are going to focus on YAML.

Create the template

Let's get started with a simple template for creating an S3 Storage bucket within AWS.

Open up your favourite editor, like NotePad++ or, as I prefer, Visual Studio Code.

The first section of the template is the Format Version and Description sections.

AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation template for s3 bucket
Enter fullscreen mode Exit fullscreen mode

The Format Version helps to identify the capabilities of the template. The most up to date version as I write is 2010-09-09.
The second line is the Description, which is used to help you and others understand what the template does.
The next section we are doing to declare is the Resources section. Every template needs to at least define one resource.

    DeletionPolicy: Retain
    Type: 'AWS::S3::Bucket'
    Description: Creating Amazon S3 bucket from CloudFormation
      AccessControl: Private
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: AES256
        Status: Enabled
Enter fullscreen mode Exit fullscreen mode

We have defined what resource we want to be created and some of the properties that we want to have with that resource. For this S3 Bucket we have enabled:

  • Versioning: this enables multiple versions of all the objects in the bucket to protect against accidental deletion.
  • Encryption: enabling this ensures that all objects are encrypted when stored within the bucket.
  • Private Only: we have set it so this bucket can't be accessed by anyone from the public.

The third and final section of this template we are going to define is the Outputs.

    Description: Bucket Created using this template.
    Value: !Ref S3Bucket
Enter fullscreen mode Exit fullscreen mode

This last section of the template will return us the name of the S3 Bucket that is created. With the output section, we can use this to help us pass information to other automation tasks or for reference.

Now we have the template created save this file with the YML extension.

Deploy the template

Now that you have written the template it is now time to deploy it and create the resource.
You can deploy the template in a number of ways, the first way is through the AWS portal. Or you can deploy the template using the AWS CLI tool.

In this example we are going to deploy the template through the portal.

Head on over to the AWS management console -

Now head over to the CloudFormation Console and select "Create Stack".

AWS CloudFormation - Create stack

You'll be presented with a few options here. We have a template so we select "Template is ready" and then we want to "Upload a template file".

Create stack within AWS CloudFormation

Upload the template you just created and click on Next.
Within the next screen you need to provide a "Stack Name". This is the name of your deployment, make this something that makes sense to you.

There are a lot of other configuration options here, but we are going to leave the defaults and click Next.
You will now be taken to a review screen, check over everything and then click on Create Stack.

Review AWS CloudFormation deployment

Resource Creation

It should only take a few minutes for the storage account to be created. You can monitor progress from the CloudFormation page.

AWS CloudFormation deployed

Once the resource has been created you will get additional information under the sections such as Outputs. It will display any outputs you asked for the template to display.

Under Resources you will see any and all of the resources that the template created and you should have a clickable link to take you to that resource.

And with that you have deployed your first AWS CloudFormation template and created an S3 Bucket!

Watch the deployment in action

I recently walked through the process of deploying this template during a webinar. If you'd like to see me walk through the template in more detail then show you the deployment head on over to YouTube or watch below:

Top comments (0)