Creating EC2 with AWS CLI
Create EC2 instances easily with copy and paste
AI-generated content may be inaccurate or misleading.
This article assumes that AWS CLI is already installed and configured.
Create VPC
aws ec2 create-vpc --cidr-block
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.

When you create a VPC with the command above, the VPC information will be returned.
Create Subnet
aws ec2 create-subnet --vpc-id \ --cidr-block
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.

When you create a subnet, the subnet information is returned as shown above.
Create IGW
aws ec2 create-internet-gateway
Create an internet gateway and note the returned ID.
aws ec2 attach-internet-gateway --internet-gateway-id \ --vpc-id
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.
Now attach the internet gateway to the VPC.

If you run it twice and get the message already attached to network, it's successful.
Create Route Table
aws ec2 create-route-table --vpc-id
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.

Create a Route table with the following command.
aws ec2 create-route --route-table-id \ --destination-cidr-block \ --gateway-id
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.

The result value True is returned.
Now let's associate the subnet with the Route table.
aws ec2 describe-subnets --filters "Name=vpc-id,Values=" \ --query "Subnets[*].{ID:SubnetId,CIDR:CidrBlock}"
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.

You can check the subnet ID with the following command.
aws ec2 associate-route-table --subnet-id \ --route-table-id
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.

Associate the subnet with the Route table.
aws ec2 modify-subnet-attribute --subnet-id \ --map-public-ip-on-launch
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.
Use the following command to enable automatic public IP address assignment. Since EC2 instance IP addresses change upon restart, this must be configured. Otherwise, you need to associate an Elastic IP address with the instance.
Create SG
aws ec2 create-security-group --group-name \ --description \ --vpc-id
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.

Create a security group with the following command.
aws ec2 authorize-security-group-ingress --group-id \ --protocol \ --port \ --cidr
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.

Then open port 22 (ssh) in the security group to allow access from anywhere with 0.0.0.0/0.
Create Key Pair
aws ec2 create-key-pair --key-name \ --query 'KeyMaterial' --output text > .pem chmod 400 .pem
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.
Create a key pair and set permissions.
Create EC2
aws ec2 describe-images --owners self amazon | lessYou can find AMIs with the following command. Select an appropriate AMI.
aws ec2 run-instances --image-id \ --count 1 \ --instance-type t2.micro \ --key-name \ --security-group-ids \ --subnet-id
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.
Create an EC2 instance with the following command.

Note the instance ID output when creating the instance.
aws ec2 describe-instances --instance-id
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.

Wait until the instance state becomes running with the command above.
aws ec2 describe-instances --instance-id \ --query 'Reservations[*].Instances[*].PublicIpAddress' \ --output text
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.

Check the EC2 instance's public IP with the command above.
ssh -i .pem ec2-user@
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.
Try SSH connection using the key you just created and the EC2 IP.

You can see that you have successfully connected to the EC2 instance from the CLI.
aws ec2 terminate-instances --instance-ids aws ec2 delete-subnet --subnet-id aws ec2 delete-route-table --route-table-id aws ec2 detach-internet-gateway --internet-gateway-id --vpc-id aws ec2 delete-internet-gateway --internet-gateway-id aws ec2 delete-security-group --group-id aws ec2 delete-vpc --vpc-id
*파란색 텍스트를 클릭하면 간편하게 수정 후 복사할 수 있습니다.
You can delete EC2 instances and security groups in the following order. Enter them one at a time as they may not be deleted if entered all at once.
- Delete EC2 instance
- Delete subnet
- Delete Route table
- Detach internet gateway from VPC
- Delete internet gateway
- Delete security group
- Delete VPC
A better method is to use nuke to delete everything.
ref: https://dev.classmethod.jp/articles/build-ec2-with-aws-cli/