Skip to content
This repository was archived by the owner on Aug 10, 2026. It is now read-only.

Commit 2760eb1

Browse files
committed
Added resource tagging into terraform
1 parent f238bbc commit 2760eb1

11 files changed

Lines changed: 113 additions & 24 deletions

File tree

‎README.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,46 @@ projects/ # Capstone and assignment projects
9797

9898
---
9999

100+
## Resource Naming & Tagging — required convention
101+
102+
When you run a shared AWS account with multiple students, the only thing keeping your cohort organized (and cleanup-able) is consistent naming and tagging.
103+
104+
### Naming
105+
106+
Every AWS resource you create — in Console or via Terraform — must start with:
107+
108+
```
109+
student-<your-slug>-
110+
```
111+
112+
Your slug is the lowercase-hyphenated version of your name that the cohort admin assigned (e.g. `alice-johnson`, `bilal-joud`). It matches the prefix of your IAM username and the value you pass as `student_name=` in any project's Terraform.
113+
114+
The IAM policy enforces this for S3, Lambda, DynamoDB, CloudWatch Logs, and IAM roles — anything you try to create outside your namespace will be denied.
115+
116+
### Tagging
117+
118+
Two tags on every resource you create:
119+
120+
| Key | Value | Example |
121+
|---|---|---|
122+
| `student` | your slug | `alice-johnson` |
123+
| `cohort` | the cohort name your admin gave you | `fullstack-aws-batch-a` |
124+
125+
**For Terraform-managed resources**: every project's `provider "aws"` block uses `default_tags`, so this is automatic — you only supply `student_name=` and `cohort=` as variables. Nothing else to do.
126+
127+
**For Console-created resources**: add both tags manually in the "Tags" step of the Create wizard. Most Console flows surface this step before the final "Create" button. If you skip it, the resource still works, but the cohort cleanup tool may miss it (and that often translates to "your instructor pings you to delete it manually before billing closes").
128+
129+
### Why this matters
130+
131+
The instructor runs `tools/cleanup-student-resources.py` at the end of the cohort to remove everything. It finds your resources two ways:
132+
133+
1. **By name** — anything matching `student-<your-slug>-*`
134+
2. **By tag** — anything tagged `student=<your-slug>`
135+
136+
Either one finds you. Both is belt-and-suspenders. Neither leaves you on the hook for a forgotten Lambda still racking up free-tier invocations next month.
137+
138+
---
139+
100140
## About beCloudReady
101141

102142
beCloudReady bridges the gap between ambitious talent and fast-moving startups — practical training, real projects, no fluff.

‎chapters/06-cloud-cicd/terraform/main.tf‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ terraform {
1717

1818
provider "aws" {
1919
region = var.aws_region
20+
default_tags {
21+
tags = {
22+
student = var.student_name
23+
cohort = var.cohort
24+
}
25+
}
26+
}
27+
28+
locals {
29+
# All resources prefixed student-<slug>-<project>- so they fit the IAM
30+
# namespace policy and are pickable by the cleanup script.
31+
name = "student-${var.student_name}-${var.project_name}"
2032
}
2133

2234
# ─────────────────────────────────────────────
@@ -31,14 +43,14 @@ resource "tls_private_key" "ec2_key" {
3143

3244
# Register public key with AWS
3345
resource "aws_key_pair" "ec2_key" {
34-
key_name = "${var.project_name}-key"
46+
key_name = "${local.name}-key"
3547
public_key = tls_private_key.ec2_key.public_key_openssh
3648
}
3749

3850
# Save private key to ssh-keys/ folder with correct permissions
3951
resource "local_sensitive_file" "private_key" {
4052
content = tls_private_key.ec2_key.private_key_pem
41-
filename = "${path.module}/ssh-keys/${var.project_name}-key.pem"
53+
filename = "${path.module}/ssh-keys/${local.name}-key.pem"
4254
file_permission = "0400"
4355
}
4456

@@ -47,8 +59,8 @@ resource "local_sensitive_file" "private_key" {
4759
# ─────────────────────────────────────────────
4860

4961
resource "aws_security_group" "ec2" {
50-
name = "${var.project_name}-sg"
51-
description = "Security group for ${var.project_name} EC2 instance"
62+
name = "${local.name}-sg"
63+
description = "Security group for ${local.name} EC2 instance"
5264

5365
# SSH — for deployment and management
5466
ingress {
@@ -96,8 +108,8 @@ resource "aws_security_group" "ec2" {
96108
}
97109

98110
tags = {
99-
Name = "${var.project_name}-sg"
100-
Project = var.project_name
111+
Name = "${local.name}-sg"
112+
Project = local.name
101113
}
102114
}
103115

@@ -161,7 +173,7 @@ resource "aws_instance" "app" {
161173
EOF
162174

163175
tags = {
164-
Name = "${var.project_name}-server"
165-
Project = var.project_name
176+
Name = "${local.name}-server"
177+
Project = local.name
166178
}
167179
}

‎chapters/06-cloud-cicd/terraform/variables.tf‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,25 @@ variable "aws_region" {
44
default = "us-east-1"
55
}
66

7+
variable "student_name" {
8+
description = "Your slug (lowercase letters, numbers, hyphens). Same value as your IAM user's slug tag and what other projects use as student_name."
9+
type = string
10+
validation {
11+
condition = can(regex("^[a-z0-9-]+$", var.student_name))
12+
error_message = "student_name must contain only lowercase letters, numbers, and hyphens."
13+
}
14+
}
15+
16+
variable "cohort" {
17+
description = "Cohort identifier (e.g. fullstack-aws-batch-a). Tagged on every resource for cohort-scoped cleanup."
18+
type = string
19+
default = "fullstack-aws"
20+
}
21+
722
variable "project_name" {
8-
description = "Used for naming all resources"
23+
description = "Suffix combined with the student slug to form resource names: student-<student_name>-<project_name>-*"
924
type = string
10-
default = "student-employee-app"
25+
default = "employee-app"
1126
}
1227

1328
variable "instance_type" {

‎projects/01-task-tracker/terraform/main.tf‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ terraform {
77

88
provider "aws" {
99
region = var.aws_region
10+
default_tags {
11+
tags = {
12+
student = var.student_name
13+
cohort = var.cohort
14+
}
15+
}
1016
}
1117

1218
resource "random_id" "suffix" {

‎projects/01-task-tracker/terraform/variables.tf‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ variable "aws_region" {
1919
variable "mongo_host" {
2020
description = "EC2 public IP running MongoDB"
2121
}
22+
23+
variable "cohort" {
24+
description = "Cohort identifier (e.g. fullstack-aws-batch-a). Tagged on every resource for cohort-scoped cleanup."
25+
type = string
26+
default = "fullstack-aws"
27+
}

‎projects/03-url-bookmark-saver/README.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,18 @@ The workflow prefers OIDC when `URL_BOOKMARK_SAVER_AWS_ROLE_ARN` is set. Otherwi
138138
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
139139
140140
Frontend (open this in your browser):
141-
http://stash-john-smith-frontend-123456789012.s3-website-us-east-1.amazonaws.com
141+
http://student-john-smith-frontend-123456789012.s3-website-us-east-1.amazonaws.com
142142
143143
API endpoints:
144144
GET https://abc123.execute-api.us-east-1.amazonaws.com/bookmarks
145145
POST https://abc123.execute-api.us-east-1.amazonaws.com/bookmarks
146146
DELETE https://abc123.execute-api.us-east-1.amazonaws.com/bookmarks/{id}
147147
148148
AWS resources:
149-
Lambda : stash-john-smith-api
150-
DynamoDB : stash-john-smith-bookmarks
151-
S3 bucket : stash-john-smith-frontend-123456789012
152-
Dashboard : https://us-east-1.console.aws.amazon.com/cloudwatch/home#dashboards:name=stash-john-smith-dashboard
149+
Lambda : student-john-smith-api
150+
DynamoDB : student-john-smith-bookmarks
151+
S3 bucket : student-john-smith-frontend-123456789012
152+
Dashboard : https://us-east-1.console.aws.amazon.com/cloudwatch/home#dashboards:name=student-john-smith-dashboard
153153
```
154154

155155
---
@@ -314,7 +314,7 @@ Every Lambda invocation emits a structured JSON log line:
314314

315315
View logs:
316316
```bash
317-
aws logs tail /aws/lambda/stash-<name>-api --follow --region us-east-1
317+
aws logs tail /aws/lambda/student-<name>-api --follow --region us-east-1
318318
```
319319

320320
---

‎projects/03-url-bookmark-saver/terraform/provider.tf‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ provider "aws" {
1616
region = var.aws_region
1717
default_tags {
1818
tags = {
19-
Project = "stash"
20-
Student = var.student_name
21-
ManagedBy = "terraform"
19+
student = var.student_name
20+
cohort = var.cohort
2221
}
2322
}
2423
}

‎projects/03-url-bookmark-saver/terraform/variables.tf‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ variable "log_retention_days" {
3131
default = 14
3232
}
3333

34+
variable "cohort" {
35+
description = "Cohort identifier (e.g. fullstack-aws-batch-a). Tagged on every resource for cohort-scoped cleanup."
36+
type = string
37+
default = "fullstack-aws"
38+
}
39+
3440
locals {
35-
prefix = "stash-${var.student_name}"
41+
prefix = "student-${var.student_name}"
3642
account_id = data.aws_caller_identity.current.account_id
3743
}

‎projects/04-architecture-diagram/terraform/main.tf‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
locals {
2-
prefix = "arcflow-${var.student_name}"
2+
prefix = "student-${var.student_name}"
33
account_id = data.aws_caller_identity.current.account_id
44
}
55

‎projects/04-architecture-diagram/terraform/provider.tf‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ provider "aws" {
1212
region = var.aws_region
1313
default_tags {
1414
tags = {
15-
Project = "arcflow"
16-
Student = var.student_name
17-
ManagedBy = "terraform"
15+
student = var.student_name
16+
cohort = var.cohort
1817
}
1918
}
2019
}

0 commit comments

Comments
 (0)