SkarpSkarp

Chapter 12 of 26

Designing Stateless, Scalable Compute with Amazon EC2 and EC2 Instance Types

Choosing the right compute building blocks is central to both performance and cost. This module focuses on Amazon EC2, EC2 instance types, and patterns for stateless, horizontally scalable applications.

27 min readen

EC2 Fundamentals and the Instance Lifecycle

What is Amazon EC2?

Amazon EC2 provides resizable virtual servers called instances. You choose an AMI, instance type, networking, storage, and security settings, then run your applications on those instances.

EC2 Lifecycle States

Key EC2 states: pending (starting), running (active and billed), stopping/stopped (OS shut down, EBS root kept), shutting-down/terminated (instance destroyed, instance store data lost).

EBS vs Instance Store

EBS-backed instances can be stopped and restarted with their root volume preserved. Instance store–backed instances cannot be stopped; they are ephemeral and lose data when terminated.

Core Launch Choices

Launching EC2 requires selecting: AMI, instance type, key pair, security groups, and an IAM role. These determine the software image, capacity, access, and permissions of your instance.

EC2 Instance Families and Use-Case Matching

Why Instance Families Matter

Instance families are optimized for different workload patterns. Picking the right family gives better performance and lower cost than just “more vCPUs”.

General Purpose

General purpose (A, M, T) balance CPU, memory, and networking. Ideal for web and app servers, microservices, and small databases. Examples: t4g.micro, m7g.large.

Compute and Memory Optimized

Compute optimized (C) suit CPU-bound tasks like rendering or batch jobs. Memory optimized (R, X, z) suit in-memory databases, caching, and analytics workloads.

Storage and Accelerated

Storage optimized (I, D, H) provide high local storage IOPS/throughput. Accelerated (P, G, Trn, Inf) add GPUs or custom chips for ML, graphics, or HPC.

Workload Matching Patterns

Match patterns: web API → M/T; CPU-heavy batch → C; in-memory cache → R; SSD-heavy NoSQL → I; ML training → P/Trn. Expect these mappings on the exam.

Stateless vs Stateful Architectures on EC2

What is Stateless?

A stateless EC2 instance does not keep durable user or session data locally. Any request can go to any instance because state lives in external services like S3, DynamoDB, or ElastiCache.

Why Stateless Matters

Stateless tiers are easy to scale horizontally, easy to replace when unhealthy, and simplify multi-AZ or multi-Region designs. They align with reliability and performance efficiency goals.

Stateful Components

Stateful parts include databases on EBS, apps writing critical data to instance store, or in-memory sessions. These usually need vertical scaling or specialized patterns, not simple Auto Scaling.

Common Exam Trap

If user files or sessions are stored on the EC2 file system and the app must scale across AZs, the design is flawed. The fix is to move that state to S3, EFS, DynamoDB, or ElastiCache.

AMIs, User Data, and Configuration Management

What is an AMI?

An AMI is a template for EC2 that bundles the OS, packages, and optionally your app. Golden AMIs give you hardened, patched images for consistent, repeatable launches.

User Data Basics

User data is a script or cloud-init that runs at first boot. It typically installs the app, fetches config from SSM or S3, and registers monitoring agents.

Baked vs Bootstrapped

Baked AMIs contain most dependencies and code, so instances start quickly. Heavy bootstrapping in user data slows scaling and can cause timeouts under load.

Exam Guidance

If a question asks how to make Auto Scaling faster and more reliable, the preferred answer is usually to create a pre-configured AMI and minimize work done in user data.

Designing a Stateless Web Tier Across Multiple AZs

Scenario Overview

We design a public HTTPS web app that must survive AZ failure, scale with demand, and keep user images and sessions durable, using EC2 as the app tier.

Traffic Flow

Route 53 points your domain to an Application Load Balancer across at least two AZs. The ALB forwards traffic to EC2 instances in an Auto Scaling group across those AZs.

Making the Tier Stateless

EC2 instances use a baked AMI for app code. Sessions live in ElastiCache or DynamoDB, and user images go to S3. No durable data is kept on the instance file system.

Why It Scales and Survives Failures

If an AZ or instance fails, the ALB routes to healthy instances in other AZs. Auto Scaling replaces failed capacity, and no state is lost because it lives in external services.

Using User Data to Bootstrap a Stateless EC2 Instance

Here is a practical example of user data for a stateless web server. It installs a simple web app, pulls configuration from SSM Parameter Store, and serves static content from S3. In real environments you would polish security and error handling, but the pattern is exam-relevant.

```bash

#!/bin/bash

Update packages

yum update -y

Install AWS CLI and web server

yum install -y awscli httpd

Enable and start Apache

systemctl enable httpd

systemctl start httpd

Retrieve app config from SSM Parameter Store (requires IAM role)

APP_CONFIG=$(aws ssm get-parameter \

--name "/myapp/config" \

--with-decryption \

--query "Parameter.Value" \

--output text \

--region ${AWS_REGION})

echo "${APP_CONFIG}" > /var/www/html/config.json

Sync static assets from S3 bucket (stateless pattern)

aws s3 sync s3://myapp-static-assets/ /var/www/html/ \

--region ${AWS_REGION}

Simple health check page

echo "OK" > /var/www/html/health.html

```

Key exam takeaways:

  • User data often installs agents, pulls configuration, or fetches static assets.
  • Durable data (like user uploads) should still be written directly to S3 from the application, not stored on the EC2 root disk.
  • The instance must have an IAM role with permissions to read SSM parameters and S3 objects.

This approach keeps the instance replaceable. If Auto Scaling launches a new instance, it will run the same user data, pull the same configuration, and join the fleet with consistent behavior.

Scaling EC2 with Elastic Load Balancing and AWS Auto Scaling

Role of the Load Balancer

An Application Load Balancer routes HTTP/HTTPS traffic, performs health checks, and balances requests across EC2 instances in multiple AZs.

Role of the Auto Scaling Group

An Auto Scaling group uses a launch template, desired/min/max capacity, and scaling policies to maintain and adjust the size of your EC2 fleet.

ALB + ASG Integration

The ASG registers instances in an ALB target group. When instances fail health checks, the ASG can terminate and replace them automatically.

Multi-AZ Best Practice

Spread the ASG across at least two AZs, use a `/health` endpoint for ALB checks, and rely on ASG replacement for failed instances for high reliability.

Design Exercise: Fix the Non-Scalable Architecture

Consider this scenario and think through how you would redesign it using the principles in this module.

Current design (problematic)

  • A single `m5.large` EC2 instance in one AZ runs a PHP web app.
  • Users upload profile pictures; the app stores them on `/var/www/uploads`.
  • PHP sessions are stored in the local file system.
  • The instance uses a public IP, and Route 53 points directly to it.
  • During traffic spikes, the instance CPU reaches 100%, and the site becomes slow or unavailable.
  • When the instance is replaced for patching, users lose their sessions and sometimes their images.

Your task

  1. Identify at least three design flaws in the current setup.
  2. Sketch a new design that:
  • Uses a stateless EC2 tier.
  • Survives the loss of one AZ.
  • Scales with traffic.
  1. For each change, explain briefly which AWS service you would use and why.

Hints

  • Think about where sessions and uploaded files should live.
  • Think about how traffic should reach your app.
  • Think about how new instances should be configured.

Pause for a minute and write down your answers. Then compare mentally with this outline:

  • Use an ALB + multi-AZ Auto Scaling group of smaller instances.
  • Store files in S3 and sessions in ElastiCache or DynamoDB.
  • Use an AMI and user data for consistent configuration.

On the actual exam, you will often need to “fix” a similar design by moving state out of EC2 and introducing ELB + Auto Scaling.

Quiz 1: Instance Families and Stateless Design

Test your understanding of instance families and stateless patterns.

You are designing a video transcoding service that processes large batches of files from S3. Jobs are CPU-intensive but can be split across many workers. Which EC2 instance family and pattern is the BEST fit?

  1. Use memory optimized (R family) instances with a single large instance to reduce coordination overhead.
  2. Use compute optimized (C family) instances in an Auto Scaling group behind an ALB, pulling jobs from an SQS queue.
  3. Use general purpose (M family) instances with instance store to keep intermediate files local and avoid S3.
  4. Use storage optimized (I family) instances with EBS volumes to store all input and output files locally.
Show Answer

Answer: B) Use compute optimized (C family) instances in an Auto Scaling group behind an ALB, pulling jobs from an SQS queue.

CPU-intensive, parallelizable workloads are a classic fit for compute optimized (C family) instances. Using an Auto Scaling group of workers that pull jobs from SQS allows horizontal scaling and stateless workers. Memory optimized (R) is unnecessary here, and relying on local storage (instance store or EBS) for input/output instead of S3 reduces durability and scalability.

Quiz 2: AMIs, User Data, and Auto Scaling

Check how well you understand configuration patterns for EC2 fleets.

Your Auto Scaling group sometimes launches new EC2 instances that take 15 minutes to become healthy because user data installs many packages and compiles code at boot. During sudden traffic spikes, requests fail. What is the MOST effective improvement?

  1. Increase the instance type size so compilation finishes faster.
  2. Configure the ALB health check timeout to 20 minutes.
  3. Create a pre-baked AMI with the application and dependencies installed, and simplify user data.
  4. Disable health checks so the ALB sends traffic immediately to new instances.
Show Answer

Answer: C) Create a pre-baked AMI with the application and dependencies installed, and simplify user data.

The best practice is to bake application code and dependencies into an AMI so that new instances come up quickly and predictably. User data should perform minimal configuration. Larger instance types or longer health checks do not solve the underlying unpredictability, and disabling health checks harms reliability.

Key Term Review: EC2 and Stateless Design

Flip through these cards to reinforce core concepts from this module.

Amazon EC2
A web service that provides resizable compute capacity in the cloud in the form of virtual servers called instances.
Amazon Machine Image (AMI)
A template that contains a software configuration (operating system, application server, and applications) used to launch EC2 instances in a known-good state.
Stateless application tier
An EC2-based application layer where no durable user or session data is stored on the instance; all state is stored in external services such as S3, DynamoDB, or ElastiCache.
User data
A script or cloud-init directives that run when an EC2 instance first boots, commonly used to install software, pull configuration, or register with other systems.
General purpose instance family
Instance families (such as A, M, T) that provide a balance of compute, memory, and networking resources suitable for a broad range of workloads.
Compute optimized instance family
Instance family (C) designed for compute-bound applications that benefit from high-performance processors, such as batch processing and high-performance web servers.
Memory optimized instance family
Instance families (R, X, z) designed to deliver fast performance for workloads that process large data sets in memory, such as in-memory databases and caching.
Storage optimized instance family
Instance families (I, D, H) designed for workloads that require high, sequential read and write access to very large data sets on local storage.
Accelerated computing instances
Instance families (such as P, G, Trn, Inf) that use hardware accelerators like GPUs or custom chips for tasks such as machine learning, graphics, or HPC.
Auto Scaling group (ASG)
A logical group of EC2 instances managed together, which can scale the number of instances in or out based on defined policies and health checks.
Application Load Balancer (ALB)
A Layer 7 load balancer that routes HTTP/HTTPS traffic based on content (path, host, headers) and distributes it across targets such as EC2 instances in multiple AZs.

Cost, Performance, and Exam Strategy for EC2 Designs

Performance and Cost Pillars

Performance efficiency and cost optimization pillars guide EC2 choices: pick the right family/size and use Auto Scaling to match capacity to demand.

Cost-Savvy EC2 Patterns

Use smaller, well-matched instance types, Auto Scaling, and Spot Instances for stateless or batch workloads to reduce cost without harming reliability.

Common Exam Moves

Typical fixes: move state out of EC2, add ALB + multi-AZ ASG, bake AMIs, simplify user data, and choose the correct instance family for CPU, memory, or storage needs.

Your Next Step

Be sure you can sketch a stateless EC2 web tier from memory and justify each component. The upcoming diagnostic and mock exams will reveal any gaps.

Key Terms

User data
A script or cloud-init directives that run when an EC2 instance first boots, commonly used to install software, pull configuration, or register with other systems.
Amazon EC2
A web service that provides resizable compute capacity in the cloud in the form of virtual servers called instances.
EC2 instance
A virtual server in Amazon's Elastic Compute Cloud used to run applications on the AWS cloud.
Spot Instance
An EC2 instance that uses spare AWS capacity at a discounted price and can be interrupted by AWS with short notice, suitable for fault-tolerant and flexible workloads.
Instance family
A group of EC2 instance types that share similar characteristics and are optimized for specific types of workloads, such as general purpose, compute optimized, or memory optimized.
Stateful application
An application design where user or session data is stored on the local compute node or tightly coupled storage, making scaling and replacement more complex.
Stateless application
An application design where no durable user or session data is stored on the compute node; all state is kept in external services, allowing any node to handle any request.
Auto Scaling group (ASG)
A logical group of EC2 instances managed together, which can scale the number of instances in or out based on defined policies and health checks.
Amazon Machine Image (AMI)
A template that contains a software configuration (operating system, application server, and applications) used to launch EC2 instances in a known-good state.
Elastic Load Balancing (ELB)
A service that automatically distributes incoming application traffic across multiple targets, such as EC2 instances, in one or more Availability Zones.
Application Load Balancer (ALB)
A Layer 7 load balancer that routes HTTP/HTTPS traffic based on content (path, host, headers) and distributes it across targets such as EC2 instances in multiple AZs.

Finished reading?

Test your understanding with a custom practice exam on this chapter.

Test yourself