Lesson content
Read, practise, then check your understanding
Compute: EC2 and Lambda
Amazon EC2 provides resizable virtual machines with control over operating systems, instance families, storage, networking, and placement. AWS Lambda runs short-lived event-driven code without server management. The right choice follows workload shape, control needs, execution duration, latency, and operating model.
Core ideas
- An AMI defines the starting operating-system and software image for an EC2 instance.
- An Auto Scaling group maintains desired capacity and can scale from demand or schedules.
- EC2 instance profiles supply temporary IAM credentials without embedding access keys.
- Lambda scales execution environments from events and charges primarily by requests and execution resources.
Design and operating model
Use EC2 for long-running services, specialized machine types, custom agents, or deep OS control. Use Lambda for event handlers, lightweight APIs, scheduled tasks, and bursty integration. For both, keep instances replaceable, place code in versioned artifacts, use roles, set limits, and emit structured telemetry.
Example
# Inspect available EC2 instances by tag
aws ec2 describe-instances \
--filters 'Name=tag:Environment,Values=dev' \
--query 'Reservations[].Instances[].[InstanceId,State.Name]'
# Invoke a deployed function with a JSON event
aws lambda invoke \
--function-name resize-image \
--payload '{"objectKey":"uploads/photo.jpg"}' \
response.json
Run examples in a disposable training account and replace Regions, identifiers, policies, resource sizes, and names with reviewed values. Verify commands with the current AWS CLI and service documentation before production use.
Production guidance
- Patch EC2 images and replace instances through an automated image pipeline.
- Set Lambda memory, timeout, reserved concurrency, retries, and dead-letter behavior intentionally.
- Measure startup latency and total cost instead of assuming one compute model is always cheaper.
The chapter quiz follows the lesson and checks both service vocabulary and architecture decisions.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.