aws,

How to create an S3 bucket through AWS CloudFormation

Oct 03, 2021 · 3 mins read · Post a comment

Deploying your infrastructure through CloudFormation as an infrastructure as code tool can cause some murky situations as a beginner because AWS is facing a lack of CloudFormation documentation. It’s weird that AWS developers are more focused on creating Terraform modules instead of focusing on CloudFormation as an AWS proprietary service.

Some of the big companies which have built their infras through CloudFormation are in abiding fear of announcing CloudFormation as deprecated. Here I’ll show you how to create an S3 template and deploy it through CF.

Prerequisites

  • AWS account

Create S3 bucket through CloudFormation in a YAML format

Step 1. First, we will use the bucket name as a parameter.

###############################################################################
## PARAMETERS
###############################################################################
Parameters:
  BucketNameDevCoops:
    Type: String
    Default: "devcoopsbucket"
    Description: Name for the DevCoops S3 bucket

Step 2. Now we can jump on the Recourse property and define the initial S3 bucket.

###############################################################################
## RESOURCES
###############################################################################
Resources:
  
  ## S3

  S3BucketDevCoops:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Ref BucketNameDevCoops
      PublicAccessBlockConfiguration:
        BlockPublicAcls : true
        BlockPublicPolicy : true
        IgnorePublicAcls : true
        RestrictPublicBuckets : true

Step 3. As the last step I will create an IAM User devcoops and define some S3 actions that I want to use.

UserDevCoops:
    Type: AWS::IAM::User
    Properties:
      Path: "/"
      UserName: devcoops 
      Policies:
        - PolicyName: user-access
          PolicyDocument:
            Statement:
              - Action:
                  - "s3:GetObject"
                  - "s3:PutObject"
                  - "s3:ListBucket"
                  - "s3:DeleteObject"
                  - "s3:GetBucketLocation"
                Effect: "Allow"
                Resource:
                  - !Join ["", ["arn:aws:s3:::", !Ref S3BucketDevCoops]]
                  - !Join ["", ["arn:aws:s3:::", !Ref S3BucketDevCoops, "/*"]]

Completed CloudFormation Configuration

###############################################################################
## PARAMETERS
###############################################################################
Parameters:
  BucketNameDevCoops:
    Type: String
    Default: "devcoopsbucket"
    Description: Name for the DevCoops S3 bucket

###############################################################################
## RESOURCES
###############################################################################
Resources:
  
  ## S3

  S3BucketDevCoops:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Ref BucketNameDevCoops
      PublicAccessBlockConfiguration:
        BlockPublicAcls : true
        BlockPublicPolicy : true
        IgnorePublicAcls : true
        RestrictPublicBuckets : true

  UserDevCoops:
    Type: AWS::IAM::User
    Properties:
      Path: "/"
      UserName: devcoops 
      Policies:
        - PolicyName: user-access
          PolicyDocument:
            Statement:
              - Action:
                  - "s3:GetObject"
                  - "s3:PutObject"
                  - "s3:ListBucket"
                  - "s3:DeleteObject"
                  - "s3:GetBucketLocation"
                Effect: "Allow"
                Resource:
                  - !Join ["", ["arn:aws:s3:::", !Ref S3BucketDevCoops]]
                  - !Join ["", ["arn:aws:s3:::", !Ref S3BucketDevCoops, "/*"]]

Deploy the CloudFormation template

To deploy the CF template follow the next steps:

  • Login to your AWS account.
  • Open the CloudFormation service.
  • Click on the Create stack button and choose With new resources (standard).
  • Next, select the Upload a template file field.
  • Upload your local yaml file.

Conclusion

This tutorial is aimed to give you a brief example of how can you create a CloudFormation template and deploy it through AWS Console as well. If you are struggling to create and deploy a CloudFormation template, let us know in the comment section below.
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.