Creating EC2 Using AWS CLI
Create EC2 using AWS CLI
- From the network infrastructure created with CLI, we will create EC2. First, create AWS Key pair
aws ec2 create-key-pair --key-name MyKeyPair --query "KeyMaterial" --output text > MyKeyPair.pem
![AWS CLI](/images/ec2-net/0001.png?featherlight=false&width=90pc)
- Check on the interface; we confirm that we have successfully created Key pair
![AWS CLI](/images/ec2-net/0002.png?featherlight=false&width=90pc)
- Use the command to decentralize:
chmod 400 MyKeyPair.pem
![AWS CLI](/images/ec2-net/0003.png?featherlight=false&width=90pc)
- Create Security group for EC2
aws ec2 create-security-group --group-name SSHAccess --description "Security group for SSH access" --vpc-id **VPC ID**
![AWS CLI](/images/ec2-net/0004.png?featherlight=false&width=90pc)
- Then, we check the created Security group.
![AWS CLI](/images/ec2-net/0005.png?featherlight=false&width=90pc)
- Make permission to SSH:
aws ec2 authorize-security-group-ingress --group-id **SG ID** --protocol tcp --port 22 --cidr 0.0.0.0/0
![AWS CLI](/images/ec2-net/0006.png?featherlight=false&width=90pc)
- Preparation is complete. Let’s start EC2
aws ec2 run-instances --image-id **AMI** --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids **SG ID** --subnet- id **Subnet ID**
![AWS CLI](/images/ec2-net/0007.png?featherlight=false&width=90pc)
- Wait about 2 minutes, see the status of EC2 instance
aws ec2 describe-instances --instance-id **Instance ID** --query "Reservations[*].Instances[*].{State:State.Name,Address:PublicIpAddress}"
![AWS CLI](/images/ec2-net/0008.png?featherlight=false&width=90pc)
- When the instance is in running state. We make the connection
ssh -i "MyKeyPair.pem" ec2-user@IP Public
![AWS CLI](/images/ec2-net/0009.png?featherlight=false&width=90pc)
- Once used, we execute terminate with the command:
aws ec2 terminate-instance --instances-ids **Instance ID**
![AWS CLI](/images/ec2-net/00010.png?featherlight=false&width=90pc)
- After about 2-3 minutes later. We check the status of instance
![AWS CLI](/images/ec2-net/00011.png?featherlight=false&width=90pc)