Using Cloudformation & AWS-Cli setup S3 Bucket

Saqib Ullah Siddiqui
3 min readAug 6, 2023

In this blog we will look at how to create a S3 bucket using IaC template and deploy the snippet using aws-cli option. To write an infrastructure code we used Cloudformation service.

Without waiting further time I’m going to jump on the Cloudformation script, which we have created in YAML format S3-Bucket-Template. AWS Cloudformation service allows you to write the either in YAML or JSON form.

AWSTemplateFormatVersion: 2010-09-09
## =================== DESCRIPTION =================== ##
Description: ---
AWS CloudFormation S3 bucket template
- Create a new S3 bucket without policy
Parameters:
paramS3Name:
Description: Name of s3 bucket
Type: String
Default: 'test-s3bucket-using-iac'
Resources:
mytestings3Bucket:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Delete #allow CloudFormation delete the bucket when stack is deleted
Properties:
BucketName: !Ref paramS3Name
Tags:
- Key: 'name'
Value: 'testing-bucket'
Outputs:
bucketName:
Value: !Ref mytestings3Bucket
Description: my testing bucket description

To create the above Cloudformation template we need to upload this on S3 bucket. To upload the IaC snippet we can either use aws console or aws-cli, here we are using aws-cli option and assuming that cli is pre-configured. To check the existing aws-cli configuration use the following command.

$ aws configure list

Here we are uploading the S3-Bucket-Template on our existing bucket called su-bucket.

$ aws s3 cp S3-bucket.yaml s3://su-bucket

To verify that file is copyed in the bucket use following command. You will get the file listing.

$ aws s3 ls s3://su-bucket

Now it a time to create a Cloudformation stack with the uploaded script. So we need to specify the template-url in the command. One thing that is important here is template-url parameter want complete template file details, I mean in which region do you upload the file? To get the region details use give s3 api option.

$ aws s3api get-bucket-location --bucket su-bucket

Then, let’s create the stack by using S3 object URL as template source!

$ aws cloudformation create-stack --stack-name test-s3bucket-demo --template-url https://subucket.s3.us-west-1.amazonaws.com/S3-bucket.yaml
Cloudformation create-stack

Parameters

By default S3 bucket name is test-s3bucket-using-iac which specify in the snippet, Cloudformation allow the parameterization and which really effective in many cases. With the help of — parameter-overrides option end user override the value of any parameter.


$ aws cloudformation create-stack --stack-name test-s3bucket-demo --template-url https://subucket.s3.us-west-1.amazonaws.com/S3-bucket.yaml --parameter-overrides paramS3Name=any-Name

It’s not feasible when you have too many parameters as arguments. So the good point is Cloudformation also allow to pass params as JSON file. Let’s take the following example and save the file as param.json in your working directory.

[
{
"ParameterKey": "paramS3Name",
"ParameterValue": "param value"
}
]

Now run this command !

$ aws cloudformation create-stack --stack-name test-s3bucket-demo --template-url https://subucket.s3.us-west-1.amazonaws.com/S3-bucket.yaml --parameter-overrides file://param.json

To get to know the result of the above step, we can either check the status of executing Cloudformation stack or directly take the listing of the S3 bucket

Using describe-stacks to get stack execution status or directly take S3 bucket listing.

$ aws cloudformation describe-stacks --stack-name test-s3bucket-demo
Cloudformation describe-stacks

I hope you find this post helpful, please feel free to share your comments !

--

--

Saqib Ullah Siddiqui
Saqib Ullah Siddiqui

Written by Saqib Ullah Siddiqui

I read to know, I write to recall.

No responses yet