Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (29.3k points)

I need to launch an EC2 instance using AWS CLI. Please help me with the steps.

1 Answer

0 votes
by (50.2k points)
edited by

Using the following AWS CLI, you can launch, list and terminate an EC2 instances. Also, you need key pair and security groups. Refer to the below steps.

Launching an EC2 instance:

You can launch an instance inside a VPC. The following example shows launching a t2.micro instance. Replace the parameter values with your own.

$ aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-903004f8 --subnet-id subnet-6e7f829e

{

    "OwnerId": "123456789012",

    "ReservationId": "r-5875ca20",

    "Groups": [

        {

            "GroupName": "my-sg",

            "GroupId": "sg-903004f8"

        }

    ],

    "Instances": [

        {

            "Monitoring": {

                "State": "disabled"

            },

            "PublicDnsName": null,

            "Platform": "windows",

            "State": {

                "Code": 0,

                "Name": "pending"

            },

            "EbsOptimized": false,

            "LaunchTime": "2013-07-19T02:42:39.000Z",

            "PrivateIpAddress": "10.0.1.114",

            "ProductCodes": [],

            "VpcId": "vpc-1a2b3c4d",

            "InstanceId": "i-5203422c",

            "ImageId": "ami-173d747e",

            "PrivateDnsName": ip-10-0-1-114.ec2.internal,

            "KeyName": "MyKeyPair",

            "SecurityGroups": [

                {

                    "GroupName": "my-sg",

                    "GroupId": "sg-903004f8"

                }

            ],

            "ClientToken": null,

            "SubnetId": "subnet-6e7f829e",

            "InstanceType": "t2.micro",

            "NetworkInterfaces": [

                {

                    "Status": "in-use",

                    "SourceDestCheck": true,

                    "VpcId": "vpc-1a2b3c4d",

                    "Description": "Primary network interface",

                    "NetworkInterfaceId": "eni-a7edb1c9",

                    "PrivateIpAddresses": [

                        {

                            "PrivateDnsName": "ip-10-0-1-114.ec2.internal",

                            "Primary": true,

                            "PrivateIpAddress": "10.0.1.114"

                        }

                    ],

                    "PrivateDnsName": "ip-10-0-1-114.ec2.internal",

                    "Attachment": {

                        "Status": "attached",

                        "DeviceIndex": 0,

                        "DeleteOnTermination": true,

                        "AttachmentId": "eni-attach-52193138",

                        "AttachTime": "2013-07-19T02:42:39.000Z"

                    },

                    "Groups": [

                        {

                            "GroupName": "my-sg",

                            "GroupId": "sg-903004f8"

                        }

                    ],

                    "SubnetId": "subnet-6e7f829e",

                    "OwnerId": "123456789012",

                    "PrivateIpAddress": "10.0.1.114"

                }              

            ],

            "SourceDestCheck": true,

            "Placement": {

                "Tenancy": "default",

                "GroupName": null,

                "AvailabilityZone": "us-west-2b"

            },

            "Hypervisor": "xen",

            "BlockDeviceMappings": [

                {

                    "DeviceName": "/dev/sda1",

                    "Ebs": {

                        "Status": "attached",

                        "DeleteOnTermination": true,

                        "VolumeId": "vol-877166c8",

                        "AttachTime": "2013-07-19T02:42:39.000Z"

                    }

                }              

            ],

            "Architecture": "x86_64",

            "StateReason": {

                "Message": "pending",

                "Code": "pending"

            },

            "RootDeviceName": "/dev/sda1",

            "VirtualizationType": "hvm",

            "RootDeviceType": "ebs",

            "Tags": [

                {

                    "Value": "MyInstance",

                    "Key": "Name"

                }

            ],

            "AmiLaunchIndex": 0

        }

    ]

}

EC2-Classic:

The following command is used to launch a t1.micro instance in a EC2-classic.

$ aws ec2 run-instances --image-id ami-173d747e --count 1 --instance-type t1.micro --key-name MyKeyPair --security-groups my-sg

{

    "OwnerId": "123456789012",

    "ReservationId": "r-5875ca20",

    "Groups": [

        {

            "GroupName": "my-sg",

            "GroupId": "sg-903004f8"

        }

    ],

    "Instances": [

        {

            "Monitoring": {

                "State": "disabled"

            },

            "PublicDnsName": null,

            "Platform": "windows",

            "State": {

                "Code": 0,

                "Name": "pending"

            },

            "EbsOptimized": false,

            "LaunchTime": "2013-07-19T02:42:39.000Z",

            "ProductCodes": [],

            "InstanceId": "i-5203422c",

            "ImageId": "ami-173d747e",

            "PrivateDnsName": null,

            "KeyName": "MyKeyPair",

            "SecurityGroups": [

                {

                    "GroupName": "my-sg",

                    "GroupId": "sg-903004f8"

                }

            ],

            "ClientToken": null,

            "InstanceType": "t1.micro",

            "NetworkInterfaces": [],

            "Placement": {

                "Tenancy": "default",

                "GroupName": null,

                "AvailabilityZone": "us-west-2b"

            },

            "Hypervisor": "xen",

            "BlockDeviceMappings": [

                {

                    "DeviceName": "/dev/sda1",

                    "Ebs": {

                        "Status": "attached",

                        "DeleteOnTermination": true,

                        "VolumeId": "vol-877166c8",

                        "AttachTime": "2013-07-19T02:42:39.000Z"

                    }

                }              

            ],

            "Architecture": "x86_64",

            "StateReason": {

                "Message": "pending",

                "Code": "pending"

            },

            "RootDeviceName": "/dev/sda1",

            "VirtualizationType": "hvm",

            "RootDeviceType": "ebs",

            "Tags": [

                {

                    "Value": "MyInstance",

                    "Key": "Name"

                }

            ],

            "AmiLaunchIndex": 0

        }

    ]

}

Adding a block device to your instance:

Each and every instances has its own root volume. For adding a block device to your instance, mention the --block-device-mappings option when you use run-instances.

The following command is an example for a standard Amazon EBS volume that is 20 GB in size, and it maps your instance using the identifier '/dev/sdf'.

--block-device-mappings "[{\"DeviceName\":\"/dev/sdf\",\"Ebs\":{\"VolumeSize\":20,\"DeleteOnTermination\":false}}]"

For adding two volumes to your instance. The available number of volumes in your instance depends on its instance type.

--block-device-mappings "[{\"DeviceName\":\"/dev/sdf\",\"VirtualName\":\"ephemeral0\"},{\"DeviceName\":\"/dev/sdg\",\"VirtualName\":\"ephemeral1\"}]"

To create the mapping (/dev/sdj), but doesn't provision a volume for the instance.

--block-device-mappings "[{\"DeviceName\":\"/dev/sdj\",\"NoDevice\":\"\"}]"

Add a tag to your instance:

For adding a tag to your instance, follow the below command.

$ aws ec2 create-tags --resources i-5203422c --tags Key=Name,Value=MyInstance

Now, connect your instance.

List your instance:

The following command lists only your t2.micro instances and the outputs of the InstanceId values for each match only.

$ aws ec2 describe-instances --filters "Name=instance-type,Values=t2.micro" --query "Reservations[].Instances[].InstanceId"

[

    "i-05e998023d9c69f9a"

]

The following command is used for listing any of your instances that have the tag Name=MyInstance.

aws ec2 describe-instances --filters "Name=tag:Name,Values=MyInstance"

The following command will list your instances that were launched using any of the following 'AMIs: ami-x0123456', 'ami-y0123456', and 'ami-z0123456'.

$ aws ec2 describe-instances --filters "Name=image-id,Values=ami-x0123456,ami-y0123456,ami-z0123456"

Terminate instance:

Follow the terminate command to delete or terminate instances.

$ aws ec2 terminate-instances --instance-ids i-5203422c

{

    "TerminatingInstances": [

        {

            "InstanceId": "i-5203422c",

            "CurrentState": {

                "Code": 32,

                "Name": "shutting-down"

            },

            "PreviousState": {

                "Code": 16,

                "Name": "running"

            }

        }

    ]

}

For more information, check out this AWS Tutorial.

Related questions

0 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
asked Mar 19, 2020 in AWS by chandra (29.3k points)

Browse Categories

...