Course: CompTIA Cloud+ CV0-004
Objective: Learn how to use the AWS CLI to deploy and terminate an EC2 instance from the command line.
t2.micro instance with Amazon Linux 2023 Open your terminal. Run:
bash
aws configure
Enter your AWS credentials:
us-east-1)jsonโ Checkpoint: Run the following to verify connectivity:
aws ec2 describe-instances
Create a key pair:
bash
aws ec2 create-key-pair --key-name cli-key --query 'KeyMaterial' --output text > cli-key.pem
chmod 400 cli-key.pem
Find the latest Amazon Linux 2023 AMI:
bash
aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64 --region us-east-1
Use the value under Value from the output as the AMI ID.
Create a security group:
bash
aws ec2 create-security-group --group-name cli-sg --description "CLI SG"
Allow SSH access:
bash
aws ec2 authorize-security-group-ingress \
--group-name cli-sg \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0
Launch the EC2 instance:
bash
aws ec2 run-instances \
--image-id <AMI-ID> \
--count 1 \
--instance-type t2.micro \
--key-name cli-key \
--security-groups cli-sg
๐ Note: Replace <AMI-ID> with the actual value from step 2.
โ Checkpoint: Use the following to get the public IP:
aws ec2 describe-instances --query "Reservations[*].Instances[*].PublicIpAddress" --output text
ssh -i "cli-key.pem" ec2-user@<public-ip>
โ Checkpoint: You should now be connected to the EC2 instance.
Get the Instance ID:
bash
aws ec2 describe-instances --query "Reservations[*].Instances[*].InstanceId" --output text
Terminate the instance:
bash
aws ec2 terminate-instances --instance-ids <INSTANCE-ID>
Confirm it's terminated:
bash
aws ec2 describe-instances --instance-ids <INSTANCE-ID> --query "Reservations[*].Instances[*].State.Name"
โ
Checkpoint: Output should say terminated.
You successfully deployed and terminated an EC2 instance using only the AWS CLI.