π©οΈ Day 24 of 30 Days DevOps Interview Preparation Challenge

Series: 30 Days DevOps Interview Preparation
Author: Tathagat Gaikwad
Topic: Add S3 + IAM Role via Terraform
π Introduction
Welcome to Day 24 of the 30 Days DevOps Interview Challenge!
Today, we are focusing on provisioning AWS S3 Buckets and IAM Roles with Terraform.
Why is this important?
S3 is the backbone of many AWS services (storage, logs, static sites, backups).
IAM Roles + Policies are critical for secure access management.
As a DevOps engineer, you must automate provisioning using Terraform instead of manual console work.
In this blog, we will:
β
Learn theory of S3 & IAM
β
Understand interview Q&A
β
Write Terraform code inside VS Code
β
Apply security best practices
π Theory
π What is AWS S3?
Object storage service.
Stores files, logs, backups, static websites.
Data is stored in Buckets.
Security: encryption, versioning, access policies.
π What is IAM Role?
An identity with permissions, but without permanent credentials.
AWS services (like EC2, Lambda) assume roles to access other services securely.
Roles are preferred over IAM users (no hardcoded keys, temporary tokens).
π IAM Policy
A JSON document defining permissions (
Allow
orDeny
).Example: An EC2 Role with S3 read-only permissions.
π Why Terraform for S3 + IAM?
Infrastructure as Code (IaC).
Version control + consistency.
Easier rollback + security enforcement.
π‘ Interview Q&A
Q1. Why prefer IAM Role over Access Keys?
π Roles provide temporary credentials, automatically rotated by AWS. No risk of key leaks.
Q2. How to secure S3 buckets?
π Disable public access, enable encryption (AES-256 or KMS), enable versioning, and apply least privilege IAM policies.
Q3. What happens if you attach AdministratorAccess
to an IAM Role?
π It grants full AWS account permissions β which is against least privilege. Instead, create fine-grained policies.
Q4. How does Terraform manage IAM changes?
π Terraform state tracks resources. If policy changes, Terraform updates IAM roles automatically.
π οΈ Practical: Provision S3 + IAM Role with Terraform (using VS Code)
β Prerequisites
AWS Account
IAM User with programmatic access
Terraform installed (Guide for Windows/Linux)
VS Code editor with Terraform extension
Step 1: Open VS Code
Create a new project folder:
mkdir terraform-day24-s3-iam
cd terraform-day24-s3-iam
Step 2: Provider Configuration
Create main.tf
in VS Code:
provider "aws" {
region = "us-east-1"
}
Step 3: Create S3 Bucket
resource "aws_s3_bucket" "devops_bucket" {
bucket = "devops-challenge-day24-bucket"
tags = {
Name = "DevOps-Day24"
}
}
Step 4: IAM Role + Policy
# IAM Role
resource "aws_iam_role" "ec2_role" {
name = "ec2-s3-access-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "ec2.amazonaws.com" }
Action = "sts:AssumeRole"
}]
})
}
# IAM Policy for S3 Read-Only
resource "aws_iam_policy" "s3_read_only" {
name = "S3ReadOnlyPolicy"
description = "Allow read access to S3 bucket"
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = ["s3:GetObject"]
Effect = "Allow"
Resource = "${aws_s3_bucket.devops_bucket.arn}/*"
}]
})
}
# Attach Policy to Role
resource "aws_iam_role_policy_attachment" "attach" {
role = aws_iam_role.ec2_role.name
policy_arn = aws_iam_policy.s3_read_only.arn
}
Step 5: Initialize + Apply
Run in terminal:
terraform init
terraform plan
terraform apply -auto-approve
Step 6: Verify
Go to AWS Console > S3 β Check bucket created.
Go to IAM > Roles β Check role with attached policy.
π Security Best Practices
β Enable S3 bucket encryption (AES-256 or KMS).
β Avoid public bucket access unless explicitly required.
β Use IAM Roles instead of hardcoding credentials.
β Follow least privilege principle when writing policies.
β Always version-control Terraform code in GitHub.
π Key Takeaways
Terraform makes S3 + IAM provisioning automated & consistent.
Use IAM Roles over Users for security.
Policies should always be least privilege.
DevOps Interview Tip: Be ready to write or explain Terraform IAM/S3 code.
π Whatβs your approach to securing S3 + IAM policies in production β Manual audit, Terraform, or AWS Config? Drop your thoughts!
#DevOps #AWS #Terraform #S3 #IAM #Cloud #InterviewPreparation #30DaysOfDevOps
Subscribe to my newsletter
Read articles from Tathagat Gaikwad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
