Ravindra BagaleCourses & study guides मराठी

How to Read This Course

DevOps

0.6 Lab safety checklist

Why: the problem

Every year students post the same sad story: "I left an EC2 instance running and got a bill of 8,000 rupees." Others push an AWS access key to a public Git repo by mistake. Bots scan public repos all day and can find such a key within minutes. Then someone runs crypto miners on that account. A lab should teach you, not cost you. Companies like Infosys and Accenture give new engineers strict rules for the same reason: budgets, least privilege, no secrets in code, and clean up after every test. In this lesson you will set up those guard rails on your own account before you start the heavy labs: a zero-spend budget, MFA on your login, a secret scanner for your Git repos, a .gitignore that keeps key files out, and a cleanup script that lists anything still running.

What we will build

You will make your lab safe in five steps: an AWS budget that emails you, MFA on your AWS login, a .gitignore for secrets, a gitleaks scan that catches a fake key before it is committed, and a cleanup script that lists running EC2 instances, unattached disks and Docker leftovers.

What: every term in plain English

  • Budget — An AWS setting that emails you when spending crosses a limit.
  • MFA — Multi-factor authentication. A code from your phone on top of the password.
  • Least privilege — Give only the permissions that are needed, nothing more.
  • Secret — A password, key or token. It must never be in code or Git.
  • .gitignore — A file that tells Git which files to never track.
  • gitleaks — A free tool that scans files and Git history for secrets.
  • Cleanup script — A script that lists or deletes lab resources so they stop costing money.
  • Region — A location of AWS data centers, like ap-south-1 (Mumbai). Resources live in one region.
Pick one region for all labs Create a monthly budget Check the budget Check MFA on your user Install gitleaks Scan your notes repo
Lab safety: budget, MFA, no secrets, cleanup

How: step by step

  1. Pick one region for all labs

    aws configure set region ap-south-1 && aws configure get region
    

    What it does: Using one region makes it easy to find and clean everything.

    Output

    ap-south-1
    
  2. Create a monthly budget

    aws budgets create-budget --account-id $(aws sts get-caller-identity --query Account --output text) --budget file://budget.json --notifications-with-subscribers file://notify.json
    

    What it does: Creates a 5 USD monthly budget that emails you at 80 percent.

    Output

    (no output means it worked)
    
  3. Check the budget

    aws budgets describe-budgets --account-id $(aws sts get-caller-identity --query Account --output text) --query "Budgets[].BudgetName"
    

    What it does: Lists your budgets.

    Output

    [
        "lab-budget"
    ]
    
  4. Check MFA on your user

    aws iam list-mfa-devices --user-name ravi-lab
    

    What it does: Shows your MFA device. Empty means MFA is not set.

    Output

    {
        "MFADevices": [
            {"UserName": "ravi-lab", "SerialNumber": "arn:aws:iam::111122223333:mfa/ravi-phone"}
        ]
    }
    
  5. Install gitleaks

    docker pull zricethezav/gitleaks:v8.18.4
    

    What it does: Gets the secret scanner as a Docker image, so nothing else needs installing.

    Output

    v8.18.4: Pulling from zricethezav/gitleaks
    Status: Downloaded newer image for zricethezav/gitleaks:v8.18.4
    
  6. Scan your notes repo

    docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:v8.18.4 detect --source /repo -v
    

    What it does: Looks for keys in all files and Git history.

    Output

    INF scan completed in 120ms
    INF no leaks found
    

Example 1: Budget files for a 5 USD monthly limit with an email alert

Stack. AWS Budgets, AWS CLI v2

Goal. Budget files for a 5 USD monthly limit with an email alert.

budget.json

{
  "BudgetName": "lab-budget",
  "BudgetLimit": {"Amount": "5", "Unit": "USD"},
  "TimeUnit": "MONTHLY",
  "BudgetType": "COST"
}

notify.json

[
  {
    "Notification": {
      "NotificationType": "ACTUAL",
      "ComparisonOperator": "GREATER_THAN",
      "Threshold": 80,
      "ThresholdType": "PERCENTAGE"
    },
    "Subscribers": [{"SubscriptionType": "EMAIL", "Address": "ravi@example.com"}]
  }
]

What each line does

  • BudgetLimit 5 USD — The monthly limit. Labs should stay far below it.
  • TimeUnit MONTHLY — The count starts again each month.
  • NotificationType ACTUAL — Alert on money already spent, not forecasts.
  • Threshold 80, PERCENTAGE — Email at 4 USD, before the limit.
  • Address — Your real email. Put your own here.

Green output

$ aws budgets describe-budgets --account-id 111122223333 --query "Budgets[].[BudgetName,BudgetLimit.Amount]"
[
    [
        "lab-budget",
        "5.0"
    ]
]

Expected result. AWS now emails you before a lab gets expensive. Many students wish they had done this on day one.

Example 2: Stop secrets before they reach Git:

Stack. gitleaks 8.18, Git

Goal. Stop secrets before they reach Git: .gitignore plus a gitleaks scan.

.gitignore

# keys and secrets
*.pem
*.key
.env
credentials
# local stuff
.venv/
*.log
terraform.tfstate*
.terraform/

terminal

cd ~/devops-lab/menu
cp ~/devops-lab/notes/.gitignore . 2>/dev/null || true
echo "AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" > config.txt
docker run --rm -v "$PWD:/repo" zricethezav/gitleaks:v8.18.4 detect --source /repo --no-git -v
rm config.txt

What each line does

  • *.pem / *.key — SSH and TLS private keys never go to Git.
  • .env — Files with app passwords stay local.
  • terraform.tfstate* — Terraform state can hold secrets; keep it in a safe backend instead.
  • the fake key line — Uses the official AWS example key, so it is safe to test.
  • detect --no-git — Scans the files in the folder, even if not committed.
  • rm config.txt — Delete the fake secret right away.

Green output

Finding:     AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
RuleID:      generic-api-key
File:        config.txt
Line:        1
WRN leaks found: 1

Expected result. gitleaks caught the key before any commit. Run it before every push, and later add it to your CI pipeline.

Example 3: A cleanup script that lists anything still running

Stack. AWS CLI v2, Bash, Docker

Goal. A cleanup script that lists anything still running.

lab-cleanup.sh

#!/usr/bin/env bash
set -uo pipefail
REGION="${AWS_REGION:-ap-south-1}"
echo "== Running EC2 in $REGION =="
aws ec2 describe-instances --region "$REGION" \
  --filters Name=instance-state-name,Values=running \
  --query "Reservations[].Instances[].[InstanceId,InstanceType,Tags[?Key=='Name']|[0].Value]" --output table
echo "== Unattached EBS volumes =="
aws ec2 describe-volumes --region "$REGION" --filters Name=status,Values=available \
  --query "Volumes[].[VolumeId,Size]" --output table
echo "== Elastic IPs not in use =="
aws ec2 describe-addresses --region "$REGION" --query "Addresses[?AssociationId==null].PublicIp" --output text
echo "== Docker leftovers =="
docker ps -a --format "{{.Names}}  {{.Status}}"
docker system df

terminal

chmod +x lab-cleanup.sh
./lab-cleanup.sh

What each line does

  • REGION=${AWS_REGION:-ap-south-1} — Uses your region, or Mumbai by default.
  • instance-state-name running — Only servers that are costing money now.
  • status available — Disks not attached to any server; they still cost money.
  • AssociationId==null — Elastic IPs not attached; AWS charges for them.
  • docker system df — How much disk Docker images and volumes use.

Green output

== Running EC2 in ap-south-1 ==
-------------------------------------------------
|               DescribeInstances               |
+----------------------+-----------+------------+
|  i-0abc12de34f56789a |  t3.micro |  lab-web   |
+----------------------+-----------+------------+
== Unattached EBS volumes ==
== Elastic IPs not in use ==
== Docker leftovers ==
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          4         0         412MB     412MB (100%)

Expected result. The script shows one t3.micro still running. Stop it with aws ec2 terminate-instances --instance-ids i-0abc12de34f56789a if you do not need it.

Verify: did it work?

  1. Confirm your identity is not root

    aws sts get-caller-identity --query Arn --output text
    

    What it does: The ARN must not end with :root.

    Output

    arn:aws:iam::111122223333:user/ravi-lab
    
  2. Run the cleanup check

    ./lab-cleanup.sh | head -5
    

    What it does: Run it at the end of every lab day.

    Output

    == Running EC2 in ap-south-1 ==
    

When it is red 1

An error occurred (AccessDeniedException) when calling the CreateBudget operation

Cause. Your IAM user has no permission for AWS Budgets.

Fix, step by step

  1. Ask the account owner, or sign in as admin once.
  2. Attach a policy that allows budgets:ModifyBudget and budgets:ViewBudget.
  3. Run the command again.

When it is red 2

WRN leaks found: 1

Cause. A secret-looking value is in your files or history.

Fix, step by step

  1. Remove the value from the file.
  2. If it was a real key, deactivate and rotate it in IAM right away.
  3. If it was committed, cleaning history is not enough; the key must be rotated.

When it is red 3

An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation

Cause. Your user cannot read EC2.

Fix, step by step

  1. Add the AmazonEC2ReadOnlyAccess policy to the lab user.
  2. Run the script again.

Cleanup

  1. Remove the gitleaks image if you want

    docker rmi zricethezav/gitleaks:v8.18.4
    

    What it does: Frees disk space. You can pull it again later.

    Output

    Untagged: zricethezav/gitleaks:v8.18.4
    
  2. Clean unused Docker data

    docker system prune -f
    

    What it does: Removes stopped containers, unused networks and dangling images.

    Output

    Total reclaimed space: 412MB
    
  3. Delete the practice folder

    rm -rf ~/devops-lab/ch0-practice
    

    What it does: Removes only the files you made in this lesson. Your other labs stay.

    Output

    (no output means it worked)
    

Ravindra Bagale's Tip

When the lab is done, run lab-cleanup.sh. If a key was pushed by mistake, rotate it at once. Set a budget today, not tomorrow!

Practice tasks

Easy Set the budget

Why: The single most important safety step.

What to do: Create the 5 USD budget with email alert.

How:
  1. Use the JSON files or the console.

Check: describe-budgets shows lab-budget.

Medium Pre-commit scan

Why: Catch secrets before they leave your laptop.

What to do: Run gitleaks on every repo in ~/devops-lab.

How:
  1. for d in ~/devops-lab/*/; do ...; done

Check: Every repo says no leaks found.

Hard Auto-stop lab servers

Why: Forgetting is human; automation is not.

What to do: Tag lab EC2 instances with auto-stop=yes and write a script that stops them.

How:
  1. aws ec2 describe-instances with a tag filter.
  2. aws ec2 stop-instances.

Check: Tagged instances stop when you run the script.

Easy Say Lab safety in two lines

Why: If you can say it simply, you understand it. Interviewers want the short version first.

What to do: Write two short lines about Lab safety for a friend who is new to IT.

How:
  1. Write what problem it solves.
  2. Write one command or file you used for it.
  3. Read it aloud.

Check: Your friend understands it without asking a question.

Easy Run the commands again from memory

Why: Typing from memory makes the commands stick. Copy-paste does not.

What to do: Close this page and run the main steps of this lesson again.

How:
  1. Open a fresh terminal.
  2. Type each command yourself.
  3. Open the page only when stuck.

Check: You got the same green output without copying.

Medium Break it on purpose

Why: You must know the red message by face. In a real job there is no time to read slowly.

What to do: Cause one error from the "When it is red" part of this lesson and fix it.

How:
  1. Follow one error section.
  2. Copy the exact message into your notes.
  3. Write the fix in one line.

Check: You fixed it using only your notes.

Medium Write a one-page note

Why: A short note in your own words is the best revision.

What to do: Write a note page for this lesson in ~/devops-lab/notes.md.

How:
  1. Title, why, the commands, one error and its fix.

Check: A classmate can follow your note alone.

Hard Explain it like an interview

Why: Interviewers want real stories, not definitions.

What to do: Record a 60 second answer about this lesson on your phone.

How:
  1. Say the problem, what you did, the command you ran, and one error you hit.

Check: Your answer has a real command and a real error message.

Hard Draw the flow top to bottom

Why: A drawing shows the order of things.

What to do: Draw this lesson as boxes from top to bottom on paper.

How:
  1. One box per step.
  2. Write the command next to each box.

Check: Each box matches a step you really ran.

Easy Teach it to someone

Why: Teaching shows the gaps in your own understanding.

What to do: Explain this lesson to a friend or family member in five minutes.

How:
  1. Use one real app as the example (Swiggy, Netflix, Paytm).

Check: They can tell you back the main idea.

Homework: try at home

Try at home

At home, set the budget, check MFA on your login, add the .gitignore to every lab repo and run lab-cleanup.sh. Put a reminder in your phone: run lab-cleanup.sh every evening after practice.

Recap

  • Use one region for labs and set a small budget with email alerts.
  • Turn on MFA and never use the root user.
  • Keep secrets out of Git with .gitignore and gitleaks.
  • Rotate any key that leaks, at once.
  • Run a cleanup check at the end of every lab day.

FAQ

Is the Free Tier enough for this course?

Mostly yes, if you stop servers after each lab. EKS, NAT Gateways and load balancers cost money, so delete them the same day.

What if I get a surprise bill?

Delete the resources, then open a case with AWS Support. They often help first-time mistakes.

Can I keep keys in a .env file?

Locally, yes, if .env is in .gitignore. In CI, use the tool's secret store or IAM roles.

Why not open file permissions to everyone to fix errors?

It lets anyone change the file. Fix the owner or use sudo for that one command.

How often should I run the cleanup script?

After every lab day.

Interview questions

  1. How do you prevent secrets from being committed?

    .gitignore, pre-commit scanners like gitleaks, secret stores like AWS Secrets Manager, and CI scanning.

  2. What do you do if an AWS key is leaked?

    Deactivate it at once, rotate it, check CloudTrail for misuse, and remove it from the code.

  3. What is least privilege?

    Giving each user or service only the permissions it needs.

  4. How do you control AWS costs in a team?

    Budgets and alerts, tags, cleanup automation, right-sizing and Savings Plans.

  5. Why use IAM roles instead of access keys?

    Roles give short-lived credentials that rotate automatically, so there is no long-lived key to leak.

Quick quiz

Q1. What tool emails you when AWS spend crosses a limit?

AWS Budgets.

Q2. Why use MFA?

A stolen password alone is not enough to log in.

Q3. Which file keeps .pem and .env out of Git?

.gitignore.

Q4. A real key was pushed to Git. Is deleting the commit enough?

No. Deactivate and rotate the key at once.

Q5. Which AWS things cost money even when not used?

Running EC2, unattached EBS volumes and unused Elastic IPs, among others.

Thodkyaat sangaycha tar

  • DevOps sits beside AWS: ship and run changes safely after the server exists.
  • Laptop + GitHub is enough to start; cloud VM comes later.
  • Read Why → What → How → What you see; steps stay vertical.
  • Tips and Labs are part of learning, not optional decoration.
  • Safety: no secrets in Git; stop idle labs; real-world suppose stories only.

Did you understand? Let us try it once more.