From Clicks to Code: Mastering Your AWS Infrastructure with the CLI and CloudFormation


If you’ve spent any time with Amazon Web Services (AWS), you know the power of the AWS Management Console. It’s a great way to explore and launch resources with just a few clicks. However, as you scale, you quickly encounter a problem: clicking is slow, error-prone, and impossible to automate.
How do you ensure you build the same environment every single time? How do you manage infrastructure for development, staging, and production without driving yourself crazy?
The answer is to stop clicking and start coding. In this article, we’ll explore the two fundamental tools that will transform the way you work with AWS: the AWS Command Line Interface (CLI) and AWS CloudFormation (CFT).
What is the AWS CLI? Your Command Center for AWS
Think of the AWS CLI as a Swiss Army knife for AWS. It’s a simple yet powerful tool that you install on your computer, allowing you to interact with all AWS services directly from your terminal.
At its core, everything in AWS — even the web console you click around in — is powered by an API. The AWS CLI is a program (a “Python utility,” as you might think of it) that gives you direct, programmatic access to that same API.
Instead of clicking through five screens to list your S3 buckets, you can just type one command:
aws s3 ls
This makes it perfect for scripting, automation, and performing quick, direct tasks.
What is AWS CloudFormation (CFT)? Your Infrastructure Blueprint
If the CLI is for giving direct commands (imperative), CloudFormation is for defining a desired state (declarative).
CloudFormation is AWS’s flagship Infrastructure as Code (IaC) service. With IaC, you don’t manually build your infrastructure; you write a file that describes it, and AWS builds it for you. This file, called a template, is your blueprint.
Here’s why this is a game-changer:
Declarative: You don’t write the steps to create an EC2 instance. You simply declare in a file, “I want an EC2 instance with these properties.” CloudFormation figures out how to make it happen.
Repeatable & Consistent: You can use the same template to create identical environments for development, testing, and production. No more “it worked on my machine!”
Version Controlled: Your template is just a text file (written in YAML or JSON). You can store it in Git, track changes, review pull requests, and roll back to previous versions, just like any other code.
The Power Couple: A Hands-On Tutorial
Now, let’s see how they work together. We’ll use CloudFormation to define an EC2 server and a Security Group, and then use the AWS CLI to bring it to life.
Step 1: Write the Blueprint (The template.yaml File)
First, create a file named template.yaml. This is our CloudFormation template. It tells AWS what we want to build: one EC2 instance and one Security Group that allows SSH access.
Our template.yaml file defining an EC2 instance and a security group.
Here is the code for you to use.
IMPORTANT: Before you proceed, you must replace ami-05f456787539296f with a current Amazon Linux 2 AMI ID from your AWS region and your-key-pair with the name of an EC2 Key Pair that exists in your account.
AWSTemplateFormatVersion: '2010-09-09'
Resources:
MyEC2:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-xxxxxxx # your image ID here
InstanceType: t2.micro
KeyName: your-key-pair # your key name here
SecurityGroups:
- Ref: InstanceSecurityGroup
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow SSH
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Step 2: Deploy the Stack with the AWS CLI
Save the file. Now, open your terminal. Instead of going to the AWS Console to upload this template, we’ll use a single AWS CLI command to deploy it. This command tells CloudFormation to create a new “stack” (a managed unit of resources) based on our template.
aws cloudformation deploy \
--template-file template.yaml \
--stack-name my-first-ec2-stack
The CLI will package your template, upload it, and start the creation process. You’ll see updates right in your terminal.
Step 3: Check the Status
Once the command finishes, your stack is live! You can verify this in the AWS Console. Navigate to the CloudFormation service, and you will see your new stack, my-first-ec2-stack, with the status CREATE_COMPLETE.
It will look similar to the screenshot below, which shows a successful deployment of a different stack I created earlier. The key thing to look for is that green CREATE_COMPLETE status.
Zoom image will be displayed
A successful CREATE_COMPLETE status in the AWS CloudFormation console.
Step 4: Clean Up
This is the beauty of IaC. Tearing down your entire infrastructure is as simple as creating it. To delete everything we just built and avoid any costs, run this one command:
aws cloudformation delete-stack --stack-name my-first-ec2-stack
CloudFormation will now safely delete the EC2 instance and the security group for you.
Conclusion
You’ve just taken a massive step from being an AWS user to an AWS automator. You learned that:
The AWS CLI is your tool for direct commands and scripting.
CloudFormation is your tool for defining and managing the state of your infrastructure as code.
By combining them, you can build, update, and destroy entire environments with just a few lines of code. This is the foundation of modern cloud operations, DevOps, and building truly scalable systems.
So, the next time you find yourself clicking around the console, ask yourself: “Can I script this with the CLI or define it in CloudFormation?” The answer is almost always yes.
Thanks for reading! If you found this helpful, please clap, follow, and stay tuned for my next post, where we’ll dive into AWS Lambda and CloudWatch!
Subscribe to my newsletter
Read articles from Md Sharjil Alam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Md Sharjil Alam
Md Sharjil Alam
🚀 DevOps & Cloud Engineer | AWS | CI/CD | Terraform | Docker | Golang | Kubernetes I'm a DevOps & Cloud Engineer passionate about automating infrastructure and building reliable, scalable cloud systems. I bring hands-on experience with AWS services, CI/CD pipelines, and Infrastructure as Code to streamline software delivery and enhance operational efficiency. From writing backend logic in Golang to provisioning cloud infra with Terraform, and deploying Dockerized apps using Jenkins, I’ve worked across the stack to integrate development and operations seamlessly. 🔧 Core Skills: DevOps: Jenkins, GitHub Actions, Docker, Ansible, Terraform Cloud: AWS (EC2, S3, IAM, Lambda, Route 53, CloudWatch) IaC & Automation: Terraform, Ansible, Shell scripting Containerization & Orchestration: Docker, Kubernetes Backend Development: Golang, REST APIs, MySQL, MongoDB Frontend (for full-stack apps): ReactJS, JavaScript, Tailwind CSS Tools: Git, GitHub, Linux, VS Code 🛠️ Project Highlights: ⚙️ Built automated CI/CD pipelines with Jenkins, Docker, and GitHub Actions ☁️ Deployed and managed staging/production environments on AWS 🔧 Provisioned cloud infrastructure using Terraform and Ansible 🧠 Wrote backend APIs in Go and connected to full-stack apps 📊 Set up IAM roles, monitoring (CloudWatch), and cloud security best practices 📚 I share learnings and tutorials on Hashnode. 📩 Let’s connect: mdsharjil32@gmail.com