Creating the First EC2 Machine Using AWS CLI

We will need the ID of the VPC, the ID of the Security Group, the ID of the Subnet, the name of the key pair and the AMI of the OS we are going to use (Ubuntu 16.04). The latter depends on the zone.

Describing VPCs

Let’s see what we have as VPCs:

aws ec2 describe-vpcs

You should get a detailed list of your active VPCs:

{

"Vpcs": [

{

"DhcpOptionsId": "dopt-xxxxxx",

"CidrBlock": "172.31.0.0/16",

"InstanceTenancy": "default",

"State": "available",

"IsDefault": true,

"VpcId": "vpc-xxxxx"

},

{

"DhcpOptionsId": "dopt-xxxxx",

"CidrBlock": "172.20.0.0/16",

"InstanceTenancy": "default",

"State": "available",

"Tags": [

{

"Key": "xxxxx",

"Value": "xxxxx"

},

{

"Key": "xxxxxx",

"Value": "xxxxx"

},

{

"Key": "xxxxxx",

"Value": "xxxxxx"

}

],

"IsDefault": false,

"VpcId": "vpc-xxxxxx"

}

]

}

Listing the Security Groups

aws ec2 describe-security-groups

You should get a list:

{

"IpPermissionsEgress": [

{

"IpProtocol": "-1",

"IpRanges": [

{

"CidrIp": "0.0.0.0/0"

}

],

"UserIdGroupPairs": [



],

"PrefixListIds": [



]

}

],

"Tags": [

{

"Key": "Name",

"Value": "xxxxxx"

}

],

"OwnerId": "xxxxx",

"GroupName": "xxxxxxxx",

"VpcId": "vpc-xxxxxxx",

"Description": "xxxxxx",

"IpPermissions": [

{

"IpProtocol": "-1",

"IpRanges": [

{

"CidrIp": "0.0.0.0/0"

}

],

"UserIdGroupPairs": [



],

"PrefixListIds": [



]

},

{

"IpRanges": [

{

"CidrIp": "0.0.0.0/0"

}

],

"ToPort": 22,

"UserIdGroupPairs": [



],

"PrefixListIds": [



],

"IpProtocol": "tcp",

"FromPort": 22

},

{

"IpRanges": [

{

"CidrIp": "0.0.0.0/0"

}

],

"ToPort": 2376,

"UserIdGroupPairs": [



],

"PrefixListIds": [



],

"IpProtocol": "tcp",

"FromPort": 2376

}

],

"GroupId": "sg-xxxxxx"

}

...

Describing Availability Zones & Choosing the AMI:

aws ec2 describe-availability-zones

Even if we’re not going to use this directly but to find the AMI, we should get this information first:

{

"AvailabilityZones": [

{

"Messages": [],

"RegionName": "eu-west-1",

"ZoneName": "eu-west-1a",

"State": "available"

},

{

"Messages": [],

"RegionName": "eu-west-1",

"ZoneName": "eu-west-1b",

"State": "available"

},

{

"Messages": [],

"RegionName": "eu-west-1",

"ZoneName": "eu-west-1c",

"State": "available"

}

]

}

Since I am going to use Ubuntu, I used this website: https://cloud-images.ubuntu.com/locator/ec2/

Describing the Subnets

aws ec2 describe-subnets

And you get a similar list to this:

{

"AvailableIpAddressCount": 4091,

"MapPublicIpOnLaunch": true,

"AvailabilityZone": "eu-west-1b",

"VpcId": "vpc-xxxxxx",

"State": "available",

"DefaultForAz": true,

"CidrBlock": "172.31.0.0/20",

"Tags": [

{

"Value": "xxxxxxxxxx",

"Key": "Name"

}

],

"SubnetId": "subnet-xxxxxxxxxx"

}

Getting the Key Pairs

aws ec2 describe-key-pairs

And it will show the list of existent key pairs:

{

"KeyPairs": [

{

"KeyFingerprint": "xxxxxxxxxxx",

"KeyName": "xxxxxxxx"

},

{

"KeyFingerprint": "xxxxxxxxx",

"KeyName": "xxxxxxxx"

}

]

}

Let’s Create the Machine

aws ec2 run-instances --image-id ami-785db401 --count 1 --instance-type t2.micro --key-name .xxxxx --security-group-ids sg-xxxxx --subnet-id subnet-xxxxx --associate-public-ip-address --query 'Instances[0].InstanceId' --output text

This should show us the id of the instance:

i-0ed688fc95b1feeb2

Get the Public DNS

In order to connect using SSH, we should get this !

aws ec2 describe-instances --instance-ids i-0ed688fc95b1feeb2 --query 'Reservations[0].Instances[0].PublicDnsName' --output text

And the output was:

ec2-34-253-201-12.eu-west-1.compute.amazonaws.com

Let’s Try the Public IP !

Same as DNS, type:

aws ec2 describe-instances --instance-ids i-0ed688fc95b1feeb2 --query 'Reservations[0].Instances[0].PublicIpAddress' --output text

Output:

34.253.201.12

I Need the Private IP

In my case I am going to connect the Salt Minion to the Salt Master using the private IP, you can do the same if you will create both machines in the same VPC.

If you want to use the private IP :

aws ec2 describe-instances --instance-ids i-0ed688fc95b1feeb2 --query 'Reservations[0].Instances[0].PrivateIpAddress' --output text

Output:

172.31.4.106

Installing the Master

Using the public DNS and your Key Pair connect to the created machine: