DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on • Edited on

Part 1: Decoupled Architecture

Students will SEE messages moving.


🎯 LAB: Event-Driven Image Processing Architecture

What Students Will Build

User β†’ ALB β†’ EC2 (Web Tier) β†’ S3
EC2 publishes event β†’ SNS β†’ SQS
Worker EC2 reads SQS β†’ processes file β†’ stores result in S3


🌐 Final Architecture

Image

Image

Image

Image


🟒 PHASE 1 β€” Create S3 Bucket

Step 1 β€” Open S3

  1. AWS Console β†’ search S3
  2. Click Create bucket

Step 2 β€” Configure

Bucket name:

student-upload-bucket-<yourname>
Enter fullscreen mode Exit fullscreen mode

Region:

us-east-2
Enter fullscreen mode Exit fullscreen mode

Uncheck:

Block all public access
Enter fullscreen mode Exit fullscreen mode

Check confirmation box.

Click:

Create bucket
Enter fullscreen mode Exit fullscreen mode

🟒 PHASE 2 β€” Create SNS Topic

  1. Search SNS
  2. Click Create topic
  3. Type: Standard
  4. Name:
file-upload-topic
Enter fullscreen mode Exit fullscreen mode
  1. Click Create topic
  2. Copy ARN β†’ save it

🟒 PHASE 3 β€” Create SQS Queue

  1. Search SQS
  2. Click Create queue
  3. Type: Standard
  4. Name:
file-processing-queue
Enter fullscreen mode Exit fullscreen mode
  1. Click Create queue
  2. Copy ARN

🟒 PHASE 4 β€” Subscribe SQS to SNS

  1. Go back to SNS
  2. Click topic: file-upload-topic
  3. Click Create subscription
  4. Protocol: Amazon SQS
  5. Endpoint: paste SQS ARN
  6. Click Create subscription

🟒 PHASE 5 β€” Allow SNS β†’ SQS

  1. Go to SQS β†’ file-processing-queue
  2. Click Edit access policy
  3. Choose Advanced
  4. Paste:
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Allow-SNS",
      "Effect": "Allow",
      "Principal": { "Service": "sns.amazonaws.com" },
      "Action": "sqs:SendMessage",
      "Resource": "YOUR_SQS_ARN",
      "Condition": {
        "ArnEquals": {
          "aws:SourceArn": "YOUR_SNS_ARN"
        }
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Replace ARNs.

Save.


🟒 PHASE 6 β€” Create Web EC2 (Publisher)

  1. Go to EC2
  2. Click Launch Instance

Name:

web-server
Enter fullscreen mode Exit fullscreen mode

AMI:

Amazon Linux 2
Enter fullscreen mode Exit fullscreen mode

Instance type:

t2.micro
Enter fullscreen mode Exit fullscreen mode

Create key pair.

Security Group:
Allow:

  • HTTP (80)
  • SSH (22)

Click Launch.


🟒 PHASE 7 β€” Install Web App

SSH into instance:

sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
sudo yum install aws-cli -y
Enter fullscreen mode Exit fullscreen mode

Create upload page:

sudo nano /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Paste:

<h1>Upload Simulation</h1>
<form method="POST" action="/upload">
<input type="text" name="filename" placeholder="Enter file name">
<button type="submit">Upload</button>
</form>
Enter fullscreen mode Exit fullscreen mode

Save.


🟒 PHASE 8 β€” Install Python Publisher Script

sudo yum install python3 -y
nano publisher.py
Enter fullscreen mode Exit fullscreen mode

Paste:

import boto3
sns = boto3.client('sns', region_name='us-east-2')

response = sns.publish(
    TopicArn='YOUR_TOPIC_ARN',
    Message='File uploaded: test-image.png'
)
print("Message sent")
Enter fullscreen mode Exit fullscreen mode

Replace ARN.

Run:

python3 publisher.py
Enter fullscreen mode Exit fullscreen mode

It will send event to SNS.


🟒 PHASE 9 β€” Create Worker EC2

Launch second instance:

Name:

worker-server
Enter fullscreen mode Exit fullscreen mode

Install Python + AWS CLI.


🟒 PHASE 10 β€” Worker Script (Consumer)

On worker:

nano worker.py
Enter fullscreen mode Exit fullscreen mode

Paste:

import boto3
import time

sqs = boto3.client('sqs', region_name='us-east-2')

queue_url = 'YOUR_QUEUE_URL'

while True:
    messages = sqs.receive_message(
        QueueUrl=queue_url,
        MaxNumberOfMessages=1,
        WaitTimeSeconds=5
    )

    if 'Messages' in messages:
        for message in messages['Messages']:
            print("Processing:", message['Body'])
            sqs.delete_message(
                QueueUrl=queue_url,
                ReceiptHandle=message['ReceiptHandle']
            )
    time.sleep(3)
Enter fullscreen mode Exit fullscreen mode

Replace Queue URL.

Run:

python3 worker.py
Enter fullscreen mode Exit fullscreen mode

Now it polls continuously.


🟒 PHASE 11 β€” Test Flow

  1. Run publisher on web server.
  2. Worker prints:
Processing: File uploaded: test-image.png
Enter fullscreen mode Exit fullscreen mode

Students will SEE event-driven processing live.


🟒 OPTIONAL β€” Add ALB + ASG

Create:

  • Target Group
  • Launch Template
  • Auto Scaling Group
  • Application Load Balancer

Attach web-server template to ASG.

Now traffic hits ALB.


πŸŽ“ What Students Learn

β€’ Decoupling architecture
β€’ Event-driven systems
β€’ Async processing
β€’ Worker scaling
β€’ Production DevOps pattern

Top comments (0)