Vertical & Horizontal Scaling

System Scaling
Vertical Scaling
Horizontal Scaling
Horizontal vs Vertical Scaling – Key Comparison

Advanced EC2

EC2 Bootstrapping with User Data

Bootstrapping Concepts
EC2 User Data
EC2 Bootstrapping – Architecture
  1. An EC2 instance is launched with its boot volume attached
  2. User Data is provided to the instance at launch
  3. The operating system checks for the presence of User Data
  4. If present, it is executed as a startup script
  5. The instance remains in a running state during execution
    • If the script succeeds → instance becomes service-ready
    • If the script fails → instance may run but be incorrectly configured
    • Instance health checks may pass even if setup is still ongoing, so “running” does not always mean “ready”
EC2 Bootstrapping – Boot-Time-To-Service-Time

Enhanced EC2 Bootstrapping with CFN-INIT

CFN-INIT – Key Concepts
CFN-INIT – Architecture
  1. CFN-INIT is triggered through UserData provided to the instance
    • Example: /opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource EC2Instance --configsets wordpress_install --region ${AWS::Region}
  2. It retrieves configuration details from the CloudFormation template
    • Found under the Metadata section → AWS::CloudFormation::Init
  3. It applies the configuration to move the instance toward the defined desired state
    • Works with stack updates
      • Unlike User Data (which runs only once), CFN-INIT can reapply configuration whenever the stack is updated
      • This allows ongoing configuration management after launch
CFN CreationPolicy and CFN-SIGNAL

DEMO: Bootstrapping EC2 WordPress Installation

Configuring Bootstrap Scripts in EC2 User Data

Diagnosing Problems with EC2 Bootstrap Scripts

  1. User Data can always be retrieved from the Instance Metadata Endpoint
    • For newer AMIs (e.g., Amazon Linux 2023), a token may be required before accessing it.
  1. Log files are located in /var/log and provide execution details:
    • cloud-init-output.log → includes executed commands and their output
    • cloud-init.log → includes only the commands executed during boot
Bootstrapping WordPress with CFN-INIT
Diagnosing Problems with CFN-INIT

EC2 Instance Roles & InstanceProfile

EC2 Instance Roles – Architecture

EC2 Instance Profile

Credential Precedence for AWS CLI

SSM Parameter Store

SSM Parameter Store – Key Concepts
SSM Parameter Store Tiers
SSM Parameter Store TierNumber of ParametersParameter Value SizeParameter Policies AvailableCost
StandardUp to 10,000Up to 4 KBNoFree*
AdvancedNo limitUp to 8 KBYesPaid

*Additional charges may apply for higher throughput usage.

SSM Parameter Store – Characteristics
Useful CLI Commands for Retrieving Parameters

System and Application Logging on EC2

CloudWatch Logs for EC2
Demo: CloudWatch Agent Setup for WordPress EC2 Instance
  1. Create and configure an IAM role
    • Role type: EC2
    • Attach required managed policies:
      • CloudWatchAgentServerPolicy
      • AmazonSSMFullAccess
  2. Install the CloudWatch Agent
    • Command: sudo dnf install amazon-cloudwatch-agent
  3. Run the configuration wizard
    • Command:
      sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
    • Accept most defaults, but choose advanced metrics when prompted
    • Define log files to collect, such as:
      • /var/log/secure → authentication and security logs
      • /var/log/httpd/access_log → Apache access activity
      • /var/log/httpd/error_log → Apache error events
  4. Save the configuration
    • Stored locally at:
      /opt/aws/amazon-cloudwatch-agent/bin/config.json
    • Optionally store it in SSM Parameter Store for reuse
  5. Prepare required directories and files
    • Some Linux instances do not include required paths by default
    • Create them manually:
      • sudo mkdir -p /usr/share/collectd/
      • sudo touch /usr/share/collectd/types.db
  6. Start the CloudWatch Agent
    • Command:
      sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c ssm:AmazonCloudWatch-linux -s
    • This command retrieves the configuration from SSM and starts the agent

EC2 Placement Groups

EC2 Placement Groups – Overview
Cluster Placement Groups
Spread Placement Groups
Partition Placement Groups
EC2 Placement Groups – Summary Table
Feature / TypeCluster Placement GroupSpread Placement GroupPartition Placement Group
Placement StrategyInstances placed very close togetherInstances placed on separate hardwareInstances grouped into isolated partitions
Availability ZonesSingle AZ onlyCan span multiple AZsCan span multiple AZs
PerformanceVery high (low latency, high BW)StandardHigh within each partition
Fault ToleranceLowVery highHigh (isolated per partition)
Instance LimitNo fixed limit (capacity dependent)7 instances per AZ7 partitions per AZ (many instances each)
Hardware SharingOften sharedFully isolatedShared within partition only
Best Use CasesHPC, tightly coupled workloadsCritical isolated systemsLarge distributed, topology-aware systems
ComplexityLowLowHigher (requires planning)

EC2 Dedicated Hosts

CPU Sockets and Cores
EC2 Dedicated Hosts – Key Concepts & Overview

Types of Dedicated Hosts

Traditional Dedicated Hosts

Nitro-Based Dedicated Hosts

EC2 Dedicated Hosts – Considerations & Limitations

EC2 Enhanced Networking & EBS-Optimized Instances

EC2 Enhanced Networking
EBS-Optimized Instances

Containers & ECS

Containerization 101

OS Virtualization Problems
Containerization (Container Virtualization)
Image Anatomy
Container Anatomy
Container Registry

Amazon ECS (Elastic Container Service) 101

Amazon ECS – Key Concepts
Amazon ECS – Definitions

Container Definition

Task Definition

Service Definition

ECS Cluster Modes

Amazon ECS – EC2 Mode
Amazon ECS – Fargate Mode
ECS Cluster Modes – Comparison
ECS Cluster ModeContainer Host LocationContainer Host ManagementBilling
EC2EC2 instancesCustomerPay for full instances, regardless of container usage
FargateAWS-managed platformAWS (Fargate)Pay only for resources used by running tasks
Choosing Between EC2, ECS-EC2, and ECS-Fargate

DEMO: Build, Register, and Deploy a Docker Container Image on AWS

Running Docker on an EC2 Instance (Amazon Linux 2)
  1. Launch a t2.micro EC2 instance using Amazon Linux 2, then connect to it once it is running
  2. Install Docker on the instance sudo dnf install docker
    • DNF is a package manager used to install, update, and remove software packages on modern Linux distributions (successor to YUM)
  3. Start the Docker service sudo service docker start
  4. Verify Docker is running by listing containers docker ps
    • This will initially return a permission error because the current user is not allowed to interact with Docker
  5. Grant Docker permissions to the default user sudo usermod -a -G docker ec2-user
    • Adds ec2-user to the Docker group, allowing interaction with the Docker Engine
  6. Log out of the instance and reconnect
    • Required for group membership changes to take effect
  7. Switch to the ec2-user (if needed) sudo su – ec2-user
    • Necessary when using Session Manager instead of SSH or Instance Connect
  8. Run the verification command again docker ps
    • Should now execute without errors (no containers will be listed initially)
STEP 2: Building the “Container of Cats” Docker Image
STEP 3: Deploying “Container of Cats” with ECS Fargate
  1. Create an ECS cluster and select Fargate as the launch type
  2. Define a task
    • Add a container definition
    • Provide the image URI from Docker Hub (created in Step 2)
  3. Run the task in the ECS cluster
    • Choose a VPC where the task will be deployed

Container Image Registry (Amazon ECR)

Amazon ECR – Key Concepts
Amazon ECR – Benefits

Kubernetes Basics 101

Kubernetes (K8s) Concepts

Core Components

Kubernetes Cluster Structure

Node Components

Control Plane Components

Kubernetes Architecture Diagrams

Amazon EKS (Elastic Kubernetes Service) Basics

Amazon EKS – Key Concepts
Amazon EKS – Architecture