Skip to content

Commit

Permalink
Add advanced AWSCLI
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaweiZhuang committed Mar 12, 2018
1 parent 900344e commit 3bcb7c0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
73 changes: 73 additions & 0 deletions doc/source/chapter03_advanced-tutorial/advanced-awscli.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
More advanced usages of AWSCLI
==============================

Besides accessing S3, AWSCLI can control any kinds of AWS resources you can imagine. Very useful ones are ``aws ec2 run-instances`` (`official doc <https://docs.aws.amazon.com/cli/latest/reference/ec2/run-instances.html>`_) and ``aws ec2 request-spot-instances`` (`doc <https://docs.aws.amazon.com/cli/latest/reference/ec2/request-spot-instances.html>`_) as they save a lot of time clicking throught the console.

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

# == 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

- **TYPE**: `EC2 instance type <https://aws.amazon.com/ec2/instance-types/>`_.
- **AMI**: The AMI you want to launch from, such as :doc:`our tutorial AMI <../chapter06_appendix/aws-resources-for-gc>`.
- **SG**: The :doc:`security group <../chapter02_beginner-tutorial/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 :ref:`my-aws-key <keypair-label>`.
- **COUNT**: You can launch multiple instances with exactly the same configurations
- **IAM**: The :doc:`IAM role <./iam-role>` for your EC2 instance. Primarily for granting S3 access.

Request spot instances
----------------------

For spot instances, use::

#!/bin/bash

# == often change ==
TYPE=c5.4xlarge # EC2 instance type
MAXPRICE=0.68 # Price limit

# == 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

# == almost never change; just leave it as-is ==
aws ec2 request-spot-instances --spot-price "$MAXPRICE" \
--instance-count $COUNT --type "one-time" --launch-specification \
'{
"ImageId": "'"$AMI"'",
"InstanceType": "'"$TYPE"'",
"KeyName": "'"$KEY"'",
"SecurityGroupIds": [ "'"$SG"'" ],
"IamInstanceProfile": {"Name": "'"$IAM"'"}
}'

- **MAXPRICE**: Usually use `on-demand pricing <https://aws.amazon.com/ec2/pricing/on-demand/>`_ as the limit. :ref:`Check out the actual spot pricing <spot-label>` to see the most cost-effective one.

Other options are exactly the same as the on-demand case.

Other use cases
---------------

You may also `describe EC2 instances <https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html>`_
or `stop EC2 instances <https://docs.aws.amazon.com/cli/latest/reference/ec2/stop-instances.html>`_ using AWSCLI. But it is very quick to do so from the console, too.
3 changes: 3 additions & 0 deletions doc/source/chapter03_advanced-tutorial/iam-role.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enable S3 access from EC2 by IAM role
=====================================

2 changes: 2 additions & 0 deletions doc/source/chapter03_advanced-tutorial/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ This chapter provides advanced tutorials to improve your research workflow. Make
.. toctree::
:maxdepth: 2

iam-role
advanced-awscli
container
hpc-overview

0 comments on commit 3bcb7c0

Please sign in to comment.