0
0
AWScloud~5 mins

Why EC2 matters for compute in AWS - Why It Works

Choose your learning style9 modes available
Introduction
Sometimes you need a computer in the cloud to run your apps or websites. Amazon EC2 lets you create and use virtual computers quickly without buying physical machines.
When you want to run a website without owning a physical server.
When you need to test software on different operating systems without extra hardware.
When your app needs more power during busy times and less when quiet.
When you want to run background tasks or data processing in the cloud.
When you want to try new software safely without affecting your own computer.
Commands
This command starts one virtual computer (instance) using a common Linux image. It sets the size to t2.micro, uses a key pair for secure login, and places it in a network group.
Terminal
aws ec2 run-instances --image-id ami-0c94855ba95c71c99 --count 1 --instance-type t2.micro --key-name my-key-pair --security-group-ids sg-0bb1c1e2eexample --subnet-id subnet-6e7f829e
Expected OutputExpected
{ "Instances": [ { "InstanceId": "i-1234567890abcdef0", "ImageId": "ami-0c94855ba95c71c99", "InstanceType": "t2.micro", "State": { "Code": 0, "Name": "pending" }, "KeyName": "my-key-pair" } ] }
--image-id - Specifies the operating system image to use.
--instance-type - Sets the size and power of the virtual computer.
--key-name - Defines the key pair for secure SSH access.
This command checks the status and details of the virtual computer you just started.
Terminal
aws ec2 describe-instances --instance-ids i-1234567890abcdef0
Expected OutputExpected
{ "Reservations": [ { "Instances": [ { "InstanceId": "i-1234567890abcdef0", "State": { "Code": 16, "Name": "running" }, "PublicIpAddress": "54.123.45.67" } ] } ] }
--instance-ids - Specifies which instance to check.
This command connects you securely to your virtual computer using SSH and the key pair.
Terminal
ssh -i my-key-pair.pem ec2-user@54.123.45.67
Expected OutputExpected
The terminal changes to a prompt on the remote EC2 instance, for example: ec2-user@ip-172-31-16-139:~$
-i - Specifies the private key file for authentication.
This command stops and deletes the virtual computer to avoid extra charges when you no longer need it.
Terminal
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
Expected OutputExpected
{ "TerminatingInstances": [ { "InstanceId": "i-1234567890abcdef0", "CurrentState": { "Code": 32, "Name": "shutting-down" }, "PreviousState": { "Code": 16, "Name": "running" } } ] }
--instance-ids - Specifies which instance to terminate.
Key Concept

If you remember nothing else from this pattern, remember: EC2 lets you rent virtual computers on demand to run your apps without owning hardware.

Common Mistakes
Not specifying the correct key pair when launching the instance.
You won't be able to securely connect to your virtual computer via SSH.
Create or use an existing key pair and specify it with --key-name when launching.
Forgetting to terminate instances after use.
You keep paying for virtual computers you no longer need.
Always run the terminate-instances command when done.
Using the wrong image ID that does not exist in your region.
The instance launch will fail because the image is unavailable.
Check and use a valid AMI ID for your AWS region.
Summary
Use 'aws ec2 run-instances' to start a virtual computer with your chosen settings.
Check its status with 'aws ec2 describe-instances' to know when it's ready.
Connect securely using SSH with your key pair to manage your instance.
Terminate the instance with 'aws ec2 terminate-instances' to stop charges.