Skip to content

AWS Compute Services — EC2, Lambda, Elastic Beanstalk, SAM

EC2 is your foundational compute service on AWS. It gives you resizable compute capacity with full control over instance configuration, storage, networking, and security.

Different instance types optimize for different workloads:

TypeFamilyBest For
General-purposet (burstable), m (standard)Web apps, small databases, mixed workloads
Compute-optimizedc (high-performance)Batch jobs, media transcoding, HPC
Memory-optimizedr (RAM-heavy), x (extreme)In-memory databases, real-time analytics
Storage-optimizedi, d, h (I/O intensive)NoSQL databases, data warehousing
Acceleratedp (GPU), f (FPGA)ML training, graphics rendering

The exam heavily tests instance types—know which family fits which use case.

Instances go through states: launch → running → stop (reversible) → start → terminate (final). Stopping an instance preserves the EBS root volume, but terminated instances are gone forever. Auto Scaling automatically adjusts instance count based on demand or schedules.

EBS provides persistent block storage attached to EC2 instances. Volume types include:

Volume TypeUse CaseDurability
gp2/gp3 (SSD)General workloads, databasesHighly durable
io1/io2 (IOPS SSD)High-speed databases, I/O intensiveVery high IOPS
st1 (HDD)Big data, sequential I/OGood throughput
sc1 (HDD)Infrequent access, archivalLow cost

Critical for the exam: EBS volumes are AZ-specific. You cannot attach a volume from us-east-1a to an instance in us-east-1b. This is a common gotcha.

Security Groups act as virtual firewalls, controlling inbound and outbound traffic at the instance level. Elastic IPs are static, publicly routable addresses that can be remapped between instances. Elastic Load Balancers (ALB, NLB, CLB) distribute traffic across multiple EC2 instances.

Lambda is fully serverless—AWS manages servers, scaling, and patching. You pay only for compute time consumed.

Lambda functions are event-driven and scale automatically. Triggers include S3, DynamoDB Streams, Kinesis, SNS, API Gateway, CloudWatch Events, and more. Execution happens in isolated execution environments.

Lambda concurrency is the number of functions executing simultaneously. AWS allocates one execution environment per concurrent request.

Default concurrency limits depend on account and region, but reserved/provisioned concurrency gives you explicit control.

A cold start occurs when AWS must create a new execution environment to run your function. Initialization overhead (Java can take 1-2 seconds; Python faster) impacts latency.

Cold start factors: Function size, language runtime, VPC attachment (worst), and code complexity. VPC-attached Lambda is particularly slow because it must attach an ENI to the function.

Max execution time is 15 minutes (900 seconds). Lambda is not suited for long-running processes—use Fargate or EC2 for batch jobs that exceed 15 minutes.

Elastic Beanstalk is a Platform as a Service (PaaS)—you upload code, AWS manages infrastructure (EC2, load balancing, auto-scaling, monitoring).

Upload your application code, choose a platform (Java, Python, Node.js, Go, Docker, etc.), and Beanstalk handles the rest. It provisions EC2 instances, applies auto-scaling policies, configures ELBs, and manages monitoring.

Environments are all resources Beanstalk creates: EC2 instances, Auto Scaling Group, Elastic Load Balancer, and RDS (optional).

.ebextensions YAML files (placed in .ebextensions/ at app root) customize Beanstalk environments. Common configs: install packages, set environment variables, tune OS parameters, or deploy custom code.

Beanstalk handles deployment orchestration, health monitoring, and rollback if deployment fails. It integrates with CodePipeline for CI/CD automation.

SAM is a framework and shorthand syntax for defining serverless applications. It’s built on CloudFormation.

A SAM template is a YAML file using simplified syntax for Lambda functions, API Gateway APIs, DynamoDB tables, and more. At deploy time, SAM transforms the template into standard CloudFormation.

StepCommandWhat Happens
Buildsam buildCompiles, installs deps, creates artifact in .aws-sam/
Packagesam packageCreates S3 bucket for artifacts (optional, implicit in deploy)
Deploysam deployTransforms to CloudFormation, deploys stack, creates resources

SAM CLI allows local testing and debugging without deploying to AWS. sam local start-api runs a local API Gateway mock. Requires Docker to run Lambda containers locally.

SAM enforces least privilege IAM policies, secure environment variable handling, and Infrastructure as Code principles. Every resource gets a logical ID, making it easy to reference and update.

Elasticity & Scalability: Design to leverage AWS’s elastic capacity. Use Auto Scaling, Lambda concurrency controls, and load balancers to handle variable traffic.

Security: Always use IAM roles (never raw access keys on EC2), enable Security Groups, encrypt EBS volumes, and implement least-privilege policies.

Cost Optimization: Use Spot Instances for fault-tolerant workloads, Reserved Instances for predictable baseline load, and Savings Plans for compute flexibility.

Monitoring & Logging: CloudWatch provides metrics, alarms, and logs. Set up dashboards and alarms for critical thresholds to catch issues early.

Infrastructure as Code: Use CloudFormation, SAM, or Terraform to codify and version your infrastructure. This enables reproducibility and disaster recovery.