Sub topic 3
Backups, Access, and Operating Costs for EC2
Assume you know basic Linux commands, IP addresses, CIDR blocks, ports, IAM, and SSH. This chapter connects storage backups with secure access, monitoring, scaling, and cleanup.
Backing up with EBS snapshots
An Amazon EBS snapshot is a point-in-time backup of an EBS volume. Snapshots are stored durably in Amazon S3 and are incremental after the first snapshot. A snapshot can create a new volume in the same or another Availability Zone.
aws ec2 create-snapshot \
--volume-id vol-0123456789abcdef0 \
--description "tutorial-web data volume backup"For application data, coordinate the snapshot with the application or filesystem. Otherwise, files being written may be inconsistent. Snapshots are not a replacement for testing restores.
Giving instances permissions with IAM roles
Attach an IAM role to an instance through an instance profile. Applications can then request temporary credentials instead of storing access keys on disk. Grant only the actions the application needs, such as reading one S3 bucket.
Reading instance metadata securely with IMDSv2
The Instance Metadata Service provides local information such as the instance ID and attached role credentials. IMDSv2 requires a session token, which reduces some request-forgery risks.
TOKEN=$(curl -s -X PUT \
"http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/instance-idRequire IMDSv2 when launching or modifying instances.
Stable public addresses with Elastic IPs
An automatically assigned public IPv4 address can change after a stop and start. An Elastic IP is a persistent public address that you can associate with another instance. AWS charges for public IPv4 addresses, including some idle addresses, so release unused Elastic IPs.
Monitoring instances and status checks
CloudWatch collects metrics such as CPU utilization. EC2 status checks detect impaired infrastructure or instance reachability. Alarms can notify you or trigger Auto Scaling actions. High CPU alone does not prove that an application is healthy, so add application-level checks when needed.
flowchart LR
Snapshot --> Restore
Role --> Application
IMDSv2 --> Role
CloudWatch --> Scaling
Scaling --> LoadBalancerPurchasing options and controlling cost
| Option | Typical use |
|---|---|
| On-Demand | Variable or short-term workloads |
| Savings Plans or Reserved Instances | Predictable long-term usage |
| Spot Instances | Interruptible, flexible workloads |
Choose using workload duration, interruption tolerance, and required capacity. Review instance size, storage, data transfer, and public IPv4 costs.
Scaling out: Auto Scaling groups and load balancers
An Auto Scaling group maintains a desired number of instances and replaces unhealthy ones. A load balancer distributes requests across healthy instances. Together they improve availability and allow capacity to follow demand.
Cleaning up to avoid charges
Terminate test instances, delete unused EBS volumes and snapshots, and release unattached Elastic IPs. Confirm that backups required for recovery are retained before deleting them.
Exercise
Which resource should you use for a recoverable backup of an EBS volume?
Show answer
An EBS snapshot, followed by a tested restore when recovery is important.