How to Install New Ec2 Instance through awscli in aws
copy command for your reference
create VPC - aws ec2 create-vpc --cidr-block 10.0.0.0/16
create subnet - aws ec2 create-subnet --vpc-id vpcId --cidr-block 10.0.1.0/24
second subnet - aws ec2 create-subnet --vpc-id vpcId --cidr-block 10.0.0.0/24
create internet gateway - aws ec2 create-internet-gateway
After the internet gateway is created, note the InternetGatewayId and to attach this internet gateway to the already created VPC. To do so use the below command:
attached gateway - aws ec2 attach-internet-gateway --vpc-id vpcId --internet-gateway-id InternetGatewayId
creating routing table - aws ec2 create-route-table --vpc-id vpcId
Now, use the RouteTableId and use it in the next step:
create route - aws ec2 create-route --route-table-id RouteTableId --destination-cidr-block 0.0.0.0/0 --gateway-id nternetGatewayI
view the route table and subnet -
1.1 aws ec2 describe-route-tables --route-table-id RouteTableId
1.2 aws ec2 describe-subnets --filters "Name=vpc-id,Values=vpc-0cd2ff72d74bec455" --query "Subnets[*].{ID:SubnetId,CIDR:CidrBlock}"
associating route table - aws ec2 associate-route-table --subnet-id SubnetId --route-table-id RouteTableId
To map the public IP to the subnet, use the below command:
map - aws ec2 modify-subnet-attribute --subnet-id SubnetId --map-public-ip-on-launch
Creating Key Pair and Security Group
aws ec2 create-key-pair --key-name awscli --query "KeyMaterial" --output text "D:\awscli.pem"
For security group use the below commands:
aws ec2 create-security-group --group-name security-group-name --description "description" --vpc-id vpcId
GroupId and use it in the next step.
aws ec2 authorize-security-group-ingress --group-id GroupId --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id sg-0b03bf29683a98bff --protocol tcp --port 80 --cidr 0.0.0.0/0
Running the EC2 Instance
aws ec2 run-instances --image-id ami-id --count 1 --instance-type t2.micro --key-name Keypair-name --security-group-ids SecurityGroupId --subnet-id SubnetId
view your instance
aws ec2 describe-instances --instance-id InstanceId