Mastering CloudFormation: Building Scalable and Efficient AWS Infrastructure
What is AWS CloudFormation?
AWS CloudFormation allows you to model, provision, and manage AWS and third-party resources using a single source of truth for your cloud environment. Through templates written in JSON or YAML, CloudFormation gives you the ability to automate the creation of everything from EC2 instances to security groups, databases, and more.
Will do a practical now.
Before moving ahead always go with the AWS official documents before implementing any services .
The above documents have details about templates, Json as well as Yaml file.
Even the file has a Troubleshooting section where each error is mentioned with troubleshooting steps.
Go to Template format and I would like to refer to the go with the YAML example shows the structure of a YAML-formatted template with all available sections.
Go to the AWS Console and type CFT or CloudFormation.
Click on Cloudformation Service.
Above is the user interface for CloudFormation. First thing we are able to see above is “create a stack” What is the stack ? The Stack is the one that implements the template.
When we write the templates we have to submit in the stack and stack will convert our template request to the API using the CloudFormation service.
Before Moving ahead try to install the Microsoft Visual Studio Code. Visual Studio Code is a streamlined code editor with support for development operations like debugging, task running, and version control.
Click on Create Stack.
Once we click on Create Stack there are three options below:
Then Select from “ Build from application composer”
Post that click on “Create in Application Composer” So once we go in it is just like a drag and drop tool. So here if let's say we want the cloudformation template for the S3 Bucket. So type S3 in the left pane.
So just drag the S3 bucket right side.
Then click on “Template” & Template has been populated.
If you want to validate the Template just click on “Validate” & your template has been validated.
Post Validation the Template has no validation errors that means the yaml code is clean.
Then click on Create Template.
After that CloudFormation makes a default bucket to put the template or else if you want to put template in an existing bucket you can mention bucket name in option. So as of now I keep the default one.
Click on Confirm and continue to the CloudFormation.
Now the Template has been successfully imported. Click Next
Give the stack name & click on next.
Keep it all as it is. & then click next
Click Submit.
The Stack status is create_in_progress
After some time we have got the following error appear on the screen as below:
We have verified that status is CREATE_FAILED
Will go and click on Detect Root Cause & found this error “Resource handler returned message: "Bucket name should not contain uppercase characters" (RequestToken: 5240a5a9-1c9b-6e3b-9ece-4cfcc178b2cd, HandlerErrorCode: GeneralServiceException”
We rectified that the bucket was attempting to create a bucket with an uppercase letter with the name. We will go again and create a bucket with a name that only contains lower case.
Now it's in progress.
And it's been initiated by the user Aparad68 i.e.me.
So the stack has been completed successfully.
In this demo I have just taken the S3 bucket but with the help of CFT we are able to create multiple resources at the same time.
Now we can check on the console the Bucket whether created or not?
The bucket has been created successfully.
Also there is a default Bucket created by the CFT which stores all the templates cf-templates-1rry…
We have able to see the default templates have been created.
Before that I will delete the created bucket that was created through the CFT.
We will be deleting the bucket not the Stack. Stack we will keep as it is
Click on Delete Bucket.
We are going to learn a very important topic and that is Drift.
What is the Drift Detection in CFT?
AWS CloudFormation Drift Detection
AWS CloudFormation provides a drift detection feature that helps you identify resources that have drifted from their template configuration. This feature is useful for ensuring that your resources remain consistent with the defined infrastructure.
On the left pane click on drift & then Detect Stack Drift
Then go to the Stack option , Select the Stack name & click the View drift results.
So the status is showing the Drifted.
Now what can i do I’m going to delete all the configurations including stack as well
All Stacks have been deleted.
Now again we have created the stack to create a S3 bucket.
We will going to modify in the yaml code to check whether drift has been
So we have checked in the doc and found that versioning configuration. So we will enable the versioning for the created Bucket. So below is the example.
We will going to copy and paste the VersioningConfiguration in the existing yaml code
Now we will go into the bucket.
We have checked in bucket properties that bucket versioning is enabled now. So what we will do we can manually change to suspend the versioning.
Click on Edit and will suspend the versioning
Click Save changes.
Now the Versioning has been suspended,
With the help of the Drift Detection Policy it should show the exact change.
Now see with the snap below once we clicked on Detect stack drift drift status is DRIFTED
Then click on view drift details
The result is below:
So if we analyzed the details section previously the versioning was enabled but I have manually Suspended the versioning.
Conclusion
Drift can compromise the consistency and reliability of your infrastructure. AWS CloudFormation’s drift detection features help you monitor, identify, and address drift to maintain infrastructure integrity. By following best practices and regularly checking for drift, you can reduce the chances of unexpected behavior and maintain control over your cloud environment.
Happy Learning !!
Subscribe to my newsletter
Read articles from Amit Parad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Amit Parad
Amit Parad
Experienced Cloud / DevOps Engineer with a passion for automating infrastructure and streamlining software delivery processes. Skilled in AWS, Docker, Kubernetes, CI/CD pipelines, Ansible, Terraform & Jenkins. Proven ability to collaborate with development, operations, and QA teams to ensure efficient and reliable deployments.