⚙️ DevOps Day 5 — AWS CLI & CloudFormation Deep Dive

 

🚀 Introduction

Welcome to Day 5 of our DevOps series!
In Day 4, we learned how to create virtual machines (EC2 instances) using different automation tools and APIs.
Today, we’ll go hands-on with the AWS Command Line Interface (CLI), explore how to connect to EC2 instances, and walk through AWS CloudFormation templates and drift detection — key skills for any DevOps engineer.



💻 1. AWS CLI Overview

The AWS Command Line Interface (CLI) lets you control AWS services directly from your terminal.
It’s powerful for automation and scripting.

🧩 Setup

Install and configure AWS CLI:

aws configure

Enter:

  • AWS Access Key ID

  • AWS Secret Access Key

  • Default region (e.g., ap-southeast-2)

  • Output format (e.g., json)

Verify:

aws sts get-caller-identity

🖥️ 2. Launching an EC2 Instance via CLI

Example command:

aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--count 1 \
--instance-type t2.micro \
--key-name my-keypair \
--security-group-ids sg-0123456789abcdef \
--subnet-id subnet-6e7f829e

This provisions a VM (EC2 instance) using the AWS API — fully scriptable and repeatable.


🔌 3. Connecting to an EC2 Instance

🔹 From the AWS Console (UI)

  1. Open the EC2 Dashboard

  2. Select your instance → Connect

  3. Choose EC2 Instance Connect (browser-based SSH)

  4. Click Connect to open a terminal session

🔹 From the Command Line

Make sure you have your private key file (.pem):

chmod 400 my-keypair.pem
ssh -i "my-keypair.pem" ec2-user@<public-ip>

✅ You’re now connected to your VM via terminal.


🧱 4. AWS CloudFormation Template Walk-Through

CloudFormation is AWS’s native IaC tool — it lets you model and provision resources using YAML or JSON.

Example template (ec2.yaml):

Resources:
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-0abcdef1234567890
InstanceType: t2.micro
KeyName: my-keypair

Deploy it:

aws cloudformation create-stack \
--stack-name MyStack \
--template-body file://ec2.yaml

CloudFormation automatically interacts with the AWS API to create and configure your instance.


🔍 5. Drift Detection

Over time, manual changes can cause your actual resources to differ from what’s defined in your template.
This is called configuration drift.

Detect drift:

aws cloudformation detect-stack-drift --stack-name MyStack
aws cloudformation describe-stack-drift-detection-status --stack-drift-detection-id <id>

View the results in the AWS Console → CloudFormation → Drift Detection.

✅ Helps maintain consistency and trust in IaC-managed infrastructure.


🧠 6. DevOps Takeaways

  • AWS CLI gives you scriptable control over cloud resources.

  • CloudFormation enables version-controlled infrastructure.

  • Drift Detection keeps real resources aligned with templates.

  • Combined, they allow you to automate infrastructure safely and consistently.


🌟 Conclusion

“The CLI and CloudFormation are the DevOps engineer’s command center — bridging manual operations and true automation.”

Comments

Popular posts from this blog

🧩 DevOps Day 1 — Fundamentals of DevOps

DevOps Day 23 — Multi-Stage Docker Builds & Distroless Images: Build Smaller, Safer Containers

🚀 DevOps Day 2 — Understanding the SDLC and the Role of DevOps Engineers