Sub topic 2
Key Pairs and Connecting to Amazon EC2
Assume you know basic Linux commands, IP addresses, ports, and SSH. SSH is a protocol for opening a secure terminal session on a remote computer. An EC2 key pair provides the private key used to prove your identity. AWS stores the public key, while you keep the private key.
Set strict permissions on a private key:
chmod 400 my-key.pem
ssh -i my-key.pem [email protected]The username depends on the AMI. Amazon Linux commonly uses ec2-user; Ubuntu commonly uses ubuntu. A security group must allow inbound TCP port 22 from your current IP address. Avoid opening SSH to 0.0.0.0/0 unless you have a specific, temporary reason.
Launching an Instance with the AWS CLI
An instance is a virtual server. A launch needs an AMI, instance type, subnet, security group, key pair, and usually an IAM instance profile for AWS API access.
AMI_ID=$(aws ssm get-parameter \
--name /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \
--query Parameter.Value --output text)
aws ec2 run-instances \
--image-id "$AMI_ID" \
--instance-type t3.micro \
--key-name my-key \
--security-group-ids sg-0123456789abcdef0 \
--subnet-id subnet-0123456789abcdef0 \
--metadata-options HttpTokens=required \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=tutorial-web}]'A private subnet has no direct route to the internet. A public subnet needs a route through an internet gateway and a public IPv4 address for direct SSH access.
Instance States
Stopping pauses compute billing but preserves EBS data. Starting allocates compute again, and an automatically assigned public IPv4 address may change. Rebooting restarts the operating system on the same instance. Hibernate saves memory to the root EBS volume and can resume later when supported. Terminating permanently deletes the instance and usually deletes its root EBS volume.
Bootstrapping with User Data
User data is a script that runs during first boot. It can install software without an interactive SSH session.
#!/bin/bash
dnf install -y nginx
systemctl enable --now nginxPersistent and Temporary Storage
EBS is network-attached block storage. It persists when an instance stops and can be snapshotted. Instance store is local disk storage with high performance, but its data is lost when the instance stops or terminates.
| Storage | Survives stop? | Typical use |
|---|---|---|
| EBS | Yes | Operating systems and application data |
| Instance store | No | Cache, scratch space, temporary files |
Exercise
Which storage should hold a database that must survive an instance stop?
Show answer
An EBS volume, with snapshots or another backup strategy.