☰ See All Chapters |
How create EC2 instance using AWS CLI
Create Key Pair
aws ec2 create-key-pair --key-name "aws-cli-keypair"
Create Security Group
aws ec2 create-security-group --group-name "aws-cli-security-group" --description "Security Group for AWS CLI Test"
Note down the security group id and use the same while creating EC2 instance.
Add inbound rule to Security Group
aws ec2 authorize-security-group-ingress --group-name "aws-cli-security-group" --protocol "tcp" --port 22 --cidr "0.0.0.0/0"
Create EC2 instance
aws ec2 run-instances --image-id "ami-a9d09ed1" --instance-type "t2.micro" --count 1 --no-associate-public-ip-address --key-name "aws-cli-keypair" --security-group-ids "sg-076b41e64c4006391"
You will see the below output in terminal:
C:\Users\Manu>aws ec2 run-instances --image-id "ami-a9d09ed1" --instance-type "t2.micro" --count 1 --no-associate-public-ip-address --key-name "aws-cli-keypair" --security-group-ids "sg-076b41e64c4006391" { "Groups": [], "Instances": [ { "AmiLaunchIndex": 0, "ImageId": "ami-a9d09ed1", "InstanceId": "i-027032f34856e14ad", "InstanceType": "t2.micro", "KeyName": "aws-cli-keypair", "LaunchTime": "2023-02-20T14:35:07.000Z", "Monitoring": { "State": "disabled" }, "Placement": { "AvailabilityZone": "us-west-2a", "GroupName": "", "Tenancy": "default" }, "PrivateDnsName": "ip-172-31-21-215.us-west-2.compute.internal", "PrivateIpAddress": "172.31.21.215", "ProductCodes": [], "PublicDnsName": "", "State": { "Code": 0, "Name": "pending" }, "StateTransitionReason": "", "SubnetId": "subnet-0d9b9c77bdd70e680", "VpcId": "vpc-0c7f7be20f1f86feb", "Architecture": "x86_64", "BlockDeviceMappings": [], "ClientToken": "191768e0-913e-4408-853a-fed25feb935f", "EbsOptimized": false, "EnaSupport": true, "Hypervisor": "xen", "NetworkInterfaces": [ { "Attachment": { "AttachTime": "2023-02-20T14:35:07.000Z", "AttachmentId": "eni-attach-0dd798646c8c2a25e", "DeleteOnTermination": true, "DeviceIndex": 0, "Status": "attaching", "NetworkCardIndex": 0 }, "Description": "", "Groups": [ { "GroupName": "aws-cli-security-group", "GroupId": "sg-076b41e64c4006391" } ], "Ipv6Addresses": [], "MacAddress": "02:8c:3d:4f:f4:8d", "NetworkInterfaceId": "eni-059b84f000cdecab2", "OwnerId": "442884294782", "PrivateDnsName": "ip-172-31-21-215.us-west-2.compute.internal", "PrivateIpAddress": "172.31.21.215", "PrivateIpAddresses": [ { "Primary": true, "PrivateDnsName": "ip-172-31-21-215.us-west-2.compute.internal", "PrivateIpAddress": "172.31.21.215" } ], "SourceDestCheck": true, "Status": "in-use", "SubnetId": "subnet-0d9b9c77bdd70e680", "VpcId": "vpc-0c7f7be20f1f86feb", "InterfaceType": "interface" } ], "RootDeviceName": "/dev/xvda", "RootDeviceType": "ebs", "SecurityGroups": [ { "GroupName": "aws-cli-security-group", "GroupId": "sg-076b41e64c4006391" } ], "SourceDestCheck": true, "StateReason": { "Code": "pending", "Message": "pending" }, "VirtualizationType": "hvm", "CpuOptions": { "CoreCount": 1, "ThreadsPerCore": 1 }, "CapacityReservationSpecification": { "CapacityReservationPreference": "open" }, "MetadataOptions": { "State": "pending", "HttpTokens": "optional", "HttpPutResponseHopLimit": 1, "HttpEndpoint": "enabled", "HttpProtocolIpv6": "disabled", "InstanceMetadataTags": "disabled" }, "EnclaveOptions": { "Enabled": false }, "PrivateDnsNameOptions": { "HostnameType": "ip-name", "EnableResourceNameDnsARecord": false, "EnableResourceNameDnsAAAARecord": false } } ], "OwnerId": "442884294782", "ReservationId": "r-099d23d338fbd2f46" }
C:\Users\Manu> |
Verify the EC2 instance from AMS Console.
All Chapters