DOYENSYS Knowledge Portal




We Welcome you to visit our DOYENSYS KNOWLEDGE PORTAL : Doyensys Knowledge Portal




Sunday, August 6, 2017

Launch EC2 Instance from AWSCLI

Launch EC2 Instance from AWSCLI


In this blogpost, I will show you how to launch EC2 instance from AWSCLI (AWS command line).  This is very quick when compared to creating instances with console.  Please check my previous post about how to launch EC2 from AWS Console.

First, we need to install the AWS CLI, then configure it and finally launch EC2 using CLI.

Download appropriate AWS CLI MSI installer for your system, here I will be using Windows version.  Once installed, go to Windows command prompt and type aws.  If you are getting any output, then it means aws cli was installed.


Now, we need to configure AWS CLI with security credentials.  If you don’t have them, then we need to create them.   

For the sake of easiness, I am creating security credentials for root user, don't share the credentials with anyone and delete them or make them inactive after your activity.

In the navigation panel, choose user and  My Security Credentials



Press Continue to Security Credentials

 

Then click 'Create New Access Key' from Access Keys





Click Download Key File, the downloaded file will be in .csv format.  Keep the file in a safe location and don't share it with anyone.  It will contain AWSAccessKeyId and AWSSecretKey.




Now, we need to configure aws cli with the security credentials

In the command prompt, type:  aws configure
It will prompt you Access Key ID, Secret Access Key, Region and Output format; enter the first two from csv file that we have downloaded; enter region as us-west-2 - we will be using Oregon as our region; leave output as default.



We need few more details in order to launch ec2 instance from console.

They are:

1.    image-id:  The AWS AMI ID - OS media.  Here we will be using ami-6df1e514
2.    instance-type:  AWS is providing different types of instances optimized to fit different use cases.  They comprise varying combinations of CPU, memory, storage, and networking capacity and give us the flexibility to choose the appropriate mix of resources for our applications.  We will be launching t2.micro instance
3.    key-name:  Security key pair.  I had already created MyKeyPair key pair.
4.    region:  The region where we would like our instance to be hosted.  We will be hosting our instance in us-west-2 region (Oregon).

Now use the below command to launch EC2 instance from AWS CLI

aws ec2 run-instances --image-id ami-6df1e514 --count 1 --instance-type t2.micro --key-name MyKeyPair --region us-west-2


Here --count is number of instance we would like to launch

========================================================================
C:\Users\AVK>aws ec2 run-instances --image-id ami-6df1e514 --count 1 --instance-type t2.micro --key-name MyKeyPair --region us-west-2
{
    "Instances": [
        {
            "Monitoring": {
                "State": "disabled"
            },
            "PublicDnsName": "",
            "StateReason": {
                "Message": "pending",
                "Code": "pending"
            },
            "State": {
                "Code": 0,
                "Name": "pending"
            },
            "EbsOptimized": false,
            "LaunchTime": "2017-08-06T11:01:49.000Z",
            "PrivateIpAddress": "172.31.32.238",
            "ProductCodes": [],
            "VpcId": "vpc-deed20b8",
            "StateTransitionReason": "",
            "InstanceId": "i-09c9b5f291e9e63a6",
            "ImageId": "ami-6df1e514",
            "PrivateDnsName": "ip-172-31-32-238.us-west-2.compute.internal",
            "KeyName": "MyKeyPair",
            "SecurityGroups": [
                {
                    "GroupName": "default",
                    "GroupId": "sg-29ee3753"
                }
            ],
            "ClientToken": "",
            "SubnetId": "subnet-56facb1f",
            "InstanceType": "t2.micro",
            "NetworkInterfaces": [
                {
                    "Status": "in-use",
                    "MacAddress": "06:83:86:09:94:6a",
                    "SourceDestCheck": true,
                    "VpcId": "vpc-deed20b8",
                    "Description": "",
                    "NetworkInterfaceId": "eni-570b8669",
                    "PrivateIpAddresses": [
                        {
                            "PrivateDnsName": "ip-172-31-32-238.us-west-2.compute.internal",
                            "Primary": true,
                            "PrivateIpAddress": "172.31.32.238"
                        }
                    ],
                    "PrivateDnsName": "ip-172-31-32-238.us-west-2.compute.internal",
                    "Attachment": {
                        "Status": "attaching",
                        "DeviceIndex": 0,
                        "DeleteOnTermination": true,
                        "AttachmentId": "eni-attach-1460d0f8",
                        "AttachTime": "2017-08-06T11:01:49.000Z"
                    },
                    "Groups": [
                        {
                            "GroupName": "default",
                            "GroupId": "sg-29ee3753"
                        }
                    ],
                    "Ipv6Addresses": [],
                    "OwnerId": "xxxxxxxxxxxxx",
                    "SubnetId": "subnet-56facb1f",
                    "PrivateIpAddress": "172.31.32.238"
                }
            ],
            "SourceDestCheck": true,
            "Placement": {
                "Tenancy": "default",
                "GroupName": "",
                "AvailabilityZone": "us-west-2b"
            },
            "Hypervisor": "xen",
            "BlockDeviceMappings": [],
            "Architecture": "x86_64",
            "RootDeviceType": "ebs",
            "RootDeviceName": "/dev/xvda",
            "VirtualizationType": "hvm",
            "AmiLaunchIndex": 0
        }
    ],
    "ReservationId": "r-075a4bba42ad67047",
    "Groups": [],
    "OwnerId": "xxxxxxxxxxxxx"
}

C:\Users\AVK>
========================================================================

Make a note of the InstanceID and go to AWS console and then to EC2 dashboard; our instance is launched and it is up and running.





Connect to it using the public DNS/IP.

Now we are able to launch an EC2 instance from AWS CLI successfully. 

Happy Learning!!!


 

1 comment:

Unknown said...

I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
aws training in marathahalli|