More advanced usages of AWSCLI

Besides accessing S3, AWSCLI can control any kinds of AWS resources you can imagine. A very useful one is aws ec2 run-instances (official doc) , as it saves a lot of time clicking through the console.

While aws s3 xxx are often used on EC2 instances to interact with S3, aws ec2 xxx are mostly used on local computers to control remote servers.

Launch on-demand instances

Use this bash script to launch an on-demand instance:

#!/bin/bash

# == often change ==
TYPE=r4.large   # EC2 instance type

# ==  set it once and seldom change ==
AMI=ami-xxxxxxx # AMI to launch from
SG=sg-xxxxxxxx  # security group ID
KEY=xxx-key     # EC2 key pair name
COUNT=1         # how many instances to launch
IAM=xxxxx       # EC2 IAM role name
EBS_SIZE=200    # root EBS volume size (GB)

# == almost never change; just leave it as-is ==
aws ec2 run-instances --image-id $AMI \
    --security-group-ids $SG --count $COUNT \
    --instance-type $TYPE --key-name $KEY \
    --iam-instance-profile Name=$IAM \
    --block-device-mapping DeviceName=/dev/sda1,Ebs={VolumeSize=$EBS_SIZE}
  • TYPE: EC2 instance type.

  • AMI: The AMI you want to launch from, such as our tutorial AMI.

  • SG: The security group you want to assign to your EC2 instance.

  • KEY: The EC2 Key Pair for ssh. For example, in the quick start demo I used my-aws-key.

  • COUNT: You can launch multiple instances with exactly the same configurations

  • IAM: The IAM role for your EC2 instance. Primarily for granting S3 access.

  • EBS_SIZE: The size of disk (EBS volume).

Request spot instances

For spot instances, simply add --instance-market-options '{"MarketType":"spot"} option to aws ec2 run-instances. Other options are exactly the same as the on-demand case:

#!/bin/bash

# == often change ==
TYPE=c5.4xlarge  # EC2 instance type

# ==  set it once and seldom change ==
AMI=ami-xxxxxxx # AMI to launch from
SG=sg-xxxxxxxx  # security group ID
KEY=xxx-key     # EC2 key pair name
COUNT=1         # how many instances to launch
IAM=xxxxx       # EC2 IAM role name
EBS_SIZE=200    # root EBS volume size (GB)

# == almost never change; just leave it as-is ==
aws ec2 run-instances --image-id $AMI \
    --security-group-ids $SG --count $COUNT \
    --instance-type $TYPE --key-name $KEY \
    --iam-instance-profile Name=$IAM \
    --block-device-mapping DeviceName=/dev/sda1,Ebs={VolumeSize=$EBS_SIZE} \
    --instance-market-options '{"MarketType":"spot"}'

By default, the on-demand pricing will be used as the spot price limit. You can further set a custom limit (generally not necessary):

--instance-market-options '{"MarketType":"spot", "SpotOptions": {"MaxPrice": "0.68"}}'

The command aws ec2 request-spot-instances (doc) can also launch spot instances, but its syntax is slightly more complicated.

Other use cases

You may also describe EC2 instances or stop EC2 instances using AWSCLI. But it is very quick to do so from the console, too.