Bird
Raised Fist0
AWScloud~5 mins

Launching an EC2 instance in AWS - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Sometimes you need a virtual computer in the cloud to run your apps or store data. Launching an EC2 instance creates this virtual computer quickly and easily.
When you want to run a website without buying a physical server.
When you need a test environment to try new software safely.
When you want to run a database accessible from anywhere.
When you need extra computing power for a short time.
When you want to host a small app for friends or customers.
Config File - ec2-instance.json
ec2-instance.json
{
  "ImageId": "ami-0c02fb55956c7d316",
  "InstanceType": "t2.micro",
  "KeyName": "my-key-pair",
  "SecurityGroupIds": ["sg-0a1b2c3d4e5f6g7h"],
  "MinCount": 1,
  "MaxCount": 1
}

ImageId: The ID of the operating system image to use (Amazon Linux 2 here).

InstanceType: The size and power of the virtual computer (t2.micro is free tier eligible).

KeyName: The name of your SSH key to connect securely.

SecurityGroupIds: The firewall rules group allowing traffic.

MinCount and MaxCount: Number of instances to launch (1 here).

Commands
This command launches a new EC2 instance using the settings in the JSON file.
Terminal
aws ec2 run-instances --cli-input-json file://ec2-instance.json
Expected OutputExpected
{ "Instances": [ { "InstanceId": "i-0123456789abcdef0", "ImageId": "ami-0c02fb55956c7d316", "State": { "Code": 0, "Name": "pending" }, "InstanceType": "t2.micro", "KeyName": "my-key-pair", "LaunchTime": "2024-06-01T12:00:00.000Z" } ] }
--cli-input-json - Specifies the JSON file with instance configuration.
This command checks the status and details of the launched EC2 instance.
Terminal
aws ec2 describe-instances --instance-ids i-0123456789abcdef0
Expected OutputExpected
{ "Reservations": [ { "Instances": [ { "InstanceId": "i-0123456789abcdef0", "State": { "Code": 16, "Name": "running" }, "PublicIpAddress": "54.123.45.67", "InstanceType": "t2.micro" } ] } ] }
--instance-ids - Specifies which instance to describe.
This command connects you securely to the EC2 instance using SSH and your key.
Terminal
ssh -i my-key-pair.pem ec2-user@54.123.45.67
Expected OutputExpected
The authenticity of host '54.123.45.67 (54.123.45.67)' can't be established. ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '54.123.45.67' (ECDSA) to the list of known hosts. [ec2-user@ip-172-31-xx-xx ~]$
-i - Specifies the private key file for authentication.
Key Concept

If you remember nothing else from this pattern, remember: launching an EC2 instance means creating a virtual computer in the cloud ready to use with your settings.

Common Mistakes
Using a wrong or missing KeyName in the JSON file.
You won't be able to connect to the instance securely without the correct SSH key.
Make sure the KeyName matches an existing key pair in your AWS account.
Not specifying the SecurityGroupIds or using a group without SSH access.
You cannot connect to the instance via SSH if the firewall blocks port 22.
Use a security group that allows inbound SSH (port 22) traffic.
Trying to SSH before the instance state is 'running'.
The instance is not ready to accept connections yet.
Check the instance state with describe-instances and wait until it is 'running'.
Summary
Create a JSON file with the EC2 instance settings like image, type, key, and security group.
Run 'aws ec2 run-instances' with the JSON file to launch the instance.
Use 'aws ec2 describe-instances' to check the instance status and get its IP address.
Connect to the instance securely using SSH with your private key and the public IP.

Practice

(1/5)
1. What does launching an EC2 instance in AWS create?
easy
A. A storage bucket
B. A virtual server in the cloud
C. A database instance
D. A physical server in your office

Solution

  1. Step 1: Understand EC2 purpose

    EC2 stands for Elastic Compute Cloud, which provides virtual servers.
  2. Step 2: Identify what launching means

    Launching an EC2 instance means creating a virtual server in AWS cloud.
  3. Final Answer:

    A virtual server in the cloud -> Option B
  4. Quick Check:

    EC2 instance = virtual server [OK]
Hint: EC2 = virtual server, not physical hardware [OK]
Common Mistakes:
  • Confusing EC2 with physical hardware
  • Thinking EC2 creates storage or database
  • Mixing EC2 with S3 storage
2. Which AWS CLI command is used to launch a new EC2 instance?
easy
A. aws ec2 run-instances
B. aws ec2 start-instance
C. aws ec2 create-instance
D. aws ec2 deploy-instance

Solution

  1. Step 1: Recall AWS CLI commands for EC2

    The correct command to launch EC2 instances is 'run-instances'.
  2. Step 2: Eliminate incorrect commands

    Commands like 'create-instance', 'start-instance', and 'deploy-instance' do not exist or are incorrect.
  3. Final Answer:

    aws ec2 run-instances -> Option A
  4. Quick Check:

    Launch EC2 = run-instances command [OK]
Hint: Remember 'run-instances' to start EC2 instances [OK]
Common Mistakes:
  • Using 'create-instance' which is invalid
  • Confusing 'start-instance' with launching
  • Assuming 'deploy-instance' is a valid command
3. What will happen if you run this command?
aws ec2 run-instances --image-id ami-12345678 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-groups MySecurityGroup
medium
A. Deletes the specified AMI image
B. Creates a new security group named MySecurityGroup
C. Launches one t2.micro EC2 instance with specified settings
D. Starts an existing stopped EC2 instance

Solution

  1. Step 1: Analyze the command parameters

    The command uses 'run-instances' with image ID, count=1, instance type, key pair, and security group.
  2. Step 2: Understand the command effect

    This command launches one new EC2 instance of type t2.micro with the given AMI and security settings.
  3. Final Answer:

    Launches one t2.micro EC2 instance with specified settings -> Option C
  4. Quick Check:

    run-instances with parameters = launch instance [OK]
Hint: run-instances with --count 1 launches one instance [OK]
Common Mistakes:
  • Thinking it deletes or modifies AMI
  • Confusing security group creation with usage
  • Assuming it starts an existing instance
4. You run this command but get an error: InvalidKeyPair.NotFound. What is the likely cause?
aws ec2 run-instances --image-id ami-87654321 --count 1 --instance-type t3.small --key-name WrongKey --security-groups Default
medium
A. The key pair named 'WrongKey' does not exist in your AWS account
B. The AMI ID is invalid
C. The instance type t3.small is not supported
D. The security group 'Default' is missing

Solution

  1. Step 1: Understand the error message

    'InvalidKeyPair.NotFound' means the specified key pair is not found in your AWS account.
  2. Step 2: Match error to command parameters

    The key-name 'WrongKey' likely does not exist or is misspelled, causing the error.
  3. Final Answer:

    The key pair named 'WrongKey' does not exist in your AWS account -> Option A
  4. Quick Check:

    InvalidKeyPair error = missing key pair [OK]
Hint: Check key pair name spelling if InvalidKeyPair error occurs [OK]
Common Mistakes:
  • Assuming AMI or instance type caused the error
  • Thinking security group 'Default' is missing
  • Ignoring key pair existence in the region
5. You want to launch 3 EC2 instances with the same settings but different subnet IDs for high availability. Which approach is best?
hard
A. Use aws ec2 create-subnet to create three subnets and launch one instance
B. Run one aws ec2 run-instances command with --count 3 and a single --subnet-id
C. Launch one instance and then clone it twice manually
D. Run three separate aws ec2 run-instances commands, each with a different --subnet-id

Solution

  1. Step 1: Understand subnet and instance launch

    Each EC2 instance can be launched in only one subnet at a time.
  2. Step 2: Plan for multiple subnets

    To launch instances in different subnets, run separate commands specifying each subnet ID.
  3. Step 3: Evaluate options

    Run three separate aws ec2 run-instances commands, each with a different --subnet-id. This runs three commands with different subnet IDs, ensuring instances spread across subnets.
  4. Final Answer:

    Run three separate aws ec2 run-instances commands, each with a different --subnet-id -> Option D
  5. Quick Check:

    One subnet per instance, multiple commands for multiple subnets [OK]
Hint: One subnet per instance, use multiple commands for multiple subnets [OK]
Common Mistakes:
  • Trying to launch multiple subnets in one command
  • Assuming cloning instances copies subnet settings
  • Confusing subnet creation with instance launch