CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

AWS CLI – AWS Configuration and Connect to EC2 Instance

AWS configuration must be set up before we run any commands from AWS CLI. AWS CLI is the AWS command-line interface tool, used to manage AWS services from the command line. And we can control multiple AWS services through scripts.

In this article, I am going to explain how to configure and control AWS services from the command line. I am using this tool from Windows 7 64-bit Operating System.

Step 1. You must have to download & install the AWS CLI command-line tool from https://aws.amazon.com/cli/, if you are not installed it already.

Step 2. Lets’ verify whether AWS CLI is working. Go to the command prompt & type “aws” at the command prompt.

C:\> aws

If you see the below message, either AWS CLI is not installed properly or the PATH variable is not set to the AWS CLI installed location where “aws.exe” exists.

‘aws’ is not recognized as an internal or external command,
operable program or batch file.

Mostly this is because of not setting a PATH environment variable. This article helps “How to set PATH environment variable in Windows?“, to set PATH environment variable in Windows Operating System.

If it is installed properly & the PATH variable is set, you will see the below message, when you type “aws” at the command prompt.

C:\>aws
usage: aws [options] <command> <subcommand> [<subcommand> …] [parameters]
To see help text, you can run:

aws help
aws <command> help
aws <command> <subcommand> help
aws: error: too few arguments

Step 3. The first step before we need to start managing AWS Services is; set up the AWS configuration. This is to enable AWS CLI to access AWS services. When we do the configuration setup, we must have to provide the below details:

  • AWS Access Key ID & AWS Secret Access Key
  • Default region name
    • This value you will get from AWS Management Console. For example: Once you login to the AWS Management console, from EC2 Dashboard; you will see Availability zones under Availability Zone Status:

Service Status:

US East (Ohio):

Availability Zone Status:
us-east-2a:
Availability zone is operating normally

us-east-2b:
Availability zone is operating normally

us-east-2c:
Availability zone is operating normally

So the region name is “us-east-2“. Don’t use the zone name.

  • Default output format
    • This is the format, AWS CLI command will use to display the results. We can specify “JSON”, “table” or “text” as the output formats.



Type below command to start the AWS configuration setup for AWS CLI. Provide appropriate values when prompt for the values.

C:\> aws configure

AWS Access Key ID [None]: xxxxxxxxxxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxxxxxxxx
Default region name [None]:us-east-2
Default output format [None]:json

This completes the AWS configuration setup.

AWS CLI - AWS Configuration
AWS CLI – AWS Configuration

Step 4. Now, we connect to AWS and fetch the EC2 instances. For that, we use the “ec2” command and the subcommand is “describe-instances“.

You must have to configure the AWS, before your type the below command; otherwise, you will see the below message;

You must specify a region. You can also configure your region by running “aws configure”.

Type below AWS CLI command at the command prompt, to describe EC2 instances:

C:\> aws ec2 describe-instances

If you see the below error message; your network or firewall rules are stopping to connect to the AWS Services. Change the settings to allow to connect.

[SSL: TLSV1_ALERT_ACCESS_DENIED] tlsv1 alert access denied (_ssl.c:581)

If you receive, below message; the key details you entered at the time of configuration setup were wrong. Redo the AWS configuration setup & provide the valid details.

An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials

If you entered, zone name “us-east-2c” instead of the region name “us-east-2“; you will get the below error.

Could not connect to the endpoint URL: “https://ec2.us-east-2c.amazonaws.com/”

After successful execution, you will see the JSON output with EC2 instance(s) details. We have mentioned “JSON” at the time of AWS configuration setup; hence we are seeing the output in “JSON” format. The output looks like below:

Note that; I marked some entries with “x” to not to disclose the details.

{
    "Reservations": [
        {
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "PublicDnsName": "ec2-xx-xxx-xx-xx.us-east-2.compute.amazonaws.com",
                    "State": {
                        "Code": 16,
                        "Name": "running"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "2018-07-07T15:44:33.000Z",
                    "PublicIpAddress": "xx.xxx.xx.xx",
                    "PrivateIpAddress": "xxx.xx.xx.xxx",
                    "ProductCodes": [],
                    "VpcId": "vpc-xxxxxxxx",
                    "CpuOptions": {
                        "CoreCount": 1,
                        "ThreadsPerCore": 1
                    },
                    "StateTransitionReason": "",
                    "InstanceId": "i-xxxxxxxxxxxxxx",
                    "EnaSupport": true,
                    "ImageId": "ami-xxxxxxxxxx",
                    "PrivateDnsName": "ip-xxx-xx-xx-xxx.us-east-2.compute.internal",
                    "KeyName": "MyKeyPair",
                    "SecurityGroups": [
                        {
                            "GroupName": "launch-wizard-1",
                            "GroupId": "sg-xxxxxxxxxx"
                        }
                    ],
                    "ClientToken": "",
                    "SubnetId": "subnet-xxxx",
                    "InstanceType": "t2.micro",
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use",
                            "MacAddress": "0a:xx:xx:xx:xx:xx",
                            "SourceDestCheck": true,
                            "VpcId": "vpc-xxxxxxb",
                            "Description": "",
                            "NetworkInterfaceId": "eni-xxxxxx",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-xxx-xx-xx-xx.us-east-2.compute.internal",
                                    "PrivateIpAddress": "xxx.xx.xx.xxx",
                                    "Primary": true,
                                    "Association": {
                                        "PublicIp": "xx.xx.xx.xx",
                                        "PublicDnsName": "ec2-xx-xxx-xx-xx.us-east-2.compute.amazonaws.com",
                                        "IpOwnerId": "amazon"
                                    }
                                }
                            ],
                            "PrivateDnsName": "ip-xxx-xx-xx-xxx.us-east-2.compute.internal",
                            "Attachment": {
                                "Status": "attached",
                                "DeviceIndex": 0,
                                "DeleteOnTermination": true,
                                "AttachmentId": "eni-attach-xxxxxxxxxxx",
                                "AttachTime": "2018-07-07T15:44:33.000Z"
                            },
                            "Groups": [
                                {
                                    "GroupName": "launch-wizard-1",
                                    "GroupId": "sg-xxxxxxxx"
                                }
                            ],
                            "Ipv6Addresses": [],
                            "OwnerId": "xxxxxxxxxx",
                            "PrivateIpAddress": "xxx.xx.xx.xxx",
                            "SubnetId": "subnet-xxxxx",
                            "Association": {
                                "PublicIp": "xx.xxx.xx.xx",
                                "PublicDnsName": "ec2-xx-xxx-xx-xx.us-east-2.compute.amazonaws.com",
                                "IpOwnerId": "amazon"
                            }
                        }
                    ],
                    "SourceDestCheck": true,
                    "Placement": {
                        "Tenancy": "default",
                        "GroupName": "",
                        "AvailabilityZone": "us-east-2c"
                    },
                    "Hypervisor": "xen",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/xvda",
                            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": true,
                                "VolumeId": "vol-xxxxxxxxxx",
                                "AttachTime": "2018-07-07T15:44:33.000Z"
                            }
                        }
                    ],
                    "Architecture": "x86_64",
                    "RootDeviceType": "ebs",
                    "RootDeviceName": "/dev/xvda",
                    "VirtualizationType": "hvm",
                    "Tags": [
                        {
                            "Value": "MyServer",
                            "Key": "Name"
                        }
                    ],
                    "AmiLaunchIndex": 0
                }
            ],
            "ReservationId": "r-xxxxxxxxxxxxxx",
            "Groups": [],
            "OwnerId": "xxxxxxxxxxx"
        }
    ]
}

Now we know how to set up AWS configuration for AWS CLI to manage the services from the command line.

Please post your comments and give feedback about this article.

[..] David

AWS CLI – AWS Configuration and Connect to EC2 Instance

6 thoughts on “AWS CLI – AWS Configuration and Connect to EC2 Instance

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top