Amazon web services EC2
AWS EC2 lets you spin up virtual servers with ease. You can do it from the web interface, but here I show
how it can be done using awscli
.
Prerequisites
apt install awscli
run aws configure
, so that you can authenticate.
~/workspace$ aws configure
AWS Access Key ID [****************GVLA]:
AWS Secret Access Key [****************TSxC]:
Default region name [eu-west-1]:
Default output format [json]:
You can see the values I passed in
Problems encountered
- I first used an availability zone instead of a region,
eu-west-1a
Vs.eu-west-1
~$ ping ec2.eu-west-1a.amazonaws.com
ping: ec2.eu-west-1a.amazonaws.com: Name or service not known
- I did not use
file://
to specify the filepath to the public key when importing a keypair
~$ aws ec2 import-key-pair --key-name "default_key" --public-key-material file://~/.ssh/id_rsa.pub
{
"KeyFingerprint": "fa:a7:a9:ba:94:f7:7f:1d:3b:c2:22:1b:2c:79:0f:a4",
"KeyName": "default_key"
}
Using awscli to manage virtual machines
I followed the following guide
Running a new instance
I made an EC2 instance through the GUI first, to see what --image-id
was available for the free tier, and which
--subnet-id
to use etc.
~/workspace$ bash -x launch.sh
+ aws ec2 run-instances --image-id ami-ca0135b3 --count 1
--instance-type t2.micro --key-name default_key
--security-group-ids sg-c36233be --subnet-id subnet-70bd0216
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-ca0135b3",
"InstanceId": "i-070e1f75ab147c6c8",
"InstanceType": "t2.micro",
"KeyName": "default_key",
"LaunchTime": "2018-06-09T14:02:55.000Z",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "eu-west-1a",
"GroupName": "",
"Tenancy": "default"
},
........
}
],
"OwnerId": "032077489097",
"ReservationId": "r-0f951329abd7cb1de"
}
Describing instances
Reservations[].Instances[].FieldType
notice that you can query for other fields as well.
~/workspace$ bash -x describe.sh
+ aws ec2 describe-instances --filters Name=instance-type,Values=t2.micro
--query 'Reservations[].Instances[].InstanceId'
[
"i-04ae1f2bcf5927707",
"i-070e1f75ab147c6c8"
]
Terminating
~/workspace$ bash -x terminate.sh i-070e1f75ab147c6c8
+ aws ec2 terminate-instances --instance-ids i-070e1f75ab147c6c8
{
"TerminatingInstances": [
{
"CurrentState": {
"Code": 32,
"Name": "shutting-down"
},
"InstanceId": "i-070e1f75ab147c6c8",
"PreviousState": {
"Code": 16,
"Name": "running"
}
}
]
}
Terminated instances will be removed from the console interface after some time.