aws cli

最後更新: 2021-08-19

目錄

 


Install aws cli

 

The AWS CLI version 2 has no dependencies on other Python packages.

It has a self-contained, embedded copy of Python included in the installer.

Install

# For the latest version of the AWS CLI

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

unzip awscliv2.zip

# The default value is "/usr/local/aws-cli" 及 "/usr/local/bin"

./aws/install

Install Opts

--install-dir or -i

This option specifies the directory to copy all of the files to.

The default value is /usr/local/aws-cli.

--bin-dir or -b

This option specifies that the main aws program in the install directory is

symbolically linked to the file aws in the specified path.

The default value is "/usr/local/bin"

i.e.

./aws/install --install-dir /opt/aws-cli --bin-dir /usr/sbin

ls -l /usr/sbin/aws

lrwxrwxrwx 1 root root 31 Sep 12 23:14 /usr/sbin/aws -> /opt/aws-cli/v2/current/bin/aws

Checking

aws --version

aws-cli/2.2.30 Python/3.8.8 Linux/3.10.0-1160.36.2.el7.x86_64 exe/x86_64.centos.7 prompt/off

 

 


Command Completion

 

# bash shell

# -C command

complete -C /opt/aws-cli/v2/current/bin/aws_completer aws      # 加入 .bashrc 裡

 


awscli Usage

 

help

  aws help

  aws <command> help

  aws <command> <subcommand> help

usage

aws [options] <command> <subcommand> [<subcommand> ...] [parameters]

 


Configure user identity

 

aws configure

之後設定

  • Access key ID
  • Secret access key
  • AWS Region                  # i.e. ap-east-1
  • Output format               # i.e. yaml

Output format

json(default), yaml, text, table(+|-)

Profiles

A collection of settings is called a profile. By default, the AWS CLI uses the default profile.

You can create and use additional named profiles with varying credentials and settings by

specifying the "--profile option" and assigning a name.

aws s3 ls --profile produser

To create access keys for an IAM user

Go https://console.aws.amazon.com/iam/

Users

> "Add users" Button

> Access type: Programmatic access

> Attach existing policies directly: MyImportExportVM

Test

aws ec2 describe-instances

 


User Configure File

 

~/.aws/config

[default]
aws_access_key_id = ?????????
aws_secret_access_key = ????????????????
region = ap-southeast-1
color on
output text

 


EC2 CLI

 

# 查看現有 instances

aws ec2 describe-instances

# Start instance

aws ec2 start-instances --instance-ids i-1348636c

DOC

http://docs.aws.amazon.com/cli/latest/reference/ec2/index.html

 


S3 CLI

 

# List

aws s3 ls s3://mybucket

i.e.

aws s3 ls

2021-08-19 03:21:44 my-import-export-bucket

aws s3 ls s3://my-import-export-bucket

2021-08-19 03:22:55 1063377408 c7.mini.ova

Opts

  • --human-readable

# Upload & Download

# Download File

aws s3 cp s3://mybucket/myfolder/file /backup

Completed 826.2 MiB/25.6 GiB (56.8 MiB/s) with 1 file(s) remaining

# Upload Folder

aws s3 cp myfolder s3://mybucket/myfolder --recursive

# Sync Content

aws s3 sync myfolder s3://mybucket/myfolder --exclude *.tmp

# Delete

aws s3 rm s3://mybucket/myfolder/file

Doc

http://docs.aws.amazon.com/cli/latest/reference/s3/index.html

 


Other