Day 2 – Networking IAM and Load Balancers
Today's Focus
Build out your cloud network: subnets, security groups, load balancers, and IAM roles with least privilege.
Tasks
- Extend your Terraform config with a private subnet (no direct internet access) alongside your public subnet. Add a NAT Gateway in the public subnet so instances in the private subnet can reach the internet for package installs.
- Define security groups as code: a
web-sgthat allows inbound80and443from0.0.0.0/0, and anapp-sgthat allows inbound on your app port only from theweb-sgCIDR. Deny all other inbound traffic. Confirm your rules in the AWS console afterterraform apply. - Create an IAM role for an EC2 instance (or Cloud Run service account) with the principle of least privilege: allow
s3:GetObjectands3:PutObjecton a specific bucket ARN, and nothing else. Attach the role to your compute resource. Verify the instance can read from S3 but is denieds3:DeleteObject. - Write an IAM policy document in Terraform using a
data "aws_iam_policy_document"block (not inline JSON). Explain why using data sources for policies is preferable tojsonencode()or raw JSON strings. - Provision an Application Load Balancer (ALB) in the public subnet. Create a target group and a listener on port 80 that forwards to the target group. Leave the targets empty for now — you will attach your app tomorrow.
- Add
outputs.tfthat outputs the ALB DNS name, VPC ID, and subnet IDs. Runterraform outputafter apply and use those values in the next task.